CMAKE中的net-snmp标志

时间:2018-11-21 10:18:41

标签: gcc cmake static-libraries compiler-flags

在开始之前,我只想说我对CMAKE很陌生,几乎从未使用过它,但是现在被迫...

我正在尝试使用Net-SNMP库在以前的项目中包含snmp功能。但是首先,我编写了一个简单的代码来测试我的功能。根据库教程,这就是我必须编译代码的方式:

首先,我必须创建目标文件:

gcc -I. `net-snmp-config --cflags`   -c -o tfsnmpset.o tfsnmpset.c

然后,我必须生成可执行文件:

gcc -o tfsnmpset tfsnmpset.o `net-snmp-config --libs`

这样做,程序可以完美编译,一切都很好。 现在我要在其中合并代码的项目使用CMakeLists.txt生成其makefile。

我的问题是,如何在我的CMakeLists.txt中包括以下标志?

When creating object files: `net-snmp-config --cflags` 
When generating executable: `net-snmp-config --libs`

我实际上试图用我可以在主项目中使用的Net-SNMP的代码构建一个库:

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)

错误:

/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

1 个答案:

答案 0 :(得分:0)

您应该链接到snmp库

find_library(NETSNMPAGENT "netsnmpagent")

find_library(NETSNMPMIBS "netsnmpmibs")

find_library(NETSNMP "netsnmp")

target_link_libraries(snmp_daemon tfsnmpset ${NETSNMPAGENT} ${NETSNMPMIBS} ${NETSNMP})