[CMake] CMAKE_SIZEOF_VOID_P did not set correctly by TDM-GCC -m32 argument

Hao Jiang drakedog2008 at outlook.com
Wed Dec 27 22:13:10 EST 2017


Hi Scott,

Thanks for the reply. It worked.

Hao.
From: Craig Scott<mailto:craig.scott at crascit.com>
Sent: 2017年12月27日 21:35
To: LightGoLeft<mailto:drakedog2008 at outlook.com>
Cc: CMake<mailto:cmake at cmake.org>
Subject: Re: [CMake] CMAKE_SIZEOF_VOID_P did not set correctly by TDM-GCC -m32 argument



On Wed, Dec 27, 2017 at 7:04 PM, LightGoLeft <drakedog2008 at outlook.com<mailto:drakedog2008 at outlook.com>> wrote:
Hello,

I am currently using TDM-GCC-64 (MinGW) and MSVC to build binaries on 64 bit
Windows to build both 32 and 64bit binaries. However, I found architecture
passed to GNUtoMS bat is determined by CMAKE_SIZEOF_VOID_P which is not
correctly set.

For GCC, I set
  SET(CMAKE_CXX_FLAGS "-m32 ${CMAKE_CXX_FLAGS}")
  SET(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}")
But, message(${CMAKE_SIZEOF_VOID_P }) is always 8. That causes GNUtoMS
generating x64 *.lib from x86 *.a. I don't know whether this is a bug or by
design.

CMake determines the bitness of the target when it does its compiler checks, which is performed at the first project() command it hits, and it caches the result for all subsequent CMake invocations. If you need to pass a flag like -m32 to build for 32-bit targets on a 64-bit host, you need to use a toolchain file and specify the -m32 option in there. If you simply add lines like your example above to your project's CMakeLists.txt file, they won't be picked up properly when CMake does its compiler tests. The correct way to specify this sort of flag in a toolchain file would look something like this (note the use of ..._INIT variables):

set(CMAKE_C_FLAGS_INIT   -m32)
set(CMAKE_CXX_FLAGS_INIT -m32)

set(CMAKE_EXE_LINKER_FLAGS_INIT    -m32)
set(CMAKE_MODULE_LINKER_FLAGS_INIT -m32)
set(CMAKE_SHARED_LINKER_FLAGS_INIT -m32)

Your toolchain file should also be setting CMAKE_SYSTEM_NAME, CMAKE_SYSTEM_VERSION and CMAKE_SYSTEM_PROCESSOR to whatever is appropriate for your target architecture/platform (sorry, I'm not sure off the top of my head what these would typically be for your case). Be sure to clear any previous build if you want to experiment with this, since CMake caches its results after the first run.

--
Craig Scott
Melbourne, Australia
https://crascit.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20171228/147b3af2/attachment.html>


More information about the CMake mailing list