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.

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

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

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.

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.

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

So what am I doing wrong?

有帮助吗?

解决方案

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

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top