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

戏子无情 提交于 2020-04-15 10:51:27

问题


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} ..

回答1:


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"'
}


来源:https://stackoverflow.com/questions/60934005/clang-10-fails-to-link-c-application-with-cmake-on-macos-10-12

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