vtk autoinit linker error - c++

I have a very confusing (at least for me :) ) problem with linking to vtk-libraries. On one desktop pc, my code compiles without any error. I have an identical installation on another pc, but there, I get linker errors.
On both pc's are installed: itk 4.8, vtk 6.3 and cmake 3.3. Both are debian-systems (8.1 - jessie).
[ 96%] Linking CXX executable project
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolume_AutoInit::~vtkRenderingVolume_AutoInit()':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Destruct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolumeOpenGL_ModuleInit::~vtkRenderingVolumeOpenGL_ModuleInit()':
project/main.cpp:4: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Destruct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolumeOpenGL_ModuleInit':
project/main.cpp:4: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolume_AutoInit':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
libcommon.a(datareader.cpp.o): In function vtkRenderingVolume_AutoInit':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
collect2: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:326: recipe for target 'project' failed
I tried to fix those errors with adding
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
to the file where I am using the vtk libraries, but it didn't help.
Does anyone know what I'm doing wrong? I am wondering, why it works on one of those pcs but not on the other one.
thank you very much!
Update: I fixed my problem by switching the position of
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
in the CMakeLists to the end. However, does anyone know, why it makes such a huge difference for different pcs?

As long as the VTK_USE_FILE is found, your code should compile correctly. In your other PC, VTK_USE_FILE was already found from a previous configuration and sits in a CMake cache entry, so that is why it compiles fine.

If your are using Nightly,
Your should put these in front of your including any vtk headers.
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
and link all the library you made from vtk source

Related

C++ build error using librealsense in ubuntu

I installed librealsense from the source. It was installed and build successfully.
When I tried to run the sample project (rs-hello-realsense) I have found the build error.
/usr/local/include/librealsense2/hpp/rs_types.hpp:90: undefined reference to `rs2_get_error_message'
/usr/local/include/librealsense2/hpp/rs_types.hpp:92: undefined reference to `rs2_get_failed_function'
/usr/local/include/librealsense2/hpp/rs_types.hpp:92: undefined reference to `rs2_get_failed_function'
Same kind of error from a different type of build.
CMakeFiles/rs-hello-realsense.dir/rs-hello-realsense.cpp.o: In function `rs2::error::error(rs2_error*)':
rs-hello-realsense.cpp:(.text._ZN3rs25errorC2EP9rs2_error[_ZN3rs25errorC5EP9rs2_error]+0x2e): undefined reference to `rs2_get_error_message'
rs-hello-realsense.cpp:(.text._ZN3rs25errorC2EP9rs2_error[_ZN3rs25errorC5EP9rs2_error]+0x73): undefined reference to `rs2_get_failed_function'
Finally, get it. Need to add a couple of lines in CMakeList.txt.
Which linked the libraries in the project.
find_package(realsense2 2.29.0)
target_link_libraries(rs-hello-realsense ${DEPENDENCIES} ${realsense2_LIBRARY})
OR
target_link_libraries(${PROJECT_NAME} ${realsense2_LIBRARY})
Other than all other properties of CMake File will be same

How to solve mingw-w64 MSVCRT dependency issues

I'm building a golang project by using Go1.12 + msys2/mingw-w64(9.2.0), it shows errors:
D:\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o: in function `pre_c_init':
E:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:146: undefined reference to `__p__fmode'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o: in function `__tmainCRTStartup':
E:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:290: undefined reference to `_set_invalid_parameter_handler'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:299: undefined reference to `__p__acmdln'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-merr.o): in function `_matherr':
E:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/merr.c:46: undefined reference to `__acrt_iob_func'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-pseudo-reloc.o): in function `__report_error':
E:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/pseudo-reloc.c:149: undefined reference to `__acrt_iob_func'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/pseudo-reloc.c:150: undefined reference to `__acrt_iob_func'
collect2.exe: error: ld returned 1 exit status
After some research I'm sure it's a msvcrt compatibility issue of mingw64.
I know there are workarounds like this: unresolved external symbol __imp__fprintf and __imp____iob_func, SDL2
But it doesn't solve all of my issues.
I try to link the library: LDFLAGS: -L%filepath% -llegacy_stdio_definitions, nothing changes.
Is there a patch to fix it, or do I have to build my custom version of mingw64?
So I answer my question by myself, the case is closed.
Yes, Mingw64 uses msvcrt.
Here are some tips that could help to solve dependency problems between Mingw64 & msvcrt.
The major goal is to let compiler load the correct version of MSVCRT library family.
1.Update Mingw64.
2.Check out MSVCRT_VERSION in Mingw64\include_mingw.h(or x86_64-w64-mingw32\include), make sure the version number matches your windows version, or modify it.
3.Take a look at linker option like "#cgo LDFLAGS: -Lxxx", don't mess the library path. Remove any unnecessary library path setting, let linker finds libraries itself.
Make sure everything is built with same libraries.

