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

Craig Scott craig.scott at crascit.com
Wed Dec 27 08:35:44 EST 2017


On Wed, Dec 27, 2017 at 7:04 PM, LightGoLeft <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/6e00e6bc/attachment.html>


More information about the CMake mailing list