Undefined reference to. In cmake: lib to lib [duplicate] - windows

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
Windows 7 x86, cmake version 3.15.2, 4.10 qtcreator, Qt5.12.4 MinGW. I build a project with two libraries, and in one of them I call a class from the other. As a result, I get an error.
...file2.cpp:-1: ошибка: undefined reference to `MyClass1::MyClass1()'
collect2.exe:-1: ошибка: error: ld returned 1 exit status
[ 91%] Linking CXX executable flasher.exe
lib2/liblib2.a(file2.cpp.obj):file2.cpp:(.text+0x18): undefined reference to `MyClass1::MyClass1()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\my.dir\build.make:107: flasher.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:83: CMakeFiles/my.dir/all] Error 2
mingw32-make.exe: *** [Makefile:83: all] Error 2
Minimal reproducible example: githab.com Src.

The Linker can not resolve the constructor for myclass1. This could mean no definition or you did not Link the library. Make sure you have a function body for the constructor and that the given library file is linked.

In main CMakeLists.txt:
target_link_libraries(${PROJECT_NAME} Qt5::Widgets
lib2 lib1)
Or in lib2 CMakeList.txt:
target_link_libraries(${PROJECT_NAME} lib1)
Or in main.cpp:
auto m1 = new MyClass1;

Related

net-snmp flags in CMAKE

