I have been trying to create a cross-platform project. Both 32 bit and 64 bit binaries (dlls) need to be built. I am using Visual Studio and selecting x86-Release or x64-Release solves the problem for windows.
But for linux it is always building 64 bit binaries.
After searching, i found that g multilib needs to be installed and -m32 flag needs to be passed to both the compiler and linker. I found that
set_target_properties(PROJECT_NAME PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
will solve the problem.
So to build a 32 bit linux binary (MODULE with .so ending), i added the above code.
I also have this code beneath the above code
if(WIN32)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
message("WIN32 x64")
else()
message("WIN32 x32")
endif()
else(WIN32)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
message("LINUX x64")
else()
message("LINUX x32")
endif()
endif(WIN32)
However, it outputs
1> [CMake] LINUX x64
So it is still building 32 bit build.
I have both g and g multilib installed, but not sure visual studio is calling which one. could that be the problem?
CodePudding user response:
As mentioned in comments, it was found that CMAKE_SIZEOF_VOID_P is not able to detect if m32 flag is passed. So i modified the code like this
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(OUTPUT_BITNESS 64)
else()
set(OUTPUT_BITNESS 32)
endif()
if(FORCE_32)
set_target_properties(PROJECT_NAME PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
set(OUTPUT_BITNESS 32)
endif()
if(WIN32)
if(OUTPUT_BITNESS EQUAL 64)
message("WIN32 x64")
else()
message("WIN32 x32")
endif()
else(WIN32)
if(OUTPUT_BITNESS EQUAL 64)
message("LINUX x64")
else()
message("LINUX x32")
endif()
endif(WIN32
So to compile for 32 bit, FORCE_32 variable must be set to 1.