Software Design

Design de software em C, C++, Java, etc…

Cross-compiling for x86 in FreeBSD 64 bit

Today I needed to create 32 bit binaries of a program but I had only a 64 bit FreeBSD 8 machine. I must say that I am far from being a FreeBSD expert like some guys I know, so maybe there is a better way for doing this. But… was the only way I found.

In Linux I often do this by using a tool called crosstool. The crosstool is a series of scripts that makes the process of building a custom toolchain so easy that my 9-year daughter is able to create her custom toolchain…

I googled and could not find anything like crosstool for BSD, the easiest way I found is by creating a chroot for i386 architecture and compiling the program inside the chroot. The information published here I found in many forums and pages about freebsd. Following are the steps I’ve done:

% mkdir /usr/x86/

% cd /usr/src

% make TARGET_ARCH=i386 buildworld

% make TARGET_ARCH=i386 DESTDIR=/usr/x86 installworld

You’ll need some coffee to wait the compilation to end. After that the base system is inside /usr/x86, but you still need to create the proc filesystem. You can do this:

% mkdir /usr/x86/proc
% mount -t procfs proc /usr/x86/proc

Now you must be able to chroot to your brand new x86 kernel. Just type:

% chroot /usr/x86

After that I realized that I still need some files that are outside the new root, so I needed to mount a nullfs for the directories containing those files:

#get out from jail
% exit

#mount /usr/ports and /usr/local/include inside the <em>jail</em>

% mkdir -p /usr/x86/usr/ports
% mkdir -p /usr/x86/usr/local/include

% mount -t nullfs /usr/ports /usr/x86/usr/ports
% mount -t nullfs /usr/local/include /usr/x86/usr/local/include

#get in the jail again

% chroot /usr/x86

# build CMake ( this is optional )
% cd /usr/ports/devel/cmake
% make && make install

#build boost libraries needed
% cd /tmp/boost/tools/build/v2/
% ./
% ./bjam install

%/usr/local/bin/bjam --with-thread --with-regex --with-filesystem --layout=tagged

# compile the program

% cd ~/<project-name>/build
% cmake ../src/
% make

To assert that the file is x86 type:

% file <binary-name>
<binary-name>: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 8.2, stripped

That’s it… hope it helps somebody.


Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )


Conectando a %s


Publicado às outubro 18, 2011 por em C++, FreeBSD, Unix e marcado , , , , , .
%d blogueiros gostam disto: