win32++ library sample code results in undefined reference error - c++

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.

Related

Undefined Reference when calling Win32 API Function

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

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.

How to use 'boost_python-vc110-mt-gd-1_54.lib' in C++?

I am trying to use BoostPython to write a program in C++.
My presettings are:
a. Win32 Console Application.
b. Property->C/C++->General->Additional Include Directories->C:\Python27\include;C:\Program Files\boost\boost_1_54_0;
c. Property->Linker->General->Additional Library Directions->C:\Python27\libs;
d. Microsoft Visual Studio 2012 Express Version + Python 27 + Boost 1.54
And my code is like follows:
#include <boost/python.hpp>
using namespace boost::python;
int main( int argc, char ** argv )
{
return 0;
}
As you can see, this code is with an empty main function. And I got an error message
error LNK1104: cannot open file 'boost_python-vc110-mt-gd-1_54.lib'
I checked the installation of boost ('bootstrap.bat'+'bjam.exe'), and I've searched within the folder where boost library installed for 'boost_python-vc110-mt-gd-1_54.lib', but found nothing.
I looked for some similar posts, unlike my problem, the lib file they missed was with a prefix 'lib'. On the other hand, I found this post is very similar to mine. But I found the OP made a mistake when he/her solve his/hers problem. That is, Boost is compatible with python 2.2 and its newer versions. And the rest of his answer is not helpful to me.
I guess this problem could because either I've done something wrong during installation, or I was wrong when I link the libs to my application.
Can anyone help me out of this? A detailed solution will be great. Many thanks. :)
==============================================================
EDIT:
I've reinstalled the Boost with a prebuilt binary file. Now I can link to the lib file the compiler asked for. However, new problems are raised. Now I have two new errors:
error LNK2001: unresolved external symbol __imp___Py_NoneStruct
error LNK1120: 1 unresolved externals
When I comment out the line #include <boost/python.hpp>, the above errors are gone.
I really cannot figure out the reason. Can anyone help me with this? Many thanks.
You've added the C:\Python27\libs folder to you linker settings, but that doesn't have the boost files.
You need to run boost's bjam to generate the boost library files. Lots of boost is header only, but there are some libraries which you will need to build.
When you have done that you need to add the directory to
"Property->Linker->General->Additional Library Directories"
I've found out the error is raised because the Boost library was not properly installed in my computer. Actually, the key is bjam, and it must be run to build all these necessary libraries.
I would suggest anyone else that encounter with this problem, if you don't know how to run a bjam properly, go to search for a prebuilt binary file of Boost. That could save you a lot of time.

GMP Library, compile error in C++, MinGW, Code::Blocks

