Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-compile to Linux from OS X

I want to compile binaries for a Linux server from an OS X 10.7 + Eclipse build system. This means a GCC cross-compiler. I'm starting from native 4.7.1 and shooting for a host 4.7.1.

Most online instructions are specific to embedded development. I want to use libstdc++-v3 and glibc, which make it a little different.

What succeeds:

  1. Install packages

    port install gcc47
    export CC=gcc-mp-47
    export LD=ld-mp-47
    export CXX=g++-mp-47
    export CPP=cpp-mp-47
    
    port install gmake
    ln /opt/local/bin/gmake /opt/local/bin/make
    
    port install gsed
    ln /opt/local/bin/gsed /opt/local/bin/sed
    
    port install gawk
    port install autoconf
    port install msgfmt
    
  2. Make and install binutils-2.22

  3. make all-gcc and make install-gcc

    Used the following config:

    $ ../gcc/configure --enable-languages=c,c++,objc,obj-c++ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --prefix=/usr/local/cross/linux --target=x86_64-pc-linux-gnu --disable-nls

  4. Point the build environment at the cross-compiler

    export CC=/usr/local/cross/linux/bin/x86_64-pc-linux-gnu-gcc
    export LD=/usr/local/cross/linux/bin/x86_64-pc-linux-gnu-ld
    export AR=/usr/local/cross/linux/bin/x86_64-pc-linux-gnu-ar
    export CXX=/usr/local/cross/linux/bin/x86_64-pc-linux-gnu-g++
    export CPP=/usr/local/cross/linux/bin/x86_64-pc-linux-gnu-cpp
    
  5. Build glibc-2.16 using the config

    ../glibc/configure --host=x86_64-pc-linux-gnu --build=x86_64-apple-darwin11 --prefix=/usr/local/cross/linux/ --with-binutils=/usr/local/cross/linux/bin/ --with-headers=/usr/local/cross/linux/include/ libc_cv_forced_unwind=yes libc_cv_ctors_header=yes libc_cv_c_cleanup=yes

The failure:

glibc build fails before long. This message appears many times:

In file included from ./sysdeps/unix/sysdep.h:20:0,
                 from ./sysdeps/unix/x86_64/sysdep.h:18,
                 from sysdeps/unix/sysv/linux/x86_64/sysdep.h:22,
                 from <stdin>:1:
sysdeps/unix/sysv/linux/sys/syscall.h:24:24: fatal error: asm/unistd.h: No such file or directory
compilation terminated.

This may be normal, though.

Then it fails to produce stdio_lim.h. With make -d, I get this:

          Successfully remade target file `/Users/dkrauss/Documents/work/glibc-build/csu/abi-tag.h'.
          Considering target file `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.h'.
           File `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.h' does not exist.
           Looking for an implicit rule for `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.h'.
           Trying pattern rule with stem `lim'.
           Trying implicit prerequisite `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.st'.
           Found an implicit rule for `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.h'.
            Considering target file `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.st'.
            Recently tried and failed to update file `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.st'.
make[2]: *** No rule to make target `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.st', needed by `/Users/dkrauss/Documents/work/glibc-build/bits/stdio_lim.h'.  Stop.

This is the first header it attempts to build in bits. There is a file stdio_lim.h.in, which is the only .in file in its directory. Should some rule be connecting .st to .in?

like image 813
Potatoswatter Avatar asked Aug 09 '12 02:08

Potatoswatter


People also ask

Can GCC compile for Mac?

Often times, you need c or gcc compiler to compile open source projects in Mac OS X. The problem is Mac OS X doesn't install the gcc compiler by default. In terminal, type “ gcc “, you will get message “command not found”.

Can GCC cross compile?

For instance when installing GCC, the GNU Compiler Collection, we can use --target= target to specify that we want to build GCC as a cross-compiler for target . Mixing --build and --target , we can cross-compile a cross-compiler; such a three-way cross-compilation is known as a Canadian cross.

Can I compile C code on Mac?

C code can be written in any platform like Mac, Windows, etc. C compilers compile C code and create an executable according to the platform.


1 Answers

In order to properly build glibc, you need the header files from a Linux system - probably the kernel headers are sufficient.

This is due to the fact that those headers are needed in any normal build of the software for that system, and the same is no different when cross-compiling.

It's been a long time since I worked on creating cross-compilers, so I can't tell you how to get the build of glibc to see the headers, but I believe it can be done in a straight-forward manner.

like image 171
ash Avatar answered Sep 28 '22 10:09

ash