简体   繁体   中英

How to change build machine type with CMake

I am learning how to use CMake,

I am converting a working project from Visual Studio to Cmake and building with NMake. The current project builds for both x64 and x86. My OS is windows 10

My current CMakeLists.txt is very simple:

cmake_minimum_required(VERSION 3.13.0)

project(example CXX)

ADD_DEFINITIONS(-DUNICODE)
ADD_DEFINITIONS(-D_UNICODE)

set(CMAKE_CXX_FLAGS -G"NMake Makefiles")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(SimManager
    Source/main.cpp
    )

set_target_properties (${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED TRUE
    CXX_EXTENSIONS FALSE
    )

The commands I'm using to build are: cmake -G"NMake Makefiles.. nmake

The error I get is

fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'x86'

I understand the way NMake chooses which compiler to use, x86 or x64, is by opening the appropriate console terminal. I am using Select x64 Native Tools Command Prompt for VS 2017

The command line for the linker that gets executed is:

command "C:\PROGRA~2\MIB055~1\2019\ENTERP~1\VC\Tools\MSVC\1425~1.286\bin\Hostx86\x86\link.exe /nologo @CMakeFiles\example.dir\objects1.rsp /out:SimManager.exe /implib:SimManager.lib /pdb:E:\Projects\example\Debug\example.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\example.dir/intermediate.manifest CMakeFiles\example.dir/manifest.res" failed (exit code 1112) with the following output: fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'x86'

I can see it has /machine:X86 set in it. I am not setting this in CMakeLists.txt. How can I make it use a 64 bit linker?

I tried adding suggested arguments to the cmake call, it did not make a difference

cmake -G"NMake Makefiles" --build build64 --config Release -host_arch=amd64 -arch=amd64..

Also tried adding to CMakeLists, same error is still there

set(CMAKE_SYSTEM_PROCESSOR AMD64)
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m64" LINK_FLAGS "-m64")
set(ARCH amd64)

I would prefer to make the changes in the CMakeLists.txt if possible, so I can make the command line calls as show as possible. This project is not intended to build for 32 bit.

Thanks

Try adding the following in the CMakeLists.txt :

target_link_options(target PRIVATE /machine:x64)

See documentation .

Or you can edit the CMakeCache.txt and change the following variable:

CMAKE_MODULE_LINKER_FLAGS:STRING=/machine:x64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM