Undefined Reference when calling Win32 API Function - c++

I am trying to use the SetCursorPos function to move my cursor in Windows 10 with C++.
Here is my code:
#include <Windows.h>
int main()
{
SetCursorPos(100,100);
return 0;
}
Whenever I run the code, I get this error:
undefined reference to `SetCursorPos#8'
I have read through What is an undefined reference/unresolved external symbol error and how do I fix it?, but I could not find a solution to my problem.
I am sure that I missed something, but I have no clue to what that something is; sorry if the answer is posted elsewhere.

Microsoft ships the SetCursorPos function inside user32 library (see MSDN)
If you use Microsoft Visual C++ compiler, you can add statically that library to your project using the name "user32.lib".
If you use GCC instead, the name has another extension: "user32.a". Normally the ".a" extension is defaulted in GCC, so no need to pass it to the compiler.
If you were to add libraries that are not in a GCC-knowing path, then you need the "-L" flag to tell GCC where to look for that libraries. The "-l" flag (lowercase L) tells GCC to use that library.
Sumarizing:
g++ movecursor.cpp -luser32

Related

win32++ library sample code results in undefined reference error

I know I asked this yesterday already but the suggested solution in this thread:
error LNK1120: 1 unresolved external - VS13 C
Did not work :/
I am getting this error:
CMakeFiles\testproject.dir/objects.a(main.cpp.obj): In function
`Win32xx::LoadCommonControls()':
PATH/lib/Win32xx891/include/wxx_wincore.h:2844: undefined reference to
`__imp_InitCommonControls'
PATH/lib/Win32xx891/include/wxx_wincore.h:2849: undefined reference to
`__imp_InitCommonControls'
while trying to run example code from the win32++ library. I have tried adding
#pragma comment(lib, "comctl32.lib")
To my header as was suggested in the thread I mentioned but that didn't work. They also said that you can solve it by linking the comctl32 library, which appearently isnt loaded and causes the issue, by adding -L -lcomctl32 to my program arguments (I think thats how to do it, correct me if im wrong). That didnt help either. If you know what the problem is please help me.
EDIT:
steps:
create project (c++ 14)
download win32++ library files
add them with cmake (I called include_directories(), is that enough?)
https://pastebin.com/w59ibVEZ
run program with "-lcomctl32" as program argument
rip
#pragma comment(lib, "comctl32.lib") is for MSVC, it doesn't do anything in MinGW (or GCC for that matter).
Symbol InitCommonControls is defined in libcomctl32.a which comes with MinGW, so you just need to link against by using linker flag -lcomctl32.
Make sure to tell CMake it's a linker flag and not a compiler flag.

Mingw64 Linker error when trying to include -lhid [duplicate]

