3

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?

Ben Farmer
  • 2,387
  • 1
  • 25
  • 44
  • 1
    It's possible you are looking at 'static' functions which may appear as only internally visible. I'd guess private members would fall into that category as well. In other words, I don't expect add_library itself is making any decisions onto visibility – Malachi Jun 01 '18 at 06:43
  • Hmm OK well perhaps my question is more general then :). What else would cause the compiler to make symbols internal-only? My function isn't static or a class member, it is just an explicitly instantiated instance of a template function. – Ben Farmer Jun 01 '18 at 07:18
  • 2
    Not really sure. Could you include some snippets of that code to give us an idea? – Malachi Jun 01 '18 at 07:44
  • Please post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Teivaz Jun 01 '18 at 12:29
  • 1
    Example? Of what? It's a general question. I can give the particular code snippet that prompted the question later, but my question wasn't about that specifically. – Ben Farmer Jun 02 '18 at 05:53
  • @BenFarmer: Do not try to make the question generic at all costs. Sometimes, generic questions can only be answered by a person, who know the **whole area in details**. Or it could require much of text to answer such questions. In your case, the question about exporting template function could be reasonable mean between usefulness and difficulty. But.. we don't know what do you mean by that: In the question you say "**specialisation** of a template function", in the comments - "explicitly **instantiated** instance of a template function." The code could shed a light, but you don't provide it. – Tsyvarev Jun 03 '18 at 23:54
  • I have made a new question with a MWE to address the specific case in my code https://stackoverflow.com/questions/50673981/visibility-of-template-function-specialisations-in-shared-libraries-dependence – Ben Farmer Jun 04 '18 at 06:03

1 Answers1

1

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.

Craig Scott
  • 9,238
  • 5
  • 56
  • 85