c++ - cmake 和 libcxx 的链接器错误

标签 c++ cmake linker clang

背景

llvm、clang、libcxx/libcxxabi 是我从源代码构建的。它们位于 usr/local

最小源文件

//findpath.cpp

#include <iostream>
int main(){
    std::cout << "Hi";
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_C_COMPILER "clang")

project(FindPath)
add_compile_options("-stdlib=libc++" "-lc++abi" "-v")

add_executable(path findpath.cpp)

使用 cmake(输出链接器错误)

~/.../findpath/build >>> cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ..                                                                                                ±[●][master]
-- The C compiler identification is Clang 8.0.0
-- The CXX compiler identification is Clang 8.0.0
-- Check for working C compiler: /usr/local/bin/clang
-- Check for working C compiler: /usr/local/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/local/bin/clang++
-- Check for working CXX compiler: /usr/local/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/firstlove/projects/smalltools/findpath/build
~/.../findpath/build >>> make                                                                                                                                      ±[●][master]
Scanning dependencies of target path
[ 50%] Building CXX object CMakeFiles/path.dir/findpath.cpp.o
clang version 8.0.0 (http://llvm.org/git/clang 55ead9f6f66545bcfda4a0f7e6dd3748a296f826) (https://github.com/llvm-mirror/llvm.git 8d97f0d4be38ebd4b38fe2cff51e0713b3dfba66)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Selected GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
clang-8: warning: -lc++abi: 'linker' input unused [-Wunused-command-line-argument]
 "/usr/local/bin/clang-8" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name findpath.cpp -mr
elocation-model static -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 -coverage-notes-file /home/firstlove/projects/smalltools/findpath/build/CMakeFiles/path.dir/findpath.cpp.gcno -resource-dir /usr/local/lib/clang/8
.0.0 -internal-isystem /usr/local/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /usr/local/lib/clang/8.0.0/include -internal-externc-isystem /inc
lude -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /home/firstlove/projects/smalltools/findpath/build -ferror-limit 19 -fmessage-length 176 
-fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o CMakeFiles/path.dir/findpath.cpp.o -x c++ /home/firstlove/projects/smalltools/
findpath/findpath.cpp -faddrsig
clang -cc1 version 8.0.0 based upon LLVM 8.0.0svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/bin/../include/c++/v1
 /usr/local/include
 /usr/local/lib/clang/8.0.0/include
 /usr/include
End of search list.
[100%] Linking CXX executable path
CMakeFiles/path.dir/findpath.cpp.o: In function `main':
findpath.cpp:(.text+0xa): undefined reference to `std::__1::cout'
CMakeFiles/path.dir/findpath.cpp.o: In function `std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<cha
r> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long)':
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x2f): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, st
d::__1::char_traits<char> >&)'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x1be): undefined reference to `std::__1::ios_base::getloc() const'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x1d4): undefined reference to `std::__1::ctype<char>::id'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x1d9): undefined reference to `std::__1::locale::use_facet(std::__1::locale::id&) const'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x235): undefined reference to `std::__1::locale::~locale()'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x257): undefined reference to `std::__1::locale::~locale()'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x327): undefined reference to `std::__1::ios_base::clear(unsigned int)'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x384): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x3ad): undefined reference to `std::__1::ios_base::__set_badbit_and_consider_rethrow()'
findpath.cpp:(.text._ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m[_ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEER
NS_13basic_ostreamIT_T0_EES7_PKS4_m]+0x3d8): undefined reference to `std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()'
CMakeFiles/path.dir/findpath.cpp.o: In function `std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> 
>(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char)':
findpath.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_tr
aitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x219): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::a
llocator<char> >::__init(unsigned long, char)'
findpath.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_tr
aitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x388): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::a
llocator<char> >::~basic_string()'
findpath.cpp:(.text._ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_[_ZNSt3__116__pad_and_outputIcNS_11char_tr
aitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_]+0x3a3): undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::a
llocator<char> >::~basic_string()'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/path.dir/build.make:84: path] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/path.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

