Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang 10 fails to link C++ application with CMake on macOS 10.12

I have a Jenkins build server running macOS 10.12.

I am compiling a C++ application using the latest Clang 10 (not AppleClang) with CMake 3.17.

The error I get is:

The C++ compiler

"/Users/XXX/llvm/bin/clang++"

is not able to compile a simple test program.

It fails with the following output:

ld: unknown option: -platform_version
clang-10: error: linker failed with exit code 1

This works fine with Clang 9 on the same server and Clang 10 works fine on macOS 10.15 with all other build tools and source files the same (Jenkins runs a clean build each time). It seems to be the combination of Clang 10 and macOS 10.12. Has anything changed between Clang 9 and Clang 10 that would cause this?

I'm invoking CMake like so:

cmake -DCMAKE_OSX_SYSROOT="${macos_sdk}" \
      -DCMAKE_C_COMPILER="${llvm_bin}/clang" \
      -DCMAKE_CXX_COMPILER="${llvm_bin}/clang++" \
      -DCMAKE_OSX_ARCHITECTURES=${architectures} \
      -DCMAKE_BUILD_TYPE=${make_build_type} ..
like image 768
keith Avatar asked Jan 01 '23 05:01

keith


2 Answers

Passing the linker version to Clang via -mlinker-version=305 resolved the issue.

For CMake this can be done like so:

-DCMAKE_CXX_FLAGS="-mlinker-version=305"

Can't help but feel this is a bug.

The linker verison can be obtained via ld -v on macOS 10.12 where the problem occurs.

A handy bash function to get the ld version for passing to Clang:

#!/bin/bash

function get_ld_version() {
  local info=$( ld -v 2>&1 > /dev/null )

  echo "${info}" | perl -wne '/.ld64-(.*?)[^0-9]/ and print "$1\n"'
}
like image 90
keith Avatar answered Jan 04 '23 04:01

keith


Just in case someone is using CMAKE with CLion IDE which tests both C and C++ compilers, adding -DCMAKE_CXX_FLAGS="-mlinker-version=405"isn't enough you also need to add "-DCMAKE_C_FLAGS="-mlinker-version=405", of course as what @keith mentioned you should use your own linker version

like image 23
jimmy mac Avatar answered Jan 04 '23 04:01

jimmy mac