我在 Linux 上编译使用 getaddrinfo_a
的最小程序时遇到链接器错误。有问题的程序
#define _GNU_SOURCE
#include <stdio.h>
#include <netdb.h>
int main(int argc, char **argv) {
int err;
err = getaddrinfo_a(0, NULL, 0, NULL);
}
编译器输出:
$ cc -lanl minimal.c
/tmp/cc89BuFU.o: In function `main':
minimal.c:(.text+0x24): undefined reference to `getaddrinfo_a'
collect2: error: ld returned 1 exit status
$ cc --version
cc (Ubuntu 4.9.2-10ubuntu13) 4.9.2
最佳答案
您使用命令的方式错误。使用
cc minimal.c -lanl
-lanl 应该出现在文件名之后而不是之前。
gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
-l It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
根据标准,库的顺序也很重要。链接器未检查先前指定的库中的符号。 Ref
关于c - 对 `getaddrinfo_a' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37268361/