C Cross-compile from Linux to FreeBSD problem

Hi,
I was trying to compile a cross-compiler from x86_64-linux to x86_64-freebsd, but I failed several times :(
Do you even know what cross compile mean?

I followed this thread https://forums.freebsd.org/threads/howto-build-a-cross-compiler.29786/ and this http://wiki.osdev.org/GCC_Cross-Compiler.
I don't know why, but I'm able to compile binutils, gmp, mpfr, mpc, but I fail when I try to compile gcc.
I know that on FreeBSD gcc is deprecated, is that the problem?
GCC including the latest 4.9.3 works great on FreeBSD. It is just not used as a system compiler. Do you know how to use ports?
 
Do you even know what cross compile mean?
Yes, I think.
A cross compiler is a compiler (with his standard lib) that compile on a system A for a system B (system as operative system, machine architecture, ecc.)
Am I right?

My intent is to build and host debug on Linux 64bit (so I don't need a gdb server) but run on a FreeBSD 64bit machine.

GCC including the latest 4.9.3 works great on FreeBSD. It is just not used as a system compiler. Do you know how to use ports?
No, but if you know a good reference I can try to learn.
With port I will be able to compile on Linux for FreeBSD?
(I was trying to build gcc from scratch)
Thank you! :)
 
Hi NickF_93 and welcome to the forums. The approach you have chosen is an unusal one. Can you say something about what you're trying to achieve? There may be an easier way to do it.

As Oko said, you can compile programs with GCC on FreeBSD, you just need to install it with pkg install lang/gcc. You can also debug programs with gdb. If you are using a makefile that has been written already, it might have been written for GNU Make. FreeBSD uses Berkeley make, which is similar but not the same. You can install devel/gmake with pkg install devel/gmake and use that if you need or prefer it.
 
Hi NickF_93 and welcome to the forums. The approach you have chosen is an unusal one. Can you say something about what you're trying to achieve? There may be an easier way to do it.

As Oko said, you can compile programs with GCC on FreeBSD, you just need to install it with pkg install lang/gcc. You can also debug programs with gdb. If you are using a makefile that has been written already, it might have been written for GNU Make. FreeBSD uses Berkeley make, which is similar but not the same. You can install devel/gmake with pkg install devel/gmake and use that if you need or prefer it.

Thank you for your reply, I would compile my own program. I have FreeBSD on virtualbox and I can compile my software there, I know. I would only learn how to build a cross compiler both to learn and to manage in a easier way my software.
However I used a cross compiler to build for raspberry but I never build one, and I thought that this can be the good occasion.
So, how can I achieve this?
Thank you
 
A web search brings back this article on using GCC to compile FreeBSD binaries on GNU/Linux*. Although it talks about FreeBSD 7, the principles should be the same. It might be more directly relevant than the links you mentioned in your first post. This isn't something I've done myself!

*Edit: Added the link
Yes this is the first guide I read, but I got and error during build process on last step and I thought that was a version problem, but I don't know exactly.
 
Please post the error you got. Maybe someone can help you solve it.
You are right!
I run:
../gcc-4.9.2/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
where TARGET=x86_64-freebsd
and then
make all-gcc
Code:
g++ -c  -g -O2 -DIN_GCC  -DCROSS_DIRECTORY_STRUCTURE  -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings  -DHAVE_CONFIG_H -DGENERATOR_FILE -I. -Ibuild -I../../gcc-4.9.2/gcc -I../../gcc-4.9.2/gcc/build -I../../gcc-4.9.2/gcc/../include  -I../../gcc-4.9.2/gcc/../libcpp/include  \
   -o build/genpreds.o ../../gcc-4.9.2/gcc/genpreds.c
In file included from ./tm.h:25:0,
  from ../../gcc-4.9.2/gcc/genpreds.c:26:
../../gcc-4.9.2/gcc/config/freebsd-spec.h:108:16: error: operator '<' has no left operand
#if FBSD_MAJOR < 5
  ^
../../gcc-4.9.2/gcc/config/freebsd-spec.h:130:16: error: operator '<' has no left operand
#if FBSD_MAJOR < 6
  ^
In file included from ./tm.h:29:0,
  from ../../gcc-4.9.2/gcc/genpreds.c:26:
../../gcc-4.9.2/gcc/config/i386/freebsd.h:144:16: error: operator '>=' has no left operand
#if FBSD_MAJOR >= 6
  ^
Makefile:2366: recipe for target 'build/genpreds.o' failed
make[1]: *** [build/genpreds.o] Error 1
make[1]: Leaving directory '/home/nick/Scaricati/build-gcc/gcc'
Makefile:3988: recipe for target 'all-gcc' failed
make: *** [all-gcc] Error 2
Here the last make log, where are errors.
 
Thanks tobik, perhaps I gave up too easily :)

