Ben Farmer
Ben Farmer

Reputation: 2864

How does cmake add_library decide on visibility for symbols?

I am building a shared library with the CMake add_library command as follows:

add_library(mylibname OPTION SHARED SOURCES ${source_files} HEADERS ${header_files})

When I inspect this library with 'nm', I find that some symbols are marked globally visible ("T"), and others are only internally visible to the library ("t"). My question is, why? What determines the symbol visibility when I haven't done anything in particular to control it?

I ask because it just so happens that when I link this library to another part of the project I get undefined reference errors, and it is because the symbols I need are apparently only internally visibile to the library for some reason. So I want to change a "t" to a "T" somehow, but, since I don't know what causes it to be a "t" in the first place, I figure that I would like to know that first :).

The symbol in question happens to be a specialisation of a template function, so perhaps the default visibility has to do with templates or something?

Upvotes: 3

Views: 3014

Answers (1)

Craig Scott
Craig Scott

Reputation: 10167

Symbol visibility is controlled on a per-target basis with the <LANG>_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN target properties. The defaults for these are taken from the variables CMAKE_<LANG>_VISIBILITY_PRESET and CMAKE_VISIBILITY_INLINES_HIDDEN respectively. If these two variables have been set, that may explain how symbols you are expecting to be exported are somehow being made internal.

Note also that while symbols are exported by default with some compilers (e.g. GCC, clang), on Windows with Visual Studio they are not exported by default.

Upvotes: 1

Related Questions