Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The proper way of forcing a 32-bit compile using CMake

Sorry that there are many similar questions, but I do find that Googling for CMake queries always yields similar-but-not-the-same scenarios, conflicting CMake commands and so on!

I need to force my project to build 32-bit binaries because I have to link with a library which is only available as 32-bit. I diagnosed this based on error messages such as:

/usr/bin/ld: i386 architecture of input file `*external-32bit-lib*' is incompatible with i386:x86-64 output 

From what I gather, I should therefore use:

set (CMAKE_CXX_FLAGS "-m32") 

This does change things - I now get several errors like:

/usr/bin/ld: i386 architecture of input file `*project-output-lib*' is incompatible with i386:x86-64 output 

AND still get the same errors for the external library too. I think this is because the -m32 made gcc generate 32-bit binaries, but ld is still trying for 64-bit output? Further Googling for this problem didn't give any success, so if anyone could verify that I am right and give the correct way of doing this, I would be very grateful!

Many thanks!

like image 762
devrobf Avatar asked Apr 27 '11 14:04

devrobf


People also ask

Is CMake 32 bit?

Kitware provides cmake-XXX-Linux-x86_64.sh, but no 32-bit equivalent. The cmake binary in their archive can run on any combination of 64-bit Linux OS and kernel that I am aware of.


1 Answers

If you want to compile and link for 32 bit using cmake use this for creating libraries and binaries:

Creating libraries:

add_library(mylib SHARED my_source.c) set_target_properties(mylib PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") 

creating executables:

add_executable(mybin sources.c) set_target_properties(mybin PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") 
like image 77
ant2009 Avatar answered Sep 19 '22 17:09

ant2009