clang seems to ignore debug information from object files in static libraries

62 views Asked by At

When I link my final binary directly from the object files, as in

clang -o binary build/lib/*.o build/bin/*.o

and my binary crashes, I have line numbers everywhere, because all my object files were build with -g, so I should get full debug info.

But when I combine some of the object files to a static library, using

ar -q lib.a build/lib/*.o

and then link the final binary with

clang -o binary lib.a build/bin/*.o

and I get a crash, I only have line numbers for the object files but no line numbers for any object file from within the static library.

What am I doing wrong?

Update

I figured out that it has something to do with symlinks. The example commands I provided were simplified, the real link command uses absolute paths as in

clang -o binary /symlink/to/lib.a /fullpath/to/build/bin/*.o

The problem is the symlink path. When I instead use the absolute path to lib.a that contains no symlinks, then everything works as expected and I get line numbers. Also when I'm within the folder of lib.a and use no path at all, just the filename, it works as expected. Only when addressing the library via a symlink it fails.

BTW, the platform is macOS Sonoma, the clang version is the one that came with Xcode 14.3.1 (clang-1403.0.22.14.1).

1

There are 1 answers

3
Employed Russian On

What am I doing wrong?

You didn't say what platform you are on, or which linker you are using.

For UNIX linkers other than LLD, this command:

clang -o binary lib.a build/bin/*.o

will result in no objects from lib.a actually being used.

Normally this would result in unresolved symbols, but (depending on which symbols are defined in lib.a) the symbols may come from the libc system library, and in that case you may in fact get the "no line numbers".

The command you want is:

clang -o binary build/bin/*.o lib.a

The order of command line arguments on the link line matters.