没有cmake(无链接错误):clang++ -stdlib=libc++ -lc++abi -std=c++17 -v findpath.cpp

~/.../smalltools/findpath >>> clang++ -stdlib=libc++ -lc++abi -std=c++17 -v findpath.cpp                                                                           ±[●][master]
clang version 8.0.0 (http://llvm.org/git/clang 55ead9f6f66545bcfda4a0f7e6dd3748a296f826) (https://github.com/llvm-mirror/llvm.git 8d97f0d4be38ebd4b38fe2cff51e0713b3dfba66)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Selected GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/usr/local/bin/clang-8" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name findpath.cpp -mr
elocation-model static -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 -resource-dir /usr/local/lib/clang/8.0.0 -internal-isystem /usr/local/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isystem
 /usr/local/lib/clang/8.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/firstl
ove/projects/smalltools/findpath -ferror-limit 19 -fmessage-length 176 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/fi
ndpath-7dd1ae.o -x c++ findpath.cpp -faddrsig
clang -cc1 version 8.0.0 based upon LLVM 8.0.0svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/bin/../include/c++/v1
 /usr/local/include
 /usr/local/lib/clang/8.0.0/include
 /usr/include
End of search list.
 "/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crt1.o /usr/lib64/gc
c/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crti.o /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/crtbegin.o -L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1 -L/usr/lib64/gcc/x86_64-pc-l
inux-gnu/8.1.1/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../.. -L/usr/local/bin/../lib -L/lib -L/usr/lib -lc++abi /tmp
/findpath-7dd1ae.o -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/crtend.o /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crtn.o

但是从/usr/bin/ld来看,好像是用gcc来链接的?


我用谷歌搜索了很多,但没有发现任何有用的东西。如果需要更多信息,请告诉我,提前致谢。

更新

除了告诉我链接器是gnu的ld之外,其他错误信息都是一样的。

然后我尝试一下 llvm 的 lld,

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -stdlib=libc++ -lc++abi")

/usr/bin/cmake -H/home/firstlove/projects/smallProjects/smalltools/findpath -B/home/firstlove/projects/smallProjects/smalltools/findpath/build --check-build-system CMakeFiles/M
akefile.cmake 0                                                                                                                                                                
/usr/bin/cmake -E cmake_progress_start /home/firstlove/projects/smallProjects/smalltools/findpath/build/CMakeFiles /home/firstlove/projects/smallProjects/smalltools/findpath/bu
ild/CMakeFiles/progress.marks                                                                                                                                                  
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/firstlove/projects/smallProjects/smalltools/findpath/build'
make -f CMakeFiles/path.dir/build.make CMakeFiles/path.dir/depend
make[2]: Entering directory '/home/firstlove/projects/smallProjects/smalltools/findpath/build'
cd /home/firstlove/projects/smallProjects/smalltools/findpath/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/firstlove/projects/smallProjects/smalltools/findpa
th /home/firstlove/projects/smallProjects/smalltools/findpath /home/firstlove/projects/smallProjects/smalltools/findpath/build /home/firstlove/projects/smallProjects/smalltools
/findpath/build /home/firstlove/projects/smallProjects/smalltools/findpath/build/CMakeFiles/path.dir/DependInfo.cmake --color=                                                 
make[2]: Leaving directory '/home/firstlove/projects/smallProjects/smalltools/findpath/build'
make -f CMakeFiles/path.dir/build.make CMakeFiles/path.dir/build
make[2]: Entering directory '/home/firstlove/projects/smallProjects/smalltools/findpath/build'
[ 50%] Linking CXX executable path
/usr/bin/cmake -E cmake_link_script CMakeFiles/path.dir/link.txt --verbose=1
clang++     -fuse-ld=lld -stdlib=libc++ -lc++abi CMakeFiles/path.dir/findpath.cpp.o  -o path 
/usr/bin/ld.lld: error: undefined symbol: std::cout
>>> referenced by findpath.cpp
>>>               CMakeFiles/path.dir/findpath.cpp.o:(main)

/usr/bin/ld.lld: error: undefined symbol: std::basic_ostream<char, std::char_traits<char> >& std::operator<<<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<
char> >&, char const*)                                                                                                                                                         
>>> referenced by findpath.cpp
>>>               CMakeFiles/path.dir/findpath.cpp.o:(main)