It looks like FBSD_MAJOR isn't defined, so once the statement has been through GCC's preprocessor it becomes something invalid like:
Code:
#if < 5
I imagine GCC is expecting to be compiled on a system running FreeBSD. However, you should be able to define it yourself using the -Dname=definition syntax (see the GCC manual).

The makefile probably sets the CFLAGS variable. Edit it to add the following to the definition:
Code:
-DFBSD_MAJOR=10
Then try running your make command again.
 
Thanks tobik, perhaps I gave up too easily :)

It looks like FBSD_MAJOR isn't defined, so once the statement has been through GCC's preprocessor it becomes something invalid like:
Code:
#if < 5
I imagine GCC is expecting to be compiled on a system running FreeBSD. However, you should be able to define it yourself using the -Dname=definition syntax (see the GCC manual).

The makefile probably sets the CFLAGS variable. Edit it to add the following to the definition:
Code:
-DFBSD_MAJOR=10
Then try running your make command again.

I understood the problem:
The target need to be x86_64-freebsd<VERSION> (I set target x86_64-freebsd10.1). This resolve that problem, but now I get this:

Code:
make[3]: Leaving directory '/home/nick/Scaricati/build-gcc/x86_64-freebsd10.1/libgcc'
DEFINES='' HEADERS='' \
   ../../../gcc-4.9.2/libgcc/mkheader.sh > tmp-libgcc_tm.h
/bin/bash ../../../gcc-4.9.2/libgcc/../move-if-change tmp-libgcc_tm.h libgcc_tm.h
echo timestamp > libgcc_tm.stamp
/home/nick/Scaricati/build-gcc/./gcc/xgcc -B/home/nick/Scaricati/build-gcc/./gcc/ -B/usr/local/cross/x86_64-freebsd10.1/bin/ -B/usr/local/cross/x86_64-freebsd10.1/lib/ -isystem /usr/local/cross/x86_64-freebsd10.1/include -isystem /usr/local/cross/x86_64-freebsd10.1/sys-include  -g -O2 -O2  -g -O2 -DIN_GCC  -DCROSS_DIRECTORY_STRUCTURE  -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition  -isystem ./include  -fpic -pthread -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector -Dinhibit_libc  -fpic -pthread -I. -I. -I../.././gcc -I../../../gcc-4.9.2/libgcc -I../../../gcc-4.9.2/libgcc/. -I../../../gcc-4.9.2/libgcc/../gcc -I../../../gcc-4.9.2/libgcc/../include  -DHAVE_CC_TLS  -o _muldi3.o -MT _muldi3.o -MD -MP -MF _muldi3.dep -DL_muldi3 -c ../../../gcc-4.9.2/libgcc/libgcc2.c -fvisibility=hidden -DHIDE_EXPORTS
In file included from ../../../gcc-4.9.2/libgcc/../gcc/tsystem.h:44:0,
  from ../../../gcc-4.9.2/libgcc/libgcc2.c:27:
/home/nick/Scaricati/build-gcc/gcc/include/stddef.h:56:24: fatal error: sys/_types.h: No such file or directory
#include <sys/_types.h>
  ^
compilation terminated.
Makefile:463: recipe for target '_muldi3.o' failed
make[2]: *** [_muldi3.o] Error 1
make[2]: Leaving directory '/home/nick/Scaricati/build-gcc/x86_64-freebsd10.1/libgcc'
Makefile:12757: recipe for target 'all-target-libgcc' failed
make[1]: *** [all-target-libgcc] Error 2
make[1]: Leaving directory '/home/nick/Scaricati/build-gcc'
Makefile:869: recipe for target 'all' failed
make: *** [all] Error 2
as result of make command.
 
Solved! I didn't set up properly sysroot.

I had to dowload (or take it from a build and working system) the target system on my machine (in my case freebsd, so I got base.txz, lib32.txz and to be sure (but I think that they are not necessary) also src.txz and kernel.txz, and then I was able to set --with-sysroot in configuration process of binutils and gcc sources, and then run

$ make
and

# make install
for binutils and gcc and finally I got my toolchain!
 
Great! Did you manage to use that toolchain on your GNU/Linux box to compile a working FreeBSD binary yet?
Yes! I tried to compile my little project and it worked! Nothing advanced, it's a simple project.
It work great for compiling for a 64-bit Instruction Set arch, but it doesn't support multilib and I don't know why. I'm trying to build it with multilib support.
Some ideas?

****** EDIT ******
In general does freebsd FreeBSD support multilib?
 
Last edited by a moderator:
Back
Top