title: Clang not exporting C++ symbols
body: <p>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. </p>
  
  <p>However, running <code>nm</code> 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.</p>
  
  <p>For example:</p>
  
  <pre><code>-- test.h --
  #pragma once
  class Test {
  public:
      Test();
  };
  
  -- test.cpp --
  #include "test.h"
  #include &lt;iostream&gt;
  Test::Test() { std::cout &lt;&lt; "Hey!\n"; }
  </code></pre>
  
  <p>In this example <code>Test::Test()</code> is not visible outside!</p>
  
  <p>I'm using clang 6.0. Checking command invocations confirms tha the library is built with the <code>-shared</code> option.</p>
  
  <p>What could cause this behaviour?</p>
  
  <p>EDIT: 
  Compilation command (I've changed some long directories to DIR)</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>(Checking test.ccp.o shows that the symbol is external, so it must be the linker messing it up).</strong></p>
  
  <p>Linker command (<code>/usr/bin/ld</code> is a link to LLVM's linker LLD):</p>
  
  <pre><code> "/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
  </code></pre>
  
  <p>Checking with <code>nm</code>:</p>
  
  <pre><code>$ 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&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*)@@GLIBCXX_3.4
  </code></pre>