Gilbert
Gilbert

Reputation: 921

CMake: Building a Static Binary

I'm trying to build a static binary with CMake.

I've looked around and found a number of non descriptive solutions(e.g add the linker flag "-static"), but I'm not sure how to test them.

How do I know if a certain library (e.g Threading) has been statically built into my binary?

Upvotes: 3

Views: 2152

Answers (3)

Randall Cook
Randall Cook

Reputation: 6776

As trenki says, use ldd to get the list of dynamically linked libraries that your executable depends on, and check it to be sure that the code you wanted statically linked is not there.

You can also use the nm and strings commands to get a dump of the programmatic symbols and human-readable text, respectively, in your executable. Use grep to filter that output for identifiers in your static library that you reference in your code, like function names.

Lastly, you can use the objdump command to get a disassembly listing of your program. Static library functions should appear in it.

The first approach, coupled with an executable that compiles, links, and runs well enough to demonstrate the functionality that the static library helps provide is probably a sufficient test. Use the second or third approaches only if you want to dig deep.

Upvotes: 3

trenki
trenki

Reputation: 7363

Under Linux you can also use the ldd command which shows which libraries your binary links to dynamically.

Upvotes: 1

ezod
ezod

Reputation: 7411

Not CMake-related, but if you're on a UNIX system the file utility can at least tell you whether your binary is using shared libs or not.

Upvotes: 1

Related Questions