Context: I'm using Qt 5.9.3 on Windows, building for MinGW 32-bit. The Qt part is a side issue though - the problem seems to be with MinGW. The version of MinGW is 4.3.0, supplied prebuilt as part of the Qt installation.
I'm building a library which talks to a USB device over HID. Everything compiles fine, but it fails at the link stage with
./..\..\object\debug\usb_hid_device.o: In function `ZN8MyApp3USB5Win3213getDevicePathB5cxx11Ell':
<MYPATH>/../../source/win32/usb_hid_device.cpp:99: undefined reference to `HidD_GetAttributes(void*, _HIDD_ATTRIBUTES*)#8'
./..\..\object\debug\usb_hid_device.o: In function `ZN8MyApp3USB5Win3214CHIDDeviceImplC2EllRNS_15LogPerComponentE':
<MYPATH>/../../source/win32/usb_hid_device.cpp:200: undefined reference to `HidD_FlushQueue(void*)#4'
The linker command is
g++ -shared -mthreads -Wl,-subsystem,windows -Wl,--out-implib,<MYPATH>\bin\debug\libusb_hid_comms.a -o <MYPATH>\bin\debug\usb_hid_comms.dll object_script.usb_hid_comms.Debug -lhid -lsetupapi -LC:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib C:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\libQt5Guid.a C:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\libQt5Cored.a
If I omit -lhid I get the same errors. I also get the same errors if I remove -lhid and explicitly set the path and filename to libhid.a. If I deliberately mistype the path and filename, it comes up with an error, so I know the command-line is getting parsed correctly. But for whatever reason, MinGW appears to not be linking with one of its own library files.
I've also tried removing -lsetupapi and I get the linker errors I'd expect for the functions defined in there. Likewise the Qt library files. But it seems that specifically for libhid.a, MinGW can see the library file but just isn't going to link with it.
Has anyone else seen this? Or can anyone else with the same (or similar) version of MinGW confirm or deny that they can link with libhid.a? Or is there something obviously wrong with what I'm doing?
I've just found the answer. I'm posting an answer myself so that other people know in future, because I think this is still a valid question which people might want to know about.
The problem is the include file hidsdi.h. The majority of other header files which pull in Win32 API calls have extern "C" around the function declarations. However this one doesn't! The result is that we end up with C++ name mangling for linker symbols, instead of the C-style "_" in front of the linker symbols.
The solution is to use
extern "C"
{
#include <hidsdi.h>
}
and then everything works fine.
The version of hidsdi.h with the older version of MinGW (which I'm porting from) did have that protection around the function declarations. However it looks like it's gone in the newer version.

Symbol lookup error when using -Wl,--defsym GCC option

I've got a question regarding using one of the GCC linker options: -Wl,--defsym.
Some time ago I decided to rewrite one of my projects in C++ but without using its standard library and without even linking to it (I compile .cpp source files to object files using C++ compiler but I link them using C compiler).
For that I used following compiler flags:
-fno-exceptions -fno-rtti -nostdlib -nodefaultlibs
And following linker options:
-Wl,--defsym -Wl,__cxa_pure_virtual=0
Using those flags I got my shared library compiling and linking fine.
But after I try to use my shared library in some simple program (also compiled and linked using above flags) I get following error while running it:
examples/bin/blink: symbol lookup error: examples/bin/libblink.so: undefined symbol: __cxa_pure_virtual
where blink is the name of the executable and libblink.so is the name of my shared library.
I tried to fix it and it looks like replacing --Wl,--defsym linker flag (for both executable and library) with this function:
extern "C" void __cxa_pure_virtual
{
while (true);
}
does the job. Why is the --Wl,--defsym not working in this case?
I'd also like to mention that I tested this under Windows and it works fine there.
I think that I've found an answer to my question.
Changing the symbol address from 0 to any other value fixes my issue.
So instead of having:
--Wl,--defsym --Wl,__cxa_pure_virtual=0
I have:
--Wl,--defsym --Wl,__cxa_pure_virtual=1
This way runtime linker does not look for a symbol (which I think is the case when the address is set to 0).

How to force MSVC to link an unused static library (Equivalent of GCCs --whole-archive parameter) [duplicate]

This question already has answers here:
What is the Microsoft Visual Studio equivalent to GCC ld option --whole-archive
(7 answers)
Closed 6 years ago.
There is a static library which is used by the main program indirectly. Therefore there is no missing reference and the static library is not linked.
The same problem was posted here for the GCC. And the proposed solution worked for me as well. But now I need to build the program with MSVC. So I suppose that I need the equivalent of GCC's --whole-archive parameter?
At the moment the linker call is like that: link.exe /nologo /out:program.exe staticLib.lib main.obj
Can someone help me with that?
I recently met with the same problem while trying to link googletest with library containing the tests and found the following note in their primer :
When you define your tests, Google Test creates certain static objects
to register them. These objects are not referenced from elsewhere but
their constructors are still supposed to run. When Visual C++ linker
sees that nothing in the library is referenced from other places it
throws the library out. You have to reference your library with tests
from your main program to keep the linker from discarding it. Here is
how to do it. Somewhere in your library code declare a function:
__declspec(dllexport) int PullInMyLibrary() { return 0; }
If you put your tests in a static library (not DLL) then __declspec(dllexport) is
not required. Now, in your main program, write a code that invokes
that function:
int PullInMyLibrary();
static int dummy = PullInMyLibrary();
This will
keep your tests referenced and will make them register themselves at
startup.
In addition, if you define your tests in a static library, add
/OPT:NOREFto your main program linker options.
While this may be no the most beautiful workaround ever this certainly worked for me and I think if there would be the one better they would have mentioned it in their primer instead.

Blowfish and undefined reference to `BF_set_key'

I have installed Win64 OpenSSL v1.0.1b and Visual C++ 2008 Redistributables from this page http://slproweb.com/products/Win32OpenSSL.html and added compiler (C:\OpenSSL-Win64\include) and linker paths (C:\OpenSSL-Win64\bin, C:\OpenSSL-Win64\lib, C:\OpenSSL-Win64) to Code::Blocks, but I still cannot compile my program.
Source code:
#include <cstring>
#include <openssl/blowfish.h>
int main() {
const char * key = "aaabbbcccdddeeefffggghh";
BF_KEY bfKey;
BF_set_key(&bfKey, strlen(key), (const unsigned char *) key);
return 0;
}
Error:
obj\Release\main.o:main.cpp|| undefined reference to `BF_set_key'|
I tried to add -lssl, -lopenssl, -llibssl, -lcrypto, but it doesn't work. Then I can see another error:
ld.exe||cannot find -lssl|
I have no idea (and Google also) what to do. Any ideas what I do wrong?
I'm not sure if you configured it properly. It seems like you also have to add the libraries your project is using somewhere in Build Options, on top of setting the library directories. Does this help? http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/
Because you are using the GCC compiler (MinGW) with Code::Blocks you have to change the library sear directory (C:\OpenSSL-Win64\lib) to C:\OpenSSL-Win64\lib\MinGW and to link the library that have the Blowfish function you must use -leay32 (in your case probably is -leay64).
Inside the directory C:\OpenSSL-Win64\lib\MinGW there are 2 files with the .def extensin that have the list of functions exported by each library (libeay32.a/libeay64.a and ssleay32.a/ssleay64.a), by the way if you use the -l option the file must be called lib.a; the if you want to use any of the functions on the library ssleay32.a/ssleay64.a you must link the file directly (for example C:\OpenSSL-Win64\lib\MinGW\ssleay32.a) or append lib to the name of the file.