Results 1 to 2 of 2

Thread: cmake specify arch

  1. #1
    Join Date
    Jul 2012
    Posts
    234
    Thanks
    22
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default cmake specify arch

    Hi,

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

    Qt Code:
    1. cmake_minimum_required(VERSION 3.14)
    2.  
    3. project(testlib LANGUAGES CXX)
    4.  
    5. set(CMAKE_INCLUDE_CURRENT_DIR ON)
    6. set(CMAKE_AUTOUIC ON)
    7. set(CMAKE_AUTOMOC ON)
    8. set(CMAKE_AUTORCC ON)
    9. set(CMAKE_CXX_STANDARD 11)
    10. set(CMAKE_CXX_STANDARD_REQUIRED ON)
    11.  
    12. find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
    13. find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)
    14.  
    15. add_library(testlib SHARED
    16. testlib_global.h
    17. testlib.cpp
    18. testlib.h
    19. )
    20.  
    21. target_link_libraries(testlib PRIVATE Qt${QT_VERSION_MAJOR}::Core)
    22.  
    23. target_compile_definitions(testlib PRIVATE TESTLIB_LIBRARY)
    To copy to clipboard, switch view to plain text mode 


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

    Qt Code:
    1. cmake ..
    2. cmake --build .
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. set(CMAKE_PREFIX_PATH "c:\qt\msvc2019_x64")
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,039
    Thanks
    282
    Thanked 820 Times in 809 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: cmake specify arch

    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(). 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):

    Qt Code:
    1. # corresponds to %%G below
    2. set generator=VS2015
    3.  
    4. set thisgenerator="Visual Studio 14 2015 Win64"
    5.  
    6. # corresponds to %%M below
    7. set build = Release
    8.  
    9. set machine=amd64
    10.  
    11. # passed in as assignment to TARGET_ARCH
    12. set arch=x64
    13.  
    14. cmake -G!thisgenerator! -DCMAKE_TOOLCHAIN_FILE=windows_toolchain.cmake -DBUILD_TYPE=%%M -DVSGEN_TYPE=%%G -DTARGET_ARCH=!arch! ..\
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. # target operating system
    2. set (CMAKE_SYSTEM_NAME Windows)
    3. set (WINDOWS true)
    4.  
    5. set (CMAKE_BUILD_TYPE ${BUILD_TYPE} CACHE STRING "Choose the type of build" FORCE)
    6. set (CMAKE_GEN_TYPE ${VSGEN_TYPE} CACHE STRING "Visual Studio generator type" FORCE)
    7. set (CMAKE_CXX_FLAGS "/Gs- /Gd /Zc:wchar_t /D WIN32 /EHsc /D UNICODE /D _UNICODE /D _WINDLL /D XERCES_STATIC_LIBRARY /D _USRDLL")
    8. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags" )
    To copy to clipboard, switch view to plain text mode 

    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" which I found to be very helpful in creating my build scripts.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    tuli (25th September 2021)

Similar Threads

  1. Qt Raspberry Pi2 - Linux Arch
    By mayrhofer in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 21st January 2016, 09:49
  2. Replies: 2
    Last Post: 23rd October 2015, 13:29
  3. no debug symbols in executable (-arch x86_64)
    By bruce2011 in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2011, 14:42
  4. Plugin Arch Questions
    By ComaWhite in forum KDE Forum
    Replies: 1
    Last Post: 1st September 2009, 21:08
  5. Compilation app sous +ieur OS et arch
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2008, 18:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.