PDA

View Full Version : cmake specify arch



tuli
24th September 2021, 02:55
Hi,

I am building a library project in pure cmake, as generated by qtcreator wizzard:


cmake_minimum_required(VERSION 3.14)

project(testlib LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)

add_library(testlib SHARED
testlib_global.h
testlib.cpp
testlib.h
)

target_link_libraries(testlib PRIVATE Qt${QT_VERSION_MAJOR}::Core)

target_compile_definitions(testlib PRIVATE TESTLIB_LIBRARY)


Now I want to compile it from command line without qtcreator.
I do


cmake ..
cmake --build .

But it tries to link the x86 build of qt into a x64 target, which fails. I have to add the following line, then it works:


set(CMAKE_PREFIX_PATH "c:\qt\msvc2019_x64")

My question: What is the correct way to specify the arch of the Qt library to be linked with cmake? What is the correct way to specify the arch for my library? (Both from pure commandline and not from within Qtcreator...)


Also ... I dont really understand how cmake can find the x86 version of Qt. How does it even know it exists?

d_stranz
24th September 2021, 17:08
Also ... I dont really understand how cmake can find the x86 version of Qt. How does it even know it exists?

The magic is in the find_package() scripts. Inside the cmake installation you will find other scripts that this one calls on to locate the Qt installation (like FindQt5.cmake, etc.).

Check out the documentation for find_package() (https://cmake.org/cmake/help/latest/command/find_package.html?highlight=find_package). You may have to specifiy the CMAKE_LIBRARY_ARCHITECTURE value or set the FIND_LIBRARY_USE_LIB64_PATHS variable to true. These are used internally in the find_package() scripts to locate the correct versions of libraries.

If you use a CMake generator to create your makefile / project file, you can pass a "TARGET_ARCH" variable on the command line. For example, something from one of my batch files that builds 4 different configurations (32bit and 64bit x debug and release) for two different MSVC versions (2013 and 2015):



# corresponds to %%G below
set generator=VS2015

set thisgenerator="Visual Studio 14 2015 Win64"

# corresponds to %%M below
set build = Release

set machine=amd64

# passed in as assignment to TARGET_ARCH
set arch=x64

cmake -G!thisgenerator! -DCMAKE_TOOLCHAIN_FILE=windows_toolchain.cmake -DBUILD_TYPE=%%M -DVSGEN_TYPE=%%G -DTARGET_ARCH=!arch! ..\


The toolchain file just sets up a bunch of CMake variables used by all of the Windows builds:



# target operating system
set (CMAKE_SYSTEM_NAME Windows)
set (WINDOWS true)

set (CMAKE_BUILD_TYPE ${BUILD_TYPE} CACHE STRING "Choose the type of build" FORCE)
set (CMAKE_GEN_TYPE ${VSGEN_TYPE} CACHE STRING "Visual Studio generator type" FORCE)
set (CMAKE_CXX_FLAGS "/Gs- /Gd /Zc:wchar_t /D WIN32 /EHsc /D UNICODE /D _UNICODE /D _WINDLL /D XERCES_STATIC_LIBRARY /D _USRDLL")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags" )


Hope this is of some use. CMake is an extremely powerful build system, but the online documentation can be impenetrable. There is a pretty good book from Kitware (who wrote CMake) called "Mastering CMake" (https://www.amazon.com/Mastering-CMake-Ken-Martin/dp/1930934319) which I found to be very helpful in creating my build scripts.