[CMake] Configure 32 bit build in 64 bit environment from cli

Martin Koller martin.koller at etm.at
Thu Mar 28 14:19:49 EDT 2013


On Wednesday 27 March 2013 10:37:12 Nicola Mori wrote:
> Hello, for my project I'd like to be able to configure a 32 bit build 
> with GCC in a 64 bit Linux environment by setting variables from command 
> line interface. As far as I understand of CMake, this can be 
> accomplished by setting CMAKE_CXX_FLAGS and CMAKE_SHARED_LINKER_FLAGS to 
> "-m32" (my project consists only of a library). So I configure with:
> 
> cmake -DCMAKE_CXX_FLAGS=-m32 -DCMAKE_SHARED_LINKER_FLAGS=-m32 /path/to/code
> 
> and indeed the cached variables reflect my settings:
> 
> $ cmake -i /path/to/code
> 
> Variable Name: CMAKE_CXX_FLAGS
> Description: Flags used by the compiler during all build types.
> Current Value: -m32
> 
> Variable Name: CMAKE_SHARED_LINKER_FLAGS
> Description: Flags used by the linker during the creation of dll's.
> Current Value: -m32
> 
> 
> So everything seems fine, and then I launch make. I immediately get this 
> error message:
> 
> /usr/bin/ld: i386:x86-64 architecture of input file 
> `CMakeFiles/GGSNameDecoder.dir/GGSNameDecoder.cpp.o' is incompatible 
> with i386 output
> 
> So it seems that the linker is trying to create a 32 bit library using a 
> 64 bit object file. Indeed, the object file is 64 bit:
> 
> $ file src/CMakeFiles/GGSNameDecoder.dir/GGSNameDecoder.cpp.o
> src/CMakeFiles/GGSNameDecoder.dir/GGSNameDecoder.cpp.o: ELF 64-bit LSB 
> relocatable, x86-64, version 1 (SYSV), not stripped
> 
> So I would say that the setting of CMAKE_CXX_FLAGS is ignored while 
> CMAKE_SHARED_LINKER_FLAGS is correctly accounted for. I tried to set 
> also CMAKE_C_FLAGS to -m32 without success, anyway my code is C++. 
> Interestingly, if I hard-code the -m32 into my CMakeLists.txt like this:
> 
> set(CMAKE_CXX_FLAGS "-m32")
> 
> then the 32 bit build works smoothly.
> I get this behavior both with CMake 2.8.7 and 2.8.10. I don't know if I 
> am missing something, if it is a bug in CMake or in my project, or if it 
> is the intended behavior. I only know that I would need some help to get 
> through it, thanks!

I'm doing this with a toolchain file, so that cmake sees this as cross-compiling.

E.g. I have the following toolchain file "linux_i686.toolchain.cmake":

# toolchain file for building a 32bit version on a 64bit host

# use it like this:
# cmake -DCMAKE_TOOLCHAIN_FILE=linux_i686.toolchain.cmake <sourcedir>

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR "i686")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32" CACHE STRING "c++ flags")
set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -m32" CACHE STRING "c flags")

-- 
Best regards/Schöne Grüße

Martin

A: Because it breaks the logical sequence of discussion
Q: Why is top posting bad?

()  ascii ribbon campaign - against html e-mail 
/\  www.asciiribbon.org   - against proprietary attachments

This mail was not scanned before sending.
It was sent from a secure Linux desktop.


More information about the CMake mailing list