Search code examples
c++linkerclangdynamic-library

Clang not exporting C++ symbols


I'm using CMake to build a shared library. This library contains a few C++ classes that are supposed to be referenced by programs making use of this library.

However, running nm on the resulting library shows that the C++ classes' functions are not externally visible but have instead internal linkage. This means I can't use them and trying to reference them causes an undefined reference in the linking stage.

For example:

-- test.h --
#pragma once
class Test {
public:
    Test();
};

-- test.cpp --
#include "test.h"
#include <iostream>
Test::Test() { std::cout << "Hey!\n"; }

In this example Test::Test() is not visible outside!

I'm using clang 6.0. Checking command invocations confirms tha the library is built with the -shared option.

What could cause this behaviour?

EDIT: Compilation command (I've changed some long directories to DIR)

clang -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -v -ffunction-sections -fdata-sections -coverage-notes-file DIR/example/CMakeFiles/examplelib.dir/test.cpp.gcno -resource-dir LLVM_DIR/build/lib/clang/6.0.0 -D HAVE_CONFIG_H -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D examplelib_EXPORTS -I LLVM_DIR/llvm/include -I LLVM_DIR/build/include -I LLVM_DIR/llvm/tools/clang/include -I LLVM_DIR/build/tools/clang/include -D GOOGLE_PROTOBUF_NO_RTTI -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/local/include -internal-isystem LLVM_DIR/build/lib/clang/6.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wall -w -std=c++14 -fdeprecated-macro -fdebug-compilation-dir DIR/example -ferror-limit 19 -fmessage-length 101 -fvisibility hidden -fvisibility-inlines-hidden -pthread -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o CMakeFiles/examplelib.dir/test.cpp.o -x c++ DIR/example/test.cpp

(Checking test.ccp.o shows that the symbol is external, so it must be the linker messing it up).

Linker command (/usr/bin/ld is a link to LLVM's linker LLD):

 "/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -shared -o libexamplelib.so /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/crtbeginS.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../.. -LDIR/build/bin/../lib -L/lib -L/usr/lib -ltinfo -z defs -z nodelete -soname libexamplelib.so CMakeFiles/examplelib.dir/test.cpp.o -lpthread -lstdc++ -lm -lgcc_s -lpthread -lc -lgcc_s /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/crtendS.o /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0/../../../x86_64-linux-gnu/crtn.o

Checking with nm:

$ nm -gC libexamplelib.so 
0000000000201038 B __bss_start
                 U __cxa_atexit@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000201038 D _edata
0000000000201040 B _end
00000000000007fc T _fini
                 w __gmon_start__
0000000000000638 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U std::ios_base::Init::Init()@@GLIBCXX_3.4
                 U std::ios_base::Init::~Init()@@GLIBCXX_3.4
                 U std::cout@@GLIBCXX_3.4
                 U std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)@@GLIBCXX_3.4

Solution

  • This is because of the -fvisibility hidden compilation flag added, seemingly by default, by CMake.

    To restore visibility to default, it is enough to add these two lines to CMakeLists.txt:

    set(CMAKE_CXX_VISIBILITY_PRESET default)
    set(CMAKE_VISIBILITY_INLINES_HIDDEN 0)