/usr/bin/ld.lld: error: undefined symbol: std::ios_base::Init::Init()
>>> referenced by findpath.cpp
>>>               CMakeFiles/path.dir/findpath.cpp.o:(__cxx_global_var_init)

/usr/bin/ld.lld: error: undefined symbol: std::ios_base::Init::~Init()
>>> referenced by findpath.cpp
>>>               CMakeFiles/path.dir/findpath.cpp.o:(__cxx_global_var_init)
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/path.dir/build.make:84: path] Error 1
make[2]: Leaving directory '/home/firstlove/projects/smallProjects/smalltools/findpath/build'
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/path.dir/all] Error 2
make[1]: Leaving directory '/home/firstlove/projects/smallProjects/smalltools/findpath/build'
make: *** [Makefile:84: all] Error 2

哎呀,类似的链接器错误...

同样,没有 cmake,它工作得很好:

~/.../smalltools/findpath >>> clang++ -stdlib=libc++ -lc++abi -std=c++17 -v -fuse-ld=lld findpath.cpp                                                            ±[A1●][master]
clang version 6.0.1 (tags/RELEASE_601/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/8.2.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.0
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.2.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/usr/bin/clang-6.0" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name findpath.cpp -mrelocation
-model pic -pic-level 2 -pic-is-pie -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 -resource-dir /usr/lib/clang/6.0.1 -internal-isystem /usr/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isyste
m /usr/lib/clang/6.0.1/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/firstlove/p
rojects/smallProjects/smalltools/findpath -ferror-limit 19 -fmessage-length 176 -stack-protector 2 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -f
color-diagnostics -o /tmp/findpath-671136.o -x c++ findpath.cpp                                                                                                                
clang -cc1 version 6.0.1 based upon LLVM 6.0.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/bin/../include/c++/v1
 /usr/local/include
 /usr/lib/clang/6.0.1/include
 /usr/include
End of search list.
 "/usr/bin/ld.lld" -pie --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../lib64/Scrt
1.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../lib64/crti.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/crtbeginS.o -L/usr/bin/../lib64/gcc/x86_64-pc-linux
-gnu/8.2.0 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../lib64 -L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-g
nu/8.2.0/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib -lc++abi /tmp/findpath-671136.o -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/
crtendS.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../lib64/crtn.o                                                                                             
~/.../smalltools/findpath >>>                                                                                                                                   ±[A1●●][master]
~/.../smalltools/findpath >>> ./a.out                                                                                                                           ±[A1●●][master]
Hi%                                                                                                                                                                             

最佳答案

@jonathon解决这个问题my this question

解决方案是设置编译器和链接器选项,以便它可以找到 libc++:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -v")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")

关于c++ - cmake 和 libcxx 的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904938/

相关文章:

C++ 链接器错误 LNK2001 和 LNK1120 Winsock2 Lib

visual-studio - cmake:add_custom_command 仅在第一次调用

c++ - 无法使用 cmake + vcpkg 在 VS Code 中包含 C++ 项目的库

android - "failed to map segment from shared object: operation not permitted"的可能原因是什么,如何调试?

android - 错误 : unknown type name 'class' NDK CDT JNI

c++ - 在类中声明一个指针,但在另一个线程中初始化

c++ - Boost Python 和 C++ 头文件

c++ - 类模板、矩阵和加法

cmake - 如何在 cmake 上获取库的完整 native 名称?

c++ - 仅 header 类 + 未定义的函数引用,仅当返回该类的对象时