gpt4 book ai didi

linux - 与 gcc -lnetsnmp 链接时 undefined reference

转载 作者:太空狗 更新时间:2023-10-29 12:00:52 28 4
gpt4 key购买 nike

我正在尝试在需要 snmp 库的树莓派上编译和运行我的代码。但是,我收到链接错误,例如“对‘EVP_DigestUpdate’的 undefined reference ”。

过去几天我一直在研究这个问题,发现这通常发生在 snmp 找不到 ssl 库 crypto 时,例如,如果您在链接对象时没有在 -lsnmp 之前指定 -lcrypto ,或者当链接器无法找到 -lcrypto.

但是,在我的树莓派上,我在/usr/lib/gcc/arm-linux-gnueabihf 中拥有所有必需的库。

这就是我安装库的方式:

apt-get install libssl-dev libsnmp-dev libsnmp-base libsnmp30

我用这个命令生成 trap.o:

gcc -c -g -pedantic -Wall -Wshadow -I/home/pi/tests/h -I/usr/include trap.c -o trap.o

我用这个命令链接 trap.o 库:

gcc -static -g -pedantic -Wall -Wshadow trap.o -o trap -L/usr/lib/arm-linux-gnueabihf/lib -lcrypto -lpthread -lnetsnmpagent -lnetsnmpmibs -lnetsnmphelpers -lnetsnmp

然后我得到这个错误:

    gcc -static -g -pedantic -Wall -Wshadow trap.o -o trap -L/usr/lib/arm-linux-gnueabihf/lib -lcrypto -lpthread -lnetsnmpagent -lnetsnmpmibs -lnetsnmphelpers -lnetsnmp
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_gid':
(.text+0x1144): warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_gid':
(.text+0x1154): warning: Using 'endgrent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_uid':
(.text+0x10d8): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_uid':
(.text+0x10e8): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_getaddrinfo':
(.text+0x4a4): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_gethostbyaddr':
(.text+0x9f8): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
trap.o: In function `parse_address':
/home/pi/tests/trap.c:56: warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/crt1.o: In function `_start':
/build/glibc-6f8a9a/glibc-2.19/csu/../ports/sysdeps/arm/start.S:119: undefined reference to `main'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_random':
(.text+0x168): undefined reference to `RAND_bytes'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_generate_keyed_hash':
(.text+0x290): undefined reference to `EVP_md5'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_generate_keyed_hash':
(.text+0x2b4): undefined reference to `HMAC'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_generate_keyed_hash':
(.text+0x33c): undefined reference to `EVP_sha1'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x424): undefined reference to `EVP_sha1'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x42c): undefined reference to `EVP_MD_CTX_create'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x438): undefined reference to `EVP_DigestInit'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x448): undefined reference to `EVP_DigestUpdate'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x458): undefined reference to `EVP_DigestFinal'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x468): undefined reference to `EVP_MD_CTX_destroy'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x488): undefined reference to `EVP_md5'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x86c): undefined reference to `DES_key_sched'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x8a0): undefined reference to `DES_ncbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x8c4): undefined reference to `DES_ncbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x90c): undefined reference to `AES_set_encrypt_key'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x948): undefined reference to `AES_cfb128_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0xa74): undefined reference to `DES_key_sched'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0xaac): undefined reference to `DES_ncbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xc78): undefined reference to `AES_set_encrypt_key'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xcb0): undefined reference to `AES_cfb128_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xd9c): undefined reference to `DES_key_sched'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xdd0): undefined reference to `DES_cbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x74): undefined reference to `EVP_MD_CTX_create'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x98): undefined reference to `EVP_md5'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0xa4): undefined reference to `EVP_DigestInit'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0xe8): undefined reference to `EVP_DigestUpdate'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x10c): undefined reference to `EVP_DigestFinal'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x138): undefined reference to `EVP_MD_CTX_destroy'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x178): undefined reference to `EVP_sha1'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x184): undefined reference to `EVP_DigestInit'
collect2: error: ld returned 1 exit status

会不会是因为我安装的 snmp 库是使用与我的环境中不同的 openssl 版本构建的 snmp 库引起的?我的 libssl-dev 和 libsnmp-dev 版本如下:

dpkg -l | grep libssl-dev:

ii  libssl-dev:armhf                      1.0.1k-3+deb8u5                           armhf        Secure Sockets Layer toolkit - development files

dpkg -l | grep libsnmp-dev:

ii  libsnmp-dev                           5.7.2.1+dfsg-1                            armhf        SNMP (Simple Network Management Protocol) development files

如有任何帮助,我们将不胜感激,

谢谢

最佳答案

所有 undefined reference 都在 libnetsnmp 中并且它们都是未定义的对 libcypto 中定义的函数的引用。发生这种情况是因为 -lcrypto在链接中的 -lnetsnmp 之前。

链接序列中,必须出现需要符号定义的文件在提供定义的那些之前,因为默认情况下链接器将仅搜索库以查找它具有的符号定义观察到在早期文件中被引用,但未定义。 (你似乎错误地了解了这条规则,错误的方式)。来自 man ld:

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.

所以在 -lnetsnmp 之后链接 -lcrypto

关于linux - 与 gcc -lnetsnmp 链接时 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37170214/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com