c - apue相关代码编译时链接问题

标签 c ld

我正在尝试编译与“UNIX® 环境中的高级编程”一书相关的代码

当我尝试像这样编译测试文件时:

$ gcc -L ../lib/ -l apue foo.c

我得到:

/tmp/cccXkUae.o: In function `main':
foo.c:(.text+0x2b): undefined reference to `err_sys'
...
collect2: error: ld returned 1 exit status

但是,函数似乎是在库中定义的...

$ grep err_sys ../lib/libapue.a 
Binary file ../lib/libapue.a matches

最终,编译没有错误:

$ gcc foo.c ../lib/error.c

只是想了解我做错了什么。

最佳答案

可用的源代码 在 http://www.apuebook.com/src.3e.tar.gz为您(构建后)在 include/ 中获取名为 apue.h 的 header ,在 lib< 中获取名为 libapue.a 的静态库。因此,要针对 libapue 进行编译和链接,您需要:

gcc -I $apue_root/include -L $apue_root/lib your_file.c -lapue

关于c - apue相关代码编译时链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39334261/

相关文章:

c++ - 您将如何列出已编译库中包含的可用函数等?

c - 如何使用 gcc 编译带有源代码文件的库存档?

gcc - 带有自定义 gcc 安装的 LIBRARY_PATH 的优先级

c - 错误 : expected declaration specifiers or '...' before XXX (all kinds of parameters)

c - 宏内容的长度有限制吗?

c - 为什么 ] 不显示在 C 中?

html - 如何在 Code::block IDE 中将 HTML 嵌入到 C 代码中?

c - Recv() 没有得到 EOF?

linux - i386 输入文件架构与 i386 不兼容 :x86-64

c - 共享库文件大小