How to list dependencies of c/c++ static library?
Asked Answered
C

2

20

For a static library (.a file), how to list the module-level dependencies of it?

I know for a shared library (.so), we can use objdump or readelf to do this:

objdump -p test.so

or

readelf -d test.so

I can get something like

NEEDED libOne.so

NEEDED libc.so.6

But for a static library, I can only get the dependencies in symbol-level, for example, by running

objdump -T test.a

I will get some thing like:

00000000 DF UND 00000000 QByteArray::mid(int, int) const

00000000 DF UND 00000000 QUrl::fromEncoded(QByteArray const&)

00000000 DF UND 00000000 QFileInfo::fileName() const

But I need the information in module-level, does anyone know how to get that information?

Claudineclaudio answered 24/11, 2016 at 15:0 Comment(0)
M
26

A static library have no such list of dependencies.

A static library is nothing more than an archive of object files. And as object files doesn't know what libraries they depend on, neither can a static library.

Mingrelian answered 24/11, 2016 at 15:6 Comment(6)
I'd say the reason for this is that static library is not linked. Therefore, no one in the build chain so far was responsible for adding such information to it.Tyronetyrosinase
@Jan Smrčina @Some programmer dude But how can a static library be generated without knowing that information? For example, library A uses a function defined in another library B. If we don't add something like #include "B.h", can A be generated? In that case, can we say A depends on B?Claudineclaudio
@Claudineclaudio Just like any other object file. The ar command used to create static libraries is a plain and very simple ARchiver. For static libraries it's up to you to keep track of the dependencies, and link with them explicitly. It's nothing that the linker is capable of, all it does is basically to extract the object files and link with them.Mingrelian
@Some programmer dude So does it mean for the case I mentioned above, if I don't specify #include "B.h", static library A can still be created, but if I want to use A to generated an executable, I have to link to B somewhere?Claudineclaudio
@Claudineclaudio That's correct. In fact, the header files have nothing to do with it.Mingrelian
It is 2024, and in binutils version 2.40, the ar tool can record dependencies in any .a file. See the documantation for ar, in particular the l (l for Lima) operation code modifier.Constanceconstancia
N
3

@Some programmer dude is right, but what you can do is build a simple program using this library statically and then check with ldd -v what are the dependencies.

Nauru answered 28/8, 2020 at 14:21 Comment(1)
This is helpful. The hard part though is building it because you would have to know all the dependencies to specify them at build time, so you already have this information, or you would be in the same predicament. 'ldd -v' would be useful to inspect applications that someone else built, but I'd say that if you have something built statically in-hand, your fastest path is the ask the person who built it what the dependencies are.Gulledge

© 2022 - 2024 — McMap. All rights reserved.