I have built GMP for MinGW. The IDE I'm using is Code::Blocks. I don't have any problems with C functions of GMP. But I'm having problem with C++. The program I tried to run is as simple as this,
#include<iostream>
#include<gmpxx.h>
using namespace std;
main()
{
mpz_class a;
a=12345;
cout<<"value"<<a<<"\n";
return 0;
}
And the Errors I get are
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osmpz.o):osmpz.cc|| undefined reference to `__gmpz_get_str'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osfuns.o):osfuns.cc:(.rdata+0x1c)||undefined reference to `__gmp_asprintf_memory'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osfuns.o):osfuns.cc:(.rdata+0x20)||undefined reference to `__gmp_asprintf_reps'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osdoprnti.o):osdoprnti.cc|| undefined reference to `__gmp_doprnt_integer'|
F:\Compilers\C_Libraries\GMP\lib\libgmpxx.a(osdoprnti.o):osdoprnti.cc|| undefined reference to `__gmp_asprintf_final'|
||=== Build finished: 5 errors, 0 warnings ===|
Now, some additional data:
I don't have any problem with C functions. And also, if I remove cout<< statement the file compiles and runs fine. The problem is probably with overloaded operators.
libgmpxx.a and libgmp.a are linked with compiler. It can be seen in the error messages too...
The problem is probably with the libgmpxx.a alone. So, I built the Library again, but the files are same.
I used this tutorial build GMP with MSYS for MinGW. http://www.cs.nyu.edu/exact/core/gmp/ and http://suchideas.com/journal/2007/07/installing-gmp-on-windows/
The version of GMP I'm using is 5.0.4.
So, what I want to know is, what could be the problem? And how could it be solved?
And, if unsolvable and if you have the working files for 5.0.4 version, please share it. :(
I suspect the command to build your program specifies the libgmp* libraries in the wrong order. Make sure the libgmp.a library is specified after the libgmpxx.a library:
-lgmpxx -lgmp
If they are specified in the other order, then when pulling in dependencies from libgmpxx.a library, the libgmp.a library won't be searched.
From the ld linker's docs on the -l option:
The linker will search an archive only once, at the location where it
is specified on the command line. If the archive defines a symbol
which was undefined in some object which appeared before the archive
on the command line, the linker will include the appropriate file(s)
from the archive. However, an undefined symbol in an object appearing
later on the command line will not cause the linker to search the
archive again.
See the -( option for a way to force the linker to search archives
multiple times.
You may list the same archive multiple times on the command line.

What is the meaning of these ' undefined reference to __glut*WithExit ' OpenGL linker errors? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
Original Question:
[Warning] passing GLfloat' for
converting 2 ofvoid
glutSolidSphere(GLdouble, GLint,
GLint)'
[Warning] passing GLfloat'
for converting 3 ofvoid
glutSolidSphere(GLdouble, GLint,
GLint)'
After applying one of the poster's suggestions of including glut.h, I now get this problem.
I can't use glutSolidSphere() without it.
[Linker error] undefined reference to
`__glutInitWithExit#12'
[Linker error] undefined reference to
`__glutCreateWindowWithExit#8'
[Linker error] undefined reference to
`__glutCreateMenuWithExit#8'
[Linker error] undefined reference to
`glutSolidSphere#16'
I'm using DEV C++, if that helps.
That looks mangled. Perhaps it's telling you that you're passing a GLfloat where a GLint was expected, as the 2nd and 3rd arguments? Please paste your exact error/warning output again, this doesn't look right.
The 'slices' and 'stacks' params are supposed to be integers; your code is passing in floats.
Edit: you really shouldn't edit your question to make it a completely different question. Undefined Reference means that you aren't linking in the library that contains those functions. You need to include GLuT in the list of libraries you're linking against.
As DNS said, but in simpler terms:
You need to link the GLUT library. I believe it is usually glut32.lib on Win32 machines. There should be an option for this somewhere in the project properties pages of your IDE; Sorry, I don't use Dev-C++, but in Visual Studio it is in the project properties, and then linker settings, and "Additional Library Includes".
Pass -lglut32 option to mingw's linker - Dev-Cpp uses mingw.
You'll have to find "linker options" textbox somewhere in Project Options. Sorry I cannot be more specific than that, it's been over a year since I used Dev-Cpp, and over three years since I really used it. It should be the same place where you added -lopengl32 and -lglu32.
Don't forget to install the GLUT devpak.
GLUTMingw32.zip file contains all files necessary to build under devcpp
I extracted it on desktop . added lib and include directories from devcpp project-project options menu to include and lib directories .
after added in project options dialog on parameters tab - > linker :
../../../../Dev-Cpp/lib/libopengl32.a
../../../../Dev-Cpp/lib/libglu32.a
../GLUTMingw32/GLUTMingw32/lib/libglut32.a
first two are in devcpp install dir/lib folder
/you can use -lopengl32 and -lglu32 instead , it works/
third one ,libglut32.a , I added from extracted GLUTMingw32.zip folder
placed glut32.dll in folder where project got generated and main.cpp resides
thats all .
after I got Permission denied
I just removed generated exe file and recompiled
if exe can't be deleted close devcpp
if it complains about short redefinition
add at the top of main.cpp
"# define _WCHAR_T_DEFINED"
without quotes