Using -fuse-ld=lld with Clang on macOS

元气小坏坏 提交于 2021-01-27 16:43:40

问题


I am using Clang 10 (not AppleClang) on macOS 10.15 and am trying to link with LLD by specifying -fuse-ld=lld in the CMake flags CMAKE_EXE_LINKER_FLAGS.

However, I am getting the following error when I try to use 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

Here is my program:

#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << "Hello World!" << std::endl;

    return EXIT_SUCCESS;
}

Here is my CMakeLists.txt file:

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")

Here is my bash script to launch CMake:

#!/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

回答1:


lld doesn't work on macOS. Use ld aka ld64. cf. http://lists.llvm.org/pipermail/cfe-dev/2019-March/061666.html




回答2:


AppleClang is a stripped down version of LLVM-Clang. Build the latter with the required projects and lld will work for simple programs on macOS too.

Searching the git history shows signs of life for lld-mach-o too!

It fails with -framework flags though. If you find a bug, please report at https://bugs.llvm.org/buglist.cgi?bug_status=__open__&component=MachO&product=lld

For Clang compile instructions (and also a way to use it in Xcode) see c++20 library support for xcode 12



来源:https://stackoverflow.com/questions/60944732/using-fuse-ld-lld-with-clang-on-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!