QtAddon SerialPort from Qt4 to Qt5

I am migrating a project from Qt4 to Qt5. I finished migrating the project itself and now I am working on the libraries. So far I haven't had many problems converting and linking them to the project, but this one is throwing undefined references.
The project uses QtSerialPort, and it compiled perfectly using the Qt4 version of it, and it was linked like this in the .pro file:
-l:"C:/Users/Sprint/Desktop/swe/marssies/libQtSerialPort.a" \
I have compiled the Serial Port library with/for Qt5 and linked it as follows:
-l:"C:/Users/Sprint/Desktop/swe/marssies/libQt5SerialPort.a" \
But I keep getting these errors:
./release\gpssettingswidget.o:gpssettingswidget.cpp:(.text+0x4ecc): undefined reference to `QtAddOn::SerialPort::SerialPortInfo::availablePorts()'
Makefile.Release:922: recipe for target '..\Release\Swibz.exe' failed
./release\gpssettingswidget.o:gpssettingswidget.cpp:(.text+0x4f3e): undefined reference to `QtAddOn::SerialPort::SerialPortInfo::portName() const'
./release\gpssettingswidget.o:gpssettingswidget.cpp:(.text+0x4f5d): undefined reference to `QtAddOn::SerialPort::SerialPortInfo::description() const'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: ./release\gpssettingswidget.o: bad reloc address 0xd in section `.text$_ZN25Ui_GPSSettingsWidgetClass13retranslateUiEP14SettingsWidget[__ZN25Ui_GPSSettingsWidgetClass13retranslateUiEP14SettingsWidget]'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [..\Release\Swibz.exe] Error 1
I know its a linker error because of the "ld returned 1 exit status"
I have tried linking the libQt5SerialPortd.a, libQt5SerialPort.dll and libQt5SerialPortd.dll but it throws the same errors
(By the way, if someone knows the difference between libNAMEd.a and libNAME.a please share your knowledge)
Maybe the QtAddOn is now in another library? Because otherwise it shouldnt recognize many other functions, not only the ones with QtAddOn.
Thankyou very much. I don't know what I would do without stackoverflow.
EDIT Alright, I found this which tells to change any SerialPortInfo declared variable to QSerialPortInfo, I have done that in my project and I am getting new errors, but now I am not sure which #include I should put:
#include <QtSerialPort>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QtSerialPort/QtSerialPort>
I have tried each one of these and all give me errors. Ill keep trying and see if anything comes clear
QtSerialPort is now a part of Qt. It officially became part of Qt with the 5.1.0 release. If you use the new version of Qt there is no need to get the source code separately and link to it.
To use the module with Qt 5 add this line to your qmake project file:
QT += serialport
You can then include the header files. To use these classes in your application, use the following include statement:
#include <QtSerialPort/QtSerialPort>
There is quite good documentation for the classes. You can view it from Qt Assistant.

Code::Blocks SDL2.0 "undefined reference to 'IMG_Load'"

