1

I am trying to cross compile for linux on macOS. It appears that clang is not respecting the sysroot I set.

Program

#include <stdio.h>

int main() {
    printf("Hello World!\n");
    return 0;
}

Commands

CC=/Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin/clang
${CC} -v -c main.c -target x86_64-pc-linux-elf -o main.o\
    -isysroot /Users/zrgorak/Development/com/prog/site_scons/builders/linux/sysroot
${CC} -v -o main -target x86_64-pc-linux-elf main.o \
    -isysroot /Users/zrgorak/Development/com/prog/site_scons/builders/linux/sysroot

Output

% make 
/Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin/clang -v -c -target x86_64-pc-linux-elf main.c -o main.o \
        -isysroot /Users/user/Development/com/prog/site_scons/builders/linux/sysroot
clang version 9.0.0 (git://github.com/llvm/llvm-project.git 0399d5a9682b3cef71c653373e38890c63c4c365)
Target: x86_64-pc-linux-elf
Thread model: posix
InstalledDir: /Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin
 "/Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin/clang-9" -cc1 -triple x86_64-pc-linux-elf -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -target-linker-version 512.4 -v -coverage-notes-file /Users/user/Development/testlinker/main.gcno -resource-dir /Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/lib/clang/9.0.0 -isysroot /Users/user/Development/com/prog/site_scons/builders/linux/sysroot -internal-isystem /usr/local/include -internal-isystem /Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/lib/clang/9.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /Users/user/Development/testlinker -ferror-limit 19 -fmessage-length 128 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o main.o -x c main.c
clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target x86_64-apple-darwin19.0.0
ignoring nonexistent directory "/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/lib/clang/9.0.0/include
End of search list.
main.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.
make: *** [target] Error 1

My sysroot is /usr and /lib copied from Ubuntu 19.x

% ll /Users/user/Development/com/prog/site_scons/builders/linux/sysroot/usr/include/stdio.h
-rw-r--r--@ 1 user  staff  29949 Sep 16 10:56 /Users/user/Development/com/prog/site_scons/builders/linux/sysroot/usr/include/stdio.h

This even happens when I use crosstool-ng and use their sysroot.

It's like clang doesn't look in the sysroot I set?

6
  • 3
    You are not cross-compiling but cross-linking. The verbose output shows you exactly this: -triple x86_64-apple-macosx10.15.0. The linker (given an elf target option) expects an elf binary but you give it mach-o. Get a libc version for your cross-compilation target and then let the whole build run with the respective options.
    – dtell
    Dec 5, 2019 at 23:33
  • @dtell - didn't notice that! adding -target x86_64-pc-linux-elf to the first command then produces fatal error: 'stdio.h' file not found. This seems like a sysroot issue? Dec 5, 2019 at 23:38
  • 1
    That's exactly the point Get a libc version for your cross-compilation target ;) Although I'm not 100% sure if Apple's ld64 supports elf.
    – dtell
    Dec 5, 2019 at 23:39
  • It may be easier to get a clean build with a VM or a Docker container process, if not using a continuous integration platform of some kind. Cross-compiling is not necessarily trivial to set up.
    – tadman
    Dec 5, 2019 at 23:46
  • @tadman that is what I'm finding out... Dec 5, 2019 at 23:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.