Crossplatform: Linux/Windows --> macOS

Does anyone have experience with cross-platform linking for Mac? We copied the SDK from a real macOS machine, when compiling we specify the target and sysroot - object files are generated.

But when we link from Linux to Mac, the resulting binary does not run. When we link from Windows to Mac - problems with the standard library.

1 Like

How are you linking/how can the problem be reproduced? And, what kind of error/problem do you get?

We are working together on a project, and I will now try to describe the whole process to reproduce the problem.

The version of system is:

Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy

Clang version is:

Ubuntu clang version 16.0.1 (++20230328073357+42d1b276f779-1~exp1~20230328073502.65)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

And the test code named as main17.cpp:

#include <string>
#include <map>
#include <cassert>
#include <iostream>

int main()
{
    std::map<std::string, std::string> map;
    auto [iterator1, succeed1] = map.try_emplace("key", "abc");
    auto [iterator2, succeed2] = map.try_emplace("key", "cde");
    auto [iterator3, succeed3] = map.try_emplace("another_key", "cde");

    assert(succeed1);
    assert(!succeed2);
    assert(succeed3);

    for (auto&& [key, value] : map)
    {
        std::cout << key << ": " << value << "\n";
    }
}

Ok,and now we are trying to get a workable executable for MacOS, in example - arm64. But for x64_86 the same thing happens:

clang++ -v -fuse-ld=lld --sysroot=/media/sf_workspace/build/targets/MacOSX13.1.sdk /media/sf_workspace/PowerTech/Samples/cpp-main/main17.cpp -o /media/sf_workspace/PowerTech/Samples/cpp-main/lin_run_lld --target=arm64-apple-darwin21 -stdlib=libc++ -std=c++17
Ubuntu clang version 16.0.1 (++20230328073357+42d1b276f779-1~exp1~20230328073502.65)
Target: arm64-apple-darwin21
Thread model: posix
InstalledDir: /usr/bin

ignoring nonexistent directory "/usr/bin/../include/c++/v1"

 "/usr/lib/llvm-16/bin/clang" -cc1 -triple arm64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main17.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +crc -target-feature +crypto -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +lse -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +neon -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sm4 -target-feature +sha3 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -v -fcoverage-compilation-dir=/home/oem -resource-dir /usr/lib/llvm-16/lib/clang/16 -isysroot /media/sf_workspace/build/targets/MacOSX13.1.sdk -stdlib=libc++ -internal-isystem /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include/c++/v1 -internal-isystem /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/local/include -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-externc-isystem /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/home/oem -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main17-ba72a0.o -x c++ /media/sf_workspace/PowerTech/Samples/cpp-main/main17.cpp

clang -cc1 version 16.0.1 based upon LLVM 16.0.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/local/include"
ignoring nonexistent directory "/media/sf_workspace/build/targets/MacOSX13.1.sdk/Library/Frameworks"

#include "..." search starts here:
#include <...> search starts here:

 /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include/c++/v1
 /usr/lib/llvm-16/lib/clang/16/include
 /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include
 /media/sf_workspace/build/targets/MacOSX13.1.sdk/System/Library/Frameworks (framework directory)
End of search list.

 "/usr/lib/llvm-16/bin/ld64.lld" -demangle -dynamic -arch arm64 -platform_version macos 12.0.0 12.0.0 -syslibroot /media/sf_workspace/build/targets/MacOSX13.1.sdk -o /media/sf_workspace/PowerTech/Samples/cpp-main/lin_run_lld /tmp/main17-ba72a0.o -lc++ -lSystem

as output i’ve got the binary, and when I try to run it, i’ve got on target machine:

dmitry@host22-129-30-192 ~ % chmod +x lin_run_lld
dmitry@host22-129-30-192 ~ % ./lin_run_lld
zsh: exec format error: ./lin_run_lld
dmitry@host22-129-30-192 ~ %

Ok, then I build the ld64 port from cctools, and when I do:

clang++ -v -fuse-ld=/home/oem/ld64/target/bin/ld64 --sysroot=/media/sf_workspace/build/targets/MacOSX13.1.sdk /media/sf_workspace/PowerTech/Samples/cpp-main/main17.cpp -o /media/sf_workspace/PowerTech/Samples/cpp-main/lin_run_ld64 --target=arm64-apple-darwin21 -stdlib=libc++ -std=c++17
Ubuntu clang version 16.0.1 (++20230328073357+42d1b276f779-1~exp1~20230328073502.65)
Target: arm64-apple-darwin21
Thread model: posix
InstalledDir: /usr/bin

ignoring nonexistent directory "/usr/bin/../include/c++/v1"

 "/usr/lib/llvm-16/bin/clang" -cc1 -triple arm64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main17.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +crc -target-feature +crypto -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +lse -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +neon -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sm4 -target-feature +sha3 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -v -fcoverage-compilation-dir=/home/oem -resource-dir /usr/lib/llvm-16/lib/clang/16 -isysroot /media/sf_workspace/build/targets/MacOSX13.1.sdk -stdlib=libc++ -internal-isystem /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include/c++/v1 -internal-isystem /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/local/include -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-externc-isystem /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/home/oem -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main17-e0afb5.o -x c++ /media/sf_workspace/PowerTech/Samples/cpp-main/main17.cpp