Before anything, I just want to say I am very new to CMAKE, almost never used it but now forced to...
I am trying to include snmp features in a previous project, using Net-SNMP library. But first, I wrote a minimalistic code just to test my functions. According to the library tutorial, this is how I must compile the code:
First, I must create object file:
gcc -I. `net-snmp-config --cflags` -c -o tfsnmpset.o tfsnmpset.c
Then, I must generate the executable:
gcc -o tfsnmpset tfsnmpset.o `net-snmp-config --libs`
By doing this, the program compiles perfectly and everything is fine.
Now the project in which I want to incorporate that piece of code uses CMakeLists.txt to generate its makefile.
My question is, how do I include the following flags in my CMakeLists.txt?
When creating object files: `net-snmp-config --cflags`
When generating executable: `net-snmp-config --libs`
I actually tried to build a library out of my code that uses Net-SNMP that I could just use in my main project:
cmake_minimum_required(VERSION 3.12)
project(snmp_daemon C)
set(CMAKE_C_STANDARD 99)
SET(CMAKE_C_COMPILER /usr/bin/gcc)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I. `net-snmp-config --cflags`" )
add_library(tfsnmpset tfsnmp.c tfsnmp.h)
add_executable(snmp_daemon main.c ./tfsnmp.h)
target_link_libraries(snmp_daemon tfsnmpset)
The errors:
/media/user/xtra/apps/clion-2018.2.1/bin/cmake/linux/bin/cmake --build /home/fabrice/projects/snmp-daemon/cmake-build-debug --target snmp_daemon -- -j 2
[ 25%] Linking C static library libtfsnmpset.a
[ 50%] Built target tfsnmpset
[ 75%] Linking C executable snmp_daemon
libtfsnmpset.a(tfsnmp.c.o): In function `tfsnmpset':
/home/user/projects/snmp-daemon/tfsnmp.c:121: undefined reference to `snmp_parse_args'
/home/user/projects/snmp-daemon/tfsnmp.c:147: undefined reference to `snmp_get_do_debugging'
/home/user/projects/snmp-daemon/tfsnmp.c:147: undefined reference to `debugmsgtoken'
/home/user/projects/snmp-daemon/tfsnmp.c:147: undefined reference to `debugmsg'
/home/user/projects/snmp-daemon/tfsnmp.c:147: undefined reference to `debugmsgtoken'
/home/user/projects/snmp-daemon/tfsnmp.c:147: undefined reference to `debugmsg'
/home/user/projects/snmp-daemon/tfsnmp.c:194: undefined reference to `snmp_open'
/home/user/projects/snmp-daemon/tfsnmp.c:199: undefined reference to `snmp_sess_perror'
/home/user/projects/snmp-daemon/tfsnmp.c:207: undefined reference to `snmp_pdu_create'
/home/user/projects/snmp-daemon/tfsnmp.c:210: undefined reference to `snmp_parse_oid'
/home/user/projects/snmp-daemon/tfsnmp.c:211: undefined reference to `snmp_perror'
/home/user/projects/snmp-daemon/tfsnmp.c:214: undefined reference to `snmp_add_var'
/home/user/projects/snmp-daemon/tfsnmp.c:216: undefined reference to `snmp_perror'
/home/user/projects/snmp-daemon/tfsnmp.c:222: undefined reference to `snmp_close'
/home/user/projects/snmp-daemon/tfsnmp.c:230: undefined reference to `snmp_synch_response'
/home/user/projects/snmp-daemon/tfsnmp.c:236: undefined reference to `print_variable'
/home/user/projects/snmp-daemon/tfsnmp.c:239: undefined reference to `snmp_errstring'
/home/user/projects/snmp-daemon/tfsnmp.c:247: undefined reference to `fprint_objid'
/home/user/projects/snmp-daemon/tfsnmp.c:257: undefined reference to `snmp_sess_perror'
/home/user/projects/snmp-daemon/tfsnmp.c:262: undefined reference to `snmp_free_pdu'
/home/user/projects/snmp-daemon/tfsnmp.c:263: undefined reference to `snmp_close'
collect2: error: ld returned 1 exit status
CMakeFiles/snmp_daemon.dir/build.make:84: recipe for target 'snmp_daemon' failed
make[3]: *** [snmp_daemon] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/snmp_daemon.dir/all' failed
make[2]: *** [CMakeFiles/snmp_daemon.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/snmp_daemon.dir/rule' failed
make[1]: *** [CMakeFiles/snmp_daemon.dir/rule] Error 2
Makefile:118: recipe for target 'snmp_daemon' failed
make: *** [snmp_daemon] Error 2
You should link to snmp libraries
find_library(NETSNMPAGENT "netsnmpagent")
find_library(NETSNMPMIBS "netsnmpmibs")
find_library(NETSNMP "netsnmp")
target_link_libraries(snmp_daemon tfsnmpset ${NETSNMPAGENT} ${NETSNMPMIBS} ${NETSNMP})

Building Artery on Ubuntu fails

When I try to build Artery on Ubuntu 16.04. the following happens after invoking
$ cmake --build /home/hardt/Veins/artery-master/build
[ 23%] Linking CXX shared library libartery.so
/usr/bin/ld: /usr/local/lib/libvanetza_btp.a(data_request.cpp.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared
object; recompile with -fPIC
/usr/local/lib/libvanetza_btp.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
src/artery/CMakeFiles/artery.dir/build.make:954: recipe for target'src/artery/libartery.so' failed
make[2]: *** [src/artery/libartery.so] Error 1
CMakeFiles/Makefile2:229: recipe for target 'src/artery/CMakeFiles/artery.dir/all' failed
make[1]: *** [src/artery/CMakeFiles/artery.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
As I am not all that familiar with building source on Unix systems I am not quite sure how to deal with this error.
I am using cmake version 3.9.3, boost 1.65.1 and crypto++ 5.6.4 the geographiclib built without error as well.
As one can read in the comments, the problem is solved.
The path to the vanetza directory VANETZA_DIR in the cmake configuration was wrong. Correcting this via ccmake solved the problem.

fglut/libfglut.a(freeglut_state.o): undefined reference to symbol 'XGetWindowAttributes'

I am compiling a C++ program using make, this is the error i'm getting.
/usr/bin/ld: fglut/libfglut.a(freeglut_state.o): undefined reference to symbol 'XGetWindowAttributes'
//usr/lib/i386-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Makefile:55: recipe for target 'morphlines' failed
make: *** [morphlines] Error 1
I'm beginner
/usr/bin/ld: fglut/libfglut.a(freeglut_state.o):
This tells me you link libfglut statically (*.a is just an archive of object files). When you do this, you must link all dependencies as well, because with the object files from the static library actually compiled into your program, your program will depend on them.
Either link libfglut dynamically (this is the default with the GNU toolchain), so your program will depend on libfglut.so which will itself depend on libX11.so -- or add -lX11 after -lfglut on the command line of your final linking step. You might need -Wl,-Bdynamic before -lX11 to switch the linker back to dynamic linking.
If this doesn't directly solve your problem, I suggest you edit your question to include the relevant parts of the Makefile you're using.

libfreefare mingw32-make error on windows

I am on windows 8 64 bits.
I have installed successfully libnfc, and I am able to use the .exe examples that are in libnfc\utils (like nfc-mfclassic.exe; nfc-list.exe ...)
I would like to manipulate Mifare cards and use NDEF Format, so I am trying to install libfreefare on Windows.
I have followed a tutorial on libnfc.org showing how to have libfreefare on Windows and It worked fine.
And I used cmake-gui to generate build file
Coming now to the step of mingw32-make, I have this error :
[ 55%] Building C object libfreefare/CMakeFiles/freefare.dir/tlv.c.obj
Linking C shared library libfreefare.dll
CMakeFiles\freefare.dir/objects.a(mifare_desfire.c.obj):mifare_desfire.c:(.text+0x64f): undefined reference to `RAND_bytes'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x1219): undefined reference to `DES_ecb_encrypt'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x1242): undefined reference to `DES_ecb_encrypt'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x1280): undefined reference to `DES_ecb_encrypt'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x12a9): undefined reference to `DES_ecb_encrypt'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x12d0): undefined reference to `DES_ecb_encrypt'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x12f9): more undefined references to `DES_ecb_encrypt' follow
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x1489): undefined reference to `AES_set_encrypt_key'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x14a8): undefined reference to `AES_encrypt'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x14c7): undefined reference to `AES_set_decrypt_key'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_crypto.c.obj):mifare_desfire_crypto.c:(.text+0x14e6): undefined reference to `AES_decrypt'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_key.c.obj):mifare_desfire_key.c:(.text+0x17): undefined reference to `DES_set_key'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_key.c.obj):mifare_desfire_key.c:(.text+0x32): undefined reference to `DES_set_key'
CMakeFiles\freefare.dir/objects.a(mifare_desfire_key.c.obj):mifare_desfire_key.c:(.text+0x58): undefined reference to `DES_set_key'
CMakeFiles\freefare.dir/objects.a(mifare_ultralight.c.obj):mifare_ultralight.c:(.text+0x7a3): undefined reference to `DES_random_key'
collect2.exe: error: ld returned 1 exit status
libfreefare\CMakeFiles\freefare.dir\build.make:351: recipe for target 'libfreefare/libfreefare.dll' failed
mingw32-make[2]: *** [libfreefare/libfreefare.dll] Error 1
CMakeFiles\Makefile2:74: recipe for target 'libfreefare/CMakeFiles/freefare.dir/all' failed
mingw32-make[1]: *** [libfreefare/CMakeFiles/freefare.dir/all] Error 2
Makefile:116: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Thanks for your help !!
I've found a solution :
At the root of the folder : libfreefare-0.4.0 there is CMakeLists.txt
I replaced this line :
set(LIBS ${LIBS} ${LIBNFC_LIBRARIES} ${OPENSSL_LIBRARIES})
by this one
set(LIBS ${LIBS} ${LIBNFC_LIBRARIES} D:/tools/OpenSSL-Win32/libeay32.dll D:/tools/OpenSSL-Win32/libssl32.dll)
And it worked fine ! Thanks #MichaelRoland for the tips on libcrypto equivalent

hidden symbol `__sync_fetch_and_add_4' is referenced by DSO

I am trying to Cross Compile opencv on linux platform (ARM 8148).
I am getting following errors:
warning: libQtTest.so.4, needed by ../../lib/libopencv_highgui.so.3.0.0, not found (try using -rpath or -rpath-link)
/opt/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/ld: warning: libQtCore.so.4, needed by ../../lib/libopencv_highgui.so.3.0.0, not found (try using -rpath or -rpath-link)
/opt/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/ld: ../../bin/opencv_perf_core: hidden symbol `__sync_fetch_and_add_4' in /opt/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/libgcc.a(linux-atomic.o) is referenced by DSO
/opt/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_perf_core] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] Error 2
I am not able to understand this error and on net also not getting proper reference for that .Can anyone help me in understanding this error .
__sync_fetch_and_add_x* is normally inlined by gcc, and compiles to inline code that performs an automic operation on a memory word.
You will find them documented here:
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gcc/_005f_005fsync-Builtins.html
As described on this page, if the compiler emitted a function call, instead of a generating inline assembly, it means that this atomic operation is not supported on the target platform.

Resources