net-snmp flags in CMAKE - gcc

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})

Related

X11 linking issue potentially musl libc related

I am running Alpine Linux with musl libc attempting to install-
https://github.com/patrickhaller/no-wm/
with-
make install
I have musl-dev and libx11-dev installed.
libx11-dev puts libs in /usr/lib not /usr/X11/lib. see-
https://pkgs.alpinelinux.org/contents?branch=edge&name=libx11-dev&arch=x86&repo=main
So I changed the Makefile line to-
X11LIB = -lX11 -L/usr/lib/
I confirmed libX11.so is at that directory location.
Yet my install still fails with this output-
$ make install
gcc -O2 -Wall -std=c99 -pedantic -lX11 -L/usr/lib/ x-alt-tab-mru.c -o x-alt-tab-mru
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: /tmp/cckobJdo.o: in function `x_alt_tab':
x-alt-tab-mru.c:(.text+0x70): undefined reference to `XGetWMHints'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text+0x84): undefined reference to `XGetWindowAttributes'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text+0xec): undefined reference to `XLowerWindow'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text+0xf8): undefined reference to `XRaiseWindow'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text+0x10c): undefined reference to `XSetInputFocus'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text+0x11c): undefined reference to `XRestackWindows'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text+0x128): undefined reference to `XSync'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: /tmp/cckobJdo.o: in function `main':
x-alt-tab-mru.c:(.text.startup+0x34): undefined reference to `XOpenDisplay'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text.startup+0x44): undefined reference to `XSync'
/usr/lib/gcc/aarch64-alpine-linux-musl/10.2.0/../../../../aarch64-alpine-linux-musl/bin/ld: x-alt-tab-mru.c:(.text.startup+0x70): undefined reference to `XQueryTree'
collect2: error: ld returned 1 exit status
make: *** [Makefile:19: x-alt-tab-mru] Error 1
I've confirmed the headers in that x-alt-tab-mru.c file are present in the correct location and were included with libx11-dev.
What can I do next to troubleshoot this and get it to compile? I've chased down everything I could think of..
The order of arguments on the link command line is significant. -L options apply only to searching for libraries designated later on the command line, and, at least for static linking, undefined symbols in one object among those being linked are resolved only against other objects designated later on the command line. Behavior may (or may not) vary a bit when linking shared libraries, but to be safe, you should always order the objects to be linked (source files, object files, and libraries) according to their dependencies.
In particular, then,
in the unlikely event that you need -L/usr/lib at all, it should come before -lX11, and
the -lX11 option should appear after x-alt-tab-mru.c in the link command.

Undefined reference to. In cmake: lib to lib [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 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;

Error building Quantlib involving Boost (Ubuntu)

I am attempting to build QuantLib 1.9 on my machine (Xubuntu 16.04), and I got Boost via "sudo apt-get...."
QuantLib compiles fine until the following sequence of events:
make[1]: Entering directory '/home/Downloads/packages/QuantLib-1.9/test-suite'
There are two calls to libtool, the first looks like this:
/bin/bash ../libtool --tag=CXX --mode=link g++ -g -O2 -o quantlib-test-suite
Followed by nearly 100 *.o files, and terminated by this string:
libUnitMain.la ../ql/libQuantLib.la -lboost_unit_test_framework
There is a second call to libtool after that one:
libtool: link: g++ -g -O2 -o .libs/quantlib-test-suite
Also followed by 100 *.o files, and terminated by this string:
./.libs/libUnitMain.a ../ql/.libs/libQuantLib.so -lboost_unit_test_framework
Here is where the error crops up, and the build then fails:
quantlibtestsuite.o: In function `make_test_case':
/usr/include/boost/test/unit_test_suite_impl.hpp:255: undefined reference to boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)'
/usr/include/boost/test/unit_test_suite_impl.hpp:255: undefined reference to `boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)'
americanoption.o: In function `make_test_case':
/usr/include/boost/test/unit_test_suite_impl.hpp:255: undefined reference to `boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)'
/usr/include/boost/test/unit_test_suite_impl.hpp:255: undefined reference to `boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)'
americanoption.o: In function `boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>)':
/usr/include/boost/test/unit_test_suite_impl.hpp:255: undefined reference to `boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)'
amortizingbond.o:/usr/include/boost/test/unit_test_suite_impl.hpp:255: more undefined references to `boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)' follow
collect2: error: ld returned 1 exit status
Makefile:1075: recipe for target 'quantlib-test-suite' failed
make[1]: *** [quantlib-test-suite] Error 1
make[1]: Leaving directory '/home/Downloads/packages/QuantLib-1.9/test-suite'
Makefile:601: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1
It's possible that you're compiling with a version of Boost and you're trying to link with another (you're saying that you got Boost via apt-get, which installs into /usr, but then you pass to ./configure a path in your home directory). This would explain why the linker is looking for a method in boost::unit_test::ut_detail that's not in the library.
If you want to use the version of Boost that came with apt-get, removing the boost paths from the call to ./configure should make it work. If you want to use the one you have installed locally, you should first check that you actually have the compiled libraries in /home/EmilioW/Downloads/boost_1_62_0/libs. If they are in fact there, and if you don't need Boost for something else, you can use apt-get to uninstall the packaged version. If you need both installed, you'll have to check the order in which the compiler and linker list the -I and -L paths and ensure that the ones you want come first...

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

Point Cloud Library(PCL) compile issues UBUNTU 12.04 - undefined reference to symbol '_ZN5boost6system15system_categoryEv'

I'm getting this error when i try to run "pcl pcl_write.cpp". The cpp file is just a test program to check if pcl is working. (Also, alias pcl = ~/.compile.sh ).
arun#Arun:~/pcl_projects/testing_pcl$ pcl pcl_write.cpp
/usr/bin/ld: /tmp/ccQOUJZ2.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/bin/ld: note: '_ZN5boost6system15system_categoryEv' is defined in DSO
/usr/lib/libboost_system.so.1.46.1 so try adding it to the linker command line
/usr/lib/libboost_system.so.1.46.1: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
NOTE:
.compile.sh contains the below line :
g++ -ggdb -I/usr/lib -I/usr/include/pcl-1.7 -I/usr/include/eigen3 -o `basename $1 .cpp`
$1 `pkg-config --libs pcl_apps-1.7 pcl_common-1.7 pcl_features-1.7
pcl_filters-1.7 pcl_geometry-1.7 pcl_io-1.7 pcl_kdtree-1.7 pcl_keypoints-1.7
pcl_registration-1.7 pcl_sample_consensus-1.7 pcl_search-1.7 pcl_segmentation-1.7
pcl_surface-1.7 pcl_tracking-1.7 pcl_visualization-1.7 flann`;
Note : I'm able to compile using CMAKE!
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(pcl_write)
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (pcl_write pcl_write.cpp)
target_link_libraries (pcl_write ${PCL_LIBRARIES})

Resources