难以针对静态库进行编译 [英] trouble compiling against static library

查看:115
本文介绍了难以针对静态库进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用gccar创建了一个静态库libstuff.a.我已经在/custom/lib/dir/中安装了该库,并在/custom/include/dir/中安装了头文件.当我尝试针对归档文件编译另一个应用程序时,遇到了问题.

I have created a static library, libstuff.a, with gcc and ar. I've installed the library in /custom/lib/dir/ and the header files in /custom/include/dir/. When I try to compile another application against the archive, I am running into issues.

我要编译的应用程序基本上使用gcc -L/custom/lib/dir/ -I/custom/include/dir/ -lstuff.

The application I am trying to compile basically uses gcc -L/custom/lib/dir/ -I/custom/include/dir/ -lstuff.

当我尝试编译时,我在stuff.h中声明并在stuff.c中定义的函数出错.

When I attempt to compile, I get error about a function I declare in stuff.h and define in stuff.c.

main.c:51: undefined reference to `stuff_init'
collect2: error: ld returned 1 exit status

如果我从/custom/lib/dir/中删除libstuff.a,则编译器会抱怨/usr/bin/ld: cannot find -lstuff.如果我把它放回去,它不会抱怨.因此,它可以正确找到存档.

If I remove libstuff.a from /custom/lib/dir/ the compiler complains /usr/bin/ld: cannot find -lstuff. If I put it back, it doen't complain. So it's finding the archive correctly.

如果我从main.c中删除代码行#include "stuff.h",则gcc会抱怨stuff_init未定义,因此它可以正确找到标头.

If I remove the line of code #include "stuff.h" from main.c gcc complains about stuff_init being undefined, so it's finding the headers correctly.

如果我运行nm libstuff.a,则输出包括0000000000000000 T stuff_init.因此,该功能在存档文件中定义.

If I run nm libstuff.a, the output includes 0000000000000000 T stuff_init. So the function is defined in the archive file.

那我在做什么错了?

推荐答案

您没有显示确切的链接行,但是有可能您应该(但不要)在目标文件后列出库:

You don't show your exact linking line, but the chances are you should (but don't) list the libraries after the object files:

gcc -o program main.o -L/custom/lib/dir -lstuff

如果库之外有多个对象文件,则在所有库之前列出所有对象文件. -L选项可能在使用给定目录中的库的-llib选项之前的任何位置.

If there's more than one object file outside of the libraries, then list them all before any of the libraries. The -L options may go anywhere before the -llib option that uses a library from the given directory.

这篇关于难以针对静态库进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