Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cross compile for linux x86 with linux amd64, cmake and g++?

+1 for each piece of information that helps to complete the whole picture. You don't need to know the whole answer. I'll appreciate individual pieces of the puzzle just as much. Thanks.

I am about to attempt my first cross-compilation. I have searched both SO and the web and found many pieces of information, but I don't always know how to put those pieces together because there are still some missing pieces.

My host: linux Kubuntu amd64.
Target: linux kubuntu x86 (32bit) (should be easy, no?)
Tools: g++ and cmake.

Here is the information I found:

How to compile a 32-bit binary on a 64-bit linux machine with gcc/cmake
mentions export CFLAGS=-m32. That's one piece.

Cross-platform: selecting data types to use 32/64 bit
mentions data types. I may have to pay attention to that within my code.

#ifdef for 32-bit platform
#ifdef for 32-bit platform
links to the following, although I am not too sure yet how to use it:
http://predef.sourceforge.net/prearch.html

http://ww.ubuntuforums.org/showthread.php?t=1377396
I did: sudo apt-get install g++-multilib

missing pieces:

Ideally, when I do 'make' (with cmake), it should spit out both a amd64 binary and a x86 one.

Part of my CMakeLists.txt looks like this:

add_definitions(-Wall -pthread) add_executable (../run.amd64 user.cpp time.cpp init.cpp utils.cpp main.cpp) target_link_libraries(../run.amd64 cppcms dbixx config++ ctemplate) 

How do I introduce the flag -m32 to create a second executable?

Should I want to make only one executable (e.g. for testing and debugging), how do I tell cmake to make either one or both binaries?

Also, you can see that I use some third party libraries, some of which I had to compile myself. Does this mean that I need to compile each of those binaries for the target host as well? Some use cmake and some use: ./configure; make;
How would I do about compiling those libraries for the target host (flags to use, etc.)?

Note: the dynamically linked libraries are already compiled and installed on the target computer, so maybe I don't need to worry about this step... I am not sure: this is one of my missing pieces...

What I need is a kind of tutorial, or at least some of the missing pieces. I'll update this post with more details on what I achieved and how.

Thanks.

P.S.

Is it possible at all?

Searching more, I found this:
http://www.mail-archive.com/[email protected]/msg26265.html
"The original design doesn't seem to be designed for anything more than windows-linux or linux-windows cross compiles."
cmake is NOT tested for linux amd64 to linux x86.

http://www.cmake.org/Wiki/CMake_Cross_Compiling#FAQ.2FPotential_Problems
"On mixed 32/64 bit Linux installations cross compilation cannot be used to build for 32/64 bit only."

??

like image 707
augustin Avatar asked Nov 09 '10 06:11

augustin


People also ask

Does CMake use gcc or G ++?

Usually under Linux, one uses CMake to generate a GNU make file which then uses gcc or g++ to compile the source file and to create the executable.


1 Answers

If you want to use a toolchain file there is an easier solution (IMHO) than what is proposed by @auledoom. You do not need to write the shell wrapper scripts at all, simply put this in the toolchain file:

# the name of the target operating system set(CMAKE_SYSTEM_NAME Linux)  # Which compilers to use for C and C++ set(CMAKE_C_COMPILER gcc -m32) set(CMAKE_CXX_COMPILER g++ -m32) 

This will make it a "list variable" in cmake. This solution works for me. Benefit of the toolchain file is that you can there also define paths for 32bit libraries etc, which is usually different from standard paths.

like image 183
yngve Avatar answered Oct 17 '22 15:10

yngve