I'm trying to use c++, SDL, and SDL-image to make a game; I'm using Code::Blocks as my IDE, and I'm having some trouble.
I've downloaded the latest mingw SDL development files, and the latest mingw SDL-image development files.
I've placed all the SDL-image stuff into the same directory as the SDL stuff (merging the one into the other).
I've added the x86_64-w64-mingw32/include and x86_64-w64-mingw32/lib directories to my Code::Blocks search directories.
My linker settings are as follows:
-lmingw32
-lSDL2main
-lSDL2
-lSDL2_image
-lopengl32
-lglu32
(Obviously I'm using openGL as well, but I don't think that's involved here).
I have the correct #includes as far as I can tell:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_opengl.h>
But I'm still getting the error: undefined reference to 'IMG_Load'
Here's the build log:
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0xd7): undefined reference to `IMG_Load'
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0x1dd): undefined reference to `SDL_FreeSurface'
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0x1f4): undefined reference to `SDL_FreeSurface'
g:/program files(x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: obj\Release\gfx_Texture.o: bad reloc address 0x4 in section `.text.startup'
g:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
And the actual code:
SDL_Surface* image = IMG_Load(file);
Where file is a char*.
I understand that there have been some changes to the way SDL_Surfaces work in SDL 2.0 as compared to SDL 1.2 (I'm trying to migrate, both from 1.2 to 2.0 and from MVSC to C::B), so could that have something to do with it? Am I using IMG_Load wrong?
Your help is much appreciated, and I'll supply any missing info on request if it'll clarify anything.
One more thought: The latest version of SDL is 2.0.1, yet the latest version of SDL_image is 2.0.0. Do I need those versions to match?
This happen When the program doesn't linker correctly at the executed time
for example:
asas#asas:~/SDL$ g++ -Wall SDL_program.cpp -lSDL2
/tmp/cc1eC6CK.o: InfunctionLTexture::loadFromFile(std::__cxx11::basic_string<char,std::char traits<char>, std::allocator<char> >)':
SDL_program.cpp:(.text+0x7d): **undefined reference toIMG_Load'**
As you can see above miss linker -lSDL2_image .
Is possible too, that you have SDL1.2 and SDL2 at the same root and again Code blocks hasn't linked correctly
Well don't I feel dumb. When I was working in MVC the x86 prefix meant 32-bit; for some reason, it means 64-bit in C::B, and the i686 prefix means 32-bit.
I encountered such an error, and all I did was check if my linker texts are correctly written check if you have typed "ISDL2_image" instead of "lSDL2_image"
(write small 'L' then SDL2 not cap 'I' then SDL2)for SDL2 projects.
for SDL1 just change the '2's into '1's. 👍

Libusb undefined reference to

I'm trying to set up libusb API on my OS. I downloaded libusb api on libusb.org. I followed the standard installation procedure:
cd into directory
./configure
make
make check //without errors
make install
Then I launched Eclipse C/C++ and copied some code from the tutorial found on the internet. But when trying to build it I got following output:
main.cpp:(.text+0x19): undefined reference to `libusb_init'
main.cpp:(.text+0x76): undefined reference to `libusb_set_debug'
main.cpp:(.text+0x8a): undefined reference to `libusb_get_device_list'
main.cpp:(.text+0x136): undefined reference to `libusb_free_device_list'
main.cpp:(.text+0x142): undefined reference to `libusb_exit'
/tmp/ccOWJGwe.o: In function `printdev(libusb_device*)':
main.cpp:(.text+0x162): undefined reference to `libusb_get_device_descriptor'
main.cpp:(.text+0x28a): undefined reference to `libusb_get_config_descriptor'
main.cpp:(.text+0x4d4): undefined reference to `libusb_free_config_descriptor'
collect2: ld returned 1 exit status
I have libusb.so in /lib and also I have usb.h in /usr/local/include and the link for the .so and libusb.a in /usr/local/lib.
Also the #include inside the code is correct.
I know that problem is in linker but I, kind of, cannot make it work :)
I'm using Fedora 15 operating system and gcc 4.6.0 20110603 (Red Hat 4.6.0-10) version compiler.
So what could I do to resolve these undefined references? Thanks very much for help :)
I did face the same problem. But I was able to solve it by adding '-lusb-1.0' to the linker.
e.g : g++ myfile.cpp -lusb-1.0
you have to set the library linker flag for compilation in the linker,
you can get a full list in the console by executing
pkg-config --list-all
These are the libraries which you have installed on your system and you have to link against the ones you want to use.
so in your example it is libusb so you do
pkg-config --libs libusb
there should be the output
-lusb
or
-lusb-1.0
This gives you the flag you have to pass to the linker. e.g.
g++ myfile.cpp -lusb[-1.0]
Then you edit the configuration of the project and search for the linkerflags, there should be a textfield for that somewhere in the buildoptions. i'm not quite shure where to find it but googling for it suggested:
Project -> Properties -> C/C++
Build -> Miscellaneous -> flags
After you found it, just add the linker flag in the textfield and you should be fine.
EDIT
since my answer is the accepted one, I also added the other flag that seems to work for a lot of people.
What is your linker command line? You need to have -lusb in the linking command; just having the header included won't work.
I don't use Eclipse C/C++ but I am pretty sure the reason is the same that I faced some while ago when setting up a C project in Netbeans.
It's not enough to have the #include in your code and the library at the right location - you also have to tell Eclipse where to look for them and how to use them. This turorial shows you how to set it up in Eclipse.