Cross-compile macOS to Ubuntu

Paolo Invernizzi paolo.invernizzi at gmail.com
Sun Dec 30 17:19:00 UTC 2018


On Wednesday, 26 December 2018 at 23:55:14 UTC, kinke wrote:
> On Wednesday, 26 December 2018 at 18:06:47 UTC, Paolo 
> Invernizzi wrote:
>> Coping the relevant files and building with the command line 
>> below [1] produce an executable, but fails on Linux  [2], oh 
>> well...
>
> I guess this has sth. to do with the linker command-line args 
> *order*. Note that gcc puts the startup object files first, 
> then the actually compiled user code object files, and then the 
> crtend object files. This order is probably not reproducible 
> with a normal compile-and-link LDC command line.
> Before blaming LLD, it may be worth feeding it the exact same 
> command line as ld/collect2 and check that result.
>
> Btw, if you see those no-warn-on-search-mismatch errors, just 
> edit your etc/ldc2.conf config file, it should be 
> self-explanatory.

Indeed it was the order, now I'm able to correctly cross compile 
for Ubuntu 18.04, but I need to use directly LLD.

There's a plan to add more flexibility in specifying the linker 
command line for `-link-internally`?

Anyway, below I'm reporting the full pass I've used, If worth I 
can add them to the wiki, if someone is interested.

-- Paolo

```
brew install llvm
curl -fsS https://dlang.org/install.sh | bash -s ldc && source 
~/dlang/ldc-1.13.0/activate

/usr/local/opt/llvm/bin/ld.lld --version : LLD 7.0.1 (compatible 
with GNU linkers)
ldc2 --version                           : LDC - the LLVM D 
compiler (1.13.0)

mkdir -p ubuntu/lib/x86_64-linux-gnu
mkdir -p ubuntu/usr/lib/gcc/x86_64-linux-gnu/7
mkdir -p ubuntu/usr/lib/x86_64-linux-gnu
mkdir -p ubuntu/ldc-1.13.0/lib

docker run --rm -it -v $PWD/ubuntu:/ubuntu ubuntu:18.04

apt-get update && apt-get install -y curl xz-utils 
build-essential lld
curl -fsS https://dlang.org/install.sh | bash -s ldc
cp /lib/x86_64-linux-gnu/libc.so.6                  
/ubuntu/lib/x86_64-linux-gnu
cp /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2       
/ubuntu/lib/x86_64-linux-gnu
cp /lib/x86_64-linux-gnu/libpthread.so.0            
/ubuntu/lib/x86_64-linux-gnu
cp /lib/x86_64-linux-gnu/libmvec.so.1               
/ubuntu/lib/x86_64-linux-gnu
cp /lib/x86_64-linux-gnu/libm.so.6                  
/ubuntu/lib/x86_64-linux-gnu
cp /lib/x86_64-linux-gnu/librt-2.27.so              
/ubuntu/lib/x86_64-linux-gnu
cp /lib/x86_64-linux-gnu/libdl-2.27.so              
/ubuntu/lib/x86_64-linux-gnu
cp /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o      
/ubuntu/usr/lib/gcc/x86_64-linux-gnu/7
cp /usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o        
/ubuntu/usr/lib/gcc/x86_64-linux-gnu/7
cp /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a         
/ubuntu/usr/lib/gcc/x86_64-linux-gnu/7
cp /usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so.1    
/ubuntu/usr/lib/gcc/x86_64-linux-gnu/7
cp /usr/lib/x86_64-linux-gnu/Scrt1.o                
/ubuntu/usr/lib/x86_64-linux-gnu
cp /usr/lib/x86_64-linux-gnu/crti.o                 
/ubuntu/usr/lib/x86_64-linux-gnu
cp /usr/lib/x86_64-linux-gnu/crtn.o                 
/ubuntu/usr/lib/x86_64-linux-gnu
cp /usr/lib/x86_64-linux-gnu/libc_nonshared.a       
/ubuntu/usr/lib/x86_64-linux-gnu
cp /usr/lib/x86_64-linux-gnu/libmvec_nonshared.a    
/ubuntu/usr/lib/x86_64-linux-gnu
cp /usr/lib/x86_64-linux-gnu/libpthread_nonshared.a 
/ubuntu/usr/lib/x86_64-linux-gnu
cp ~/dlang/ldc-1.13.0/lib/libphobos2-ldc.a          
/ubuntu/ldc-1.13.0/lib/
cp ~/dlang/ldc-1.13.0/lib/libdruntime-ldc.a         
/ubuntu/ldc-1.13.0/lib/
exit

ldc2 -c hello.d
/usr/local/opt/llvm/bin/ld.lld \
   -o hello \
   \
   --sysroot=/ \
   --build-id \
   --eh-frame-hdr \
   -m elf_x86_64 \
   --hash-style=gnu \
   -pie \
   -z now \
   -z relro \
   \
   --lto-O2 \
   --gc-sections \
   \
   -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
   \
   -L./ubuntu/usr/lib/gcc/x86_64-linux-gnu/7 \
   -L./ubuntu/usr/lib/x86_64-linux-gnu \
   -L./ubuntu/usr/lib \
   -L./ubuntu/lib/x86_64-linux-gnu  \
   -L./ubuntu/lib \
   \
   ./ubuntu/usr/lib/x86_64-linux-gnu/Scrt1.o \
   ./ubuntu/usr/lib/x86_64-linux-gnu/crti.o \
   ./ubuntu/usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o \
   \
   hello.o \
   \
   ./ubuntu/ldc-1.13.0/lib/libphobos2-ldc.a  
./ubuntu/ldc-1.13.0/lib/libdruntime-ldc.a \
   \
   ./ubuntu/lib/x86_64-linux-gnu/librt-2.27.so \
   ./ubuntu/lib/x86_64-linux-gnu/libdl-2.27.so \
   ./ubuntu/lib/x86_64-linux-gnu/libpthread.so.0 
./ubuntu/usr/lib/x86_64-linux-gnu/libpthread_nonshared.a \
   ./ubuntu/lib/x86_64-linux-gnu/libm.so.6 
./ubuntu/usr/lib/x86_64-linux-gnu/libmvec_nonshared.a 
./ubuntu/lib/x86_64-linux-gnu/libmvec.so.1 \
   \
   ./ubuntu/lib/x86_64-linux-gnu/libc.so.6 
./ubuntu/usr/lib/x86_64-linux-gnu/libc_nonshared.a 
./ubuntu/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 \
   ./ubuntu/usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a \
   ./ubuntu/usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so.1 \
   \
   ./ubuntu/usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o \
   ./ubuntu/usr/lib/x86_64-linux-gnu/crtn.o
```




More information about the Digitalmars-d mailing list