clang -cc1 version 16.0.1 based upon LLVM 16.0.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/local/include"
ignoring nonexistent directory "/media/sf_workspace/build/targets/MacOSX13.1.sdk/Library/Frameworks"

#include "..." search starts here:

#include <...> search starts here:
 /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include/c++/v1
 /usr/lib/llvm-16/lib/clang/16/include
 /media/sf_workspace/build/targets/MacOSX13.1.sdk/usr/include
 /media/sf_workspace/build/targets/MacOSX13.1.sdk/System/Library/Frameworks (framework directory)
End of search list.

 "/home/oem/ld64/target/bin/ld64" -dynamic -arch arm64 -macosx_version_min 12.0.0 -syslibroot /media/sf_workspace/build/targets/MacOSX13.1.sdk -o /media/sf_workspace/PowerTech/Samples/cpp-main/lin_run_ld64 /tmp/main17-e0afb5.o -lc++ -lSystem

As output I got an executable works great on the target machine:

dmitry@host22-129-30-192 ~ % chmod +x lin_run_ld64
dmitry@host22-129-30-192 ~ % ./lin_run_ld64
another_key: cde
key: abc
dmitry@host22-129-30-192 ~ %

after comparing the output files i see the ld64.ldd output is just zeros filed file.

PS: clang for ubuntu installed by llvm.sh from the apt.llvm.org
PPS: on the target machines os version is MacOS:

dmitry@host22-129-30-192 ~ % uname -a
Darwin host22-129-30-192.springshosting.net 22.2.0 Darwin Kernel Version 22.2.0: Fri Nov 11 02:04:44 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T8103 arm64

Thank you!

1 Like

A problem for Windows is much interesting.

I get the latest release of clang from:Release LLVM 16.0.0 · llvm/llvm-project · GitHub
Also I get the sdk from the latest xcode.

clang version 16.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\LLVM\bin

After that I do:

C:\LLVM\bin\clang++.exe -v -fuse-ld=lld --sysroot=H:\repository\workspace\build\targets\MacOSX13.1.sdk H:\repository\workspace\PowerTech\Samples\cpp-memory\main.cpp -o H:\repository\workspace\PowerTech\Samples\run_mac -target x86_64-apple-darwin21 -mlinker-version=520 -stdlib=libc++
clang version 16.0.0
Target: x86_64-apple-darwin21
Thread model: posix
InstalledDir: C:\LLVM\bin
ignoring nonexistent directory "C:\LLVM\bin\..\include\c++\v1"
 "C:\\LLVM\\bin\\clang++.exe" -cc1 -triple x86_64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=all -ffp-contract=on -fno-rounding-math -funwind-tables=2 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -target-linker-version 520 -v "-fcoverage-compilation-dir=C:\\Users\\Cynic" -resource-dir "C:\\LLVM\\lib\\clang\\16" -isysroot "H:\\repository\\workspace\\build\\targets\\MacOSX13.1.sdk" -stdlib=libc++ -internal-isystem "H:\\repository\\workspace\\build\\targets\\MacOSX13.1.sdk\\usr\\include\\c++\\v1" -internal-isystem "H:\\repository\\workspace\\build\\targets\\MacOSX13.1.sdk\\usr\\local\\include" -internal-isystem "C:\\LLVM\\lib\\clang\\16\\include" -internal-externc-isystem "H:\\repository\\workspace\\build\\targets\\MacOSX13.1.sdk\\usr\\include" -fdeprecated-macro "-fdebug-compilation-dir=C:\\Users\\Cynic" -ferror-limit 19 -fmessage-length=120 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o "C:\\Users\\Cynic\\AppData\\Local\\Temp\\main-681ce9.o" -x c++ "H:\\repository\\workspace\\PowerTech\\Samples\\cpp-memory\\main.cpp"
clang -cc1 version 16.0.0 based upon LLVM 16.0.0 default target x86_64-pc-windows-msvc
ignoring nonexistent directory "H:\repository\workspace\build\targets\MacOSX13.1.sdk\usr\local\include"
ignoring nonexistent directory "H:\repository\workspace\build\targets\MacOSX13.1.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 H:\repository\workspace\build\targets\MacOSX13.1.sdk\usr\include\c++\v1
 C:\LLVM\lib\clang\16\include
 H:\repository\workspace\build\targets\MacOSX13.1.sdk\usr\include
 H:\repository\workspace\build\targets\MacOSX13.1.sdk/System/Library/Frameworks (framework directory)
End of search list..

And I’ve got the error:

/MacOSX13.1.sdk/usr/include/c++/v1/__config:66:19: fatal error: cannot open file
      'H:/repository/workspace/build/targets/MacOSX13.1.sdk/usr/include/c++/v1/RTK_device_methods.h': No such file or
      directory
#if __has_include(<RTK_device_methods.h>) && defined(_LIBCPP_HAS_NO_RANDOM_DEVICE) && defined(_LIBCPP_HAS_NO_LOC...
                  ^
1 error generated

How can it be solved? Thank you.

PS:The system is Windows 10 pro x64 version 10.0.19044.

The test environment can be downloaded here: test.zip - Google Drive

So, finally I resolve this problem for the windows. The solution is…I just build Clang from the sources on my machine with winlibs clang with BUILD_SHARED_LIBS=ON and without -static.