c - 对 `getaddrinfo_a' 的 undefined reference

标签 c linux networking

我在 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/

相关文章:

c# - DllImport 和 char 编码(marshal)处理

objective-c - 向 Minecraft 服务器发送数据包 : Objective c

networking - 使用winsock扫描局域网游戏服务器

linux - 如何使用多个分隔符从文件中删除特定列

java - 如何将数据库共享给不同的电脑以用于java桌面应用程序?

C/UNIX 从输入读取(受字符计数和超时限制)

c - 将 EOF 写入管道

c - 分配给位域时的 GCC 转换警告

linux - 在 Chromebook 上编译内核模块

linux - 如何用包含 Linux 文件中另一行(新目录路径)的变量替换一行(目录路径)?