【发布时间】:2018-03-16 00:57:08
【问题描述】:
我正在使用 CMake 构建一个共享库。这个库包含一些 C++ 类,这些类应该被使用这个库的程序引用。
但是,在生成的库上运行 nm 表明 C++ 类的函数在外部不可见,而是具有内部链接。这意味着我不能使用它们,并且尝试引用它们会导致链接阶段出现未定义的引用。
例如:
-- test.h --
#pragma once
class Test {
public:
Test();
};
-- test.cpp --
#include "test.h"
#include <iostream>
Test::Test() { std::cout << "Hey!\n"; }
在这个例子中Test::Test()在外面是不可见的!
我正在使用 clang 6.0。检查命令调用确认库是使用-shared 选项构建的。
什么可能导致这种行为?
编辑: 编译命令(我把一些长目录改成了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
(检查test.ccp.o显示符号是外部的,所以一定是链接器搞砸了)。
链接器命令(/usr/bin/ld 是到 LLVM 的链接器 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
检查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
【问题讨论】:
-
类型没有地址,所以它们不会出现在符号表中。
-
@BenVoigt 对不起,我的意思是类构造函数或成员函数之类的函数。
-
您能否发布构建该测试示例时 CMake 运行的确切命令?
-
@aschepler 添加。我相信问题出在链接器命令中。
-
我在编译标志中看到了
-fvisibility hidden -fvisibility-inlines-hidden。这大概就是原因。对 CMake 的那一部分不够熟悉,无法提供解决方案,但GenerateExportHeader模块的文档可能是相关的。
标签: c++ linker clang dynamic-library