Checking If a Binary Compiled with "-Static"

checking if a binary compiled with -static

ldd /path/to/binary should not list any shared libraries if the binary is statically compiled.

CMake: How can I check if a STATIC library has been compiled with -stdlib=libc++?

There is no need to check this for static libraries. Static libraries are just archives with object files and they have no runtime dependencies. They are linked directly to your final binary file.

You should read more about how linker works:

  • Stack overslow answer
  • Wikipedia
  • Youtube video

If you compile your final binary without libc++ and do not have any "undefined reference" error - everything is fine. Otherwise if the undefined symbols are functions from lib++, your static libraries were compiled with libc++ support.

The only way you can check symbole before linkage stage is checks symbols, which static libraries contain. See this answer about how to do this

Get list of static libraries used in an executable

ldd <exe filename> shows dynamically linked libraries

nm <exe filename> shows the symbols in the file.

To see which symbols come from static libraries requires running nm against those libraries to get a list of the symbols (functions, etc.) in them, then comparing them to what your list of symbols from nm <exe filename>.

You compare lists with the comm command. See man comm for details.

This was taken from this forum here.

In C/C++, how do I determine if a library is statically linked or not

This does not need to be done as runtime. The common solution is to use a weak symbol. This is a symbol in a library that's used when there is no "normal" symbol to link against. Your base library provides the weak symbols, your GUI library optionally provides replacements, and the static linker figures it out.

Is static_assert compiled into the binary file

Is static_assert compiled into the binary file

No.

As my understanding, static_assert won't be executed at runtime, right?

Right.

the compiler will build the static_assert into the binary file

No.

the static_assert will be totally ignored, just like a comment?

No, it's not a comment - the expression is checked, and if the expression is false, then a message is shown. When the expression is not a constant (can't be computed at compile time) then also a message is shown.

how to check if binary is runnable

Perhaps include an extra unit-test that is directly runnable, just a "hello world" or return EXIT_SUCCESS;, and if it fails, skip all the other plugin tests of that architecture?

Fun fact: on Linux at least, a shared library (ELF shared object) can have an entry point and be executable. (That's how PIE executables are made; what used to be a silly compiler / linker trick is now the default.) IDK if it would be useful to bake a main into one of your existing plugin tests.



Related Topics



Leave a reply



Submit