Using -fuse-ld=lld with Clang on macOS
Asked Answered
D

2

7

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
Dependable answered 31/3, 2020 at 6:37 Comment(0)
B
6

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

Barbarity answered 31/3, 2020 at 11:4 Comment(0)
J
1

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

Jailhouse answered 27/9, 2020 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.