我在 macOS 10.15 上使用 Clang 10(不是 AppleClang),并试图通过-fuse-ld=lld
在 CMake flags 中指定来与 LLD 链接CMAKE_EXE_LINKER_FLAGS
。
但是,当我尝试使用 LLD 时出现以下错误:
The C++ compiler
"/Users/XXX/llvm/bin/clang++"
is not able to compile a simple test program.
It fails with the following output:
ld64.lld: warning: ignoring unknown argument: -platform_version
ld64.lld: warning: ignoring unknown argument: -search_paths_first
ld64.lld: warning: ignoring unknown argument: -headerpad_max_install_names
ld64.lld: warning: -sdk_version is required when emitting min version load command. Setting sdk version to match provided min version
ld64.lld: error: Unable to find library for -lc++
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [cmTC_0d561] Error 1
make: *** [cmTC_0d561/fast] Error 2
这是我的程序:
#include <iostream>
#include <cstdlib>
int main()
{
std::cout << "Hello World!" << std::endl;
return EXIT_SUCCESS;
}
这是我的 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13")
project(test VERSION 2.0 LANGUAGES CXX)
add_executable(testprog "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
这是我启动 CMake 的 bash 脚本:
#!/bin/bash
root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
sys_root="/Users/XXX/macOS-sdks/MacOSX10.13.sdk"
cmake_bin_path="/Applications/CMake.app/Contents/bin"
llvm_bin="/Users/XXX/llvm/bin"
function add_search_path() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="${PATH:+"$PATH:"}$1"
fi
}
add_search_path $cmake_bin_path
add_search_path $llvm_bin
cd "${root}/build"
cmake -DCMAKE_OSX_SYSROOT=${sys_root} \
-DCMAKE_C_COMPILER="${llvm_bin}/clang" \
-DCMAKE_CXX_COMPILER="${llvm_bin}/clang++" \
-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \
-DCMAKE_OSX_ARCHITECTURES=x86_64 \
-DCMAKE_BUILD_TYPE=Debug ..
cores=`sysctl -n hw.physicalcpu`
cmake --build "${root}/build" --target all -- -j ${cores}
./testprog