Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [CMake] Linking errors using MSVC
Forum Updated to NodeBB v4.3 + New Features

[CMake] Linking errors using MSVC

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dgsham

    @Christian-Ehrlicher Thanks for your answer. You're right, this shows up in the MVSC 2017 logs. However, the MSVC 2019 logs show that it's indeed using x64.

    Just to confirm, I did the following :

    C:\Users\rsd.DIGIDOM\dev\qt-cmake> cmake -B build . -G "Visual Studio 15 2017" -T "host=x64" -A "x64"                                                            
    -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.                                                                                      
    -- The CXX compiler identification is MSVC 19.16.27045.0                                                                                                         
    -- Detecting CXX compiler ABI info                                                                                                                               
    -- Detecting CXX compiler ABI info - done                                                                                                                        
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe - skipped   
    -- Detecting CXX compile features                                                                                                                                
    -- Detecting CXX compile features - done                                                                                                                         
    -- Configuring done                                                                                                                                              
    -- Generating done                                                                                                                                               
    -- Build files have been written to: C:/Users/rsd.DIGIDOM/dev/qt-cmake/build                                                                                     
    

    But I still get the same link errors.

    EDIT : And to clarify, I delete the whole build directory between each test

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @dgsham said in [CMake] Linking errors using MSVC:

    But I still get the same link errors.

    Because you still pick u the wrong Qt libs. Your cmake output does not show that Qt is searched at all - so how to you link against Qt?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    D 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @dgsham said in [CMake] Linking errors using MSVC:

      But I still get the same link errors.

      Because you still pick u the wrong Qt libs. Your cmake output does not show that Qt is searched at all - so how to you link against Qt?

      D Offline
      D Offline
      dgsham
      wrote on last edited by
      #5

      @Christian-Ehrlicher You got a point, but in the MinGW logs the Qt5 search does not show up either, and it links fine.

      As another test I did this :

      C:\Users\rsd.DIGIDOM\dev\qt-cmake> cmake -B build . -G "Visual Studio 15 2017" -T "host=x64" -A "x64"
      -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
      -- The CXX compiler identification is MSVC 19.16.27045.0
      -- Detecting CXX compiler ABI info
      -- Detecting CXX compiler ABI info - done
      -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe - skipped
      -- Detecting CXX compile features
      -- Detecting CXX compile features - done
      -- Qt5_FOUND="1"
      -- Configuring done
      -- Generating done
      -- Build files have been written to: C:/Users/rsd.DIGIDOM/dev/qt-cmake/build
      

      It looks like the Qt5 libs are properly discovered. Here is the complete CMakeLists.txt file. The only modification I have made is the Qt5_FOUND variable printing :

      cmake_minimum_required(VERSION 3.5)
      
      project(qt-cmake VERSION 0.1 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 Widgets REQUIRED)
      find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
      
      include(CMakePrintHelpers)
      cmake_print_variables(Qt5_FOUND)
      
      set(PROJECT_SOURCES
              main.cpp
              mainwindow.cpp
              mainwindow.h
              mainwindow.ui
      )
      
      if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
          qt_add_executable(qt-cmake
              MANUAL_FINALIZATION
              ${PROJECT_SOURCES}
          )
      # Define target properties for Android with Qt 6 as:
      #    set_property(TARGET qt-cmake APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
      #                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
      # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
      else()
          if(ANDROID)
              add_library(qt-cmake SHARED
                  ${PROJECT_SOURCES}
              )
      # Define properties for Android with Qt 5 after find_package() calls as:
      #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
          else()
              add_executable(qt-cmake
                  ${PROJECT_SOURCES}
              )
          endif()
      endif()
      
      target_link_libraries(qt-cmake PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
      
      set_target_properties(qt-cmake PROPERTIES
          MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
          MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
          MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
      )
      
      if(QT_VERSION_MAJOR EQUAL 6)
          qt_finalize_executable(qt-cmake)
      endif()
      
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #6

        As I said above - make sure the correct qmake.exe is picked up by cmake .

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          As I said above - make sure the correct qmake.exe is picked up by cmake .

          D Offline
          D Offline
          dgsham
          wrote on last edited by
          #7

          @Christian-Ehrlicher You are absolutely right, the find_package command is trying to use the MinGW libs. I could find out about this by using cmake --trace, here is an extract :

          C:/Users/rsd.DIGIDOM/dev/qt-cmake/CMakeLists.txt(14):  find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5ConfigVersion.cmake(2):  set(PACKAGE_VERSION 5.15.2 )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5ConfigVersion.cmake(4):  if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5ConfigVersion.cmake(6):  else()
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5ConfigVersion.cmake(7):  set(PACKAGE_VERSION_COMPATIBLE TRUE )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5ConfigVersion.cmake(8):  if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5Config.cmake(2):  if(CMAKE_VERSION VERSION_LESS 3.1.0 )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5Config.cmake(6):  if(NOT Qt5_FIND_COMPONENTS )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5Config.cmake(7):  set(Qt5_NOT_FOUND_MESSAGE The Qt5 package requires at least one component )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5Config.cmake(8):  set(Qt5_FOUND False )
          C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5Config.cmake(9):  return()
          

          But I can't find how to force the use of the MSVC libs. Do you know where I can read some documentation about this ?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #8

            Modify your PATH so the correct qmake is found. The path to qmake is also stored in CMakeCache.txt: QT_QMAKE_EXECUTABLE

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              Modify your PATH so the correct qmake is found. The path to qmake is also stored in CMakeCache.txt: QT_QMAKE_EXECUTABLE

              D Offline
              D Offline
              dgsham
              wrote on last edited by
              #9

              @Christian-Ehrlicher You nailed it, thanks.

              The problem is that I previously configured my system PATH to point to C:/Qt/5.15.2/mingw81_64/bin/. I tried adding C:/Qt/5.9.9/msvc2017_64/bin/, moving it up before the mingw entry, and rebooting : it didn't work. I had to remove the mingw entry for it to work.

              Does that mean that there is no way to use several versions of the Qt libs on the same system ? Even if both versions use the same compiler, say if I want one project to point to 5.9, and the other to 5.15, is there a way to do that ?

              Thanks a lot for your help.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #10

                Simply don't add any of those paths to your default PATH. Set up two batch files which adds the appropriate path and call them. Or Use QtCreator and add the different Qt versions there - it will automatically switch the paths when you switch the Kit.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                D 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  Simply don't add any of those paths to your default PATH. Set up two batch files which adds the appropriate path and call them. Or Use QtCreator and add the different Qt versions there - it will automatically switch the paths when you switch the Kit.

                  D Offline
                  D Offline
                  dgsham
                  wrote on last edited by
                  #11

                  By googling after your answer, I think I found an even better solution : set the CMAKE_PREFIX_PATH variable to point to C:/Qt/5.9.9/msvc2017_64/lib/cmake/. This can be done in CMakeLists.txt, and probably in CMakePresets.json too.

                  This is actually said in the documentation, but it could be more clear in my opinion : https://doc.qt.io/qt-5/cmake-get-started.html

                  For find_package to be successful, CMake must find the Qt installation in one of the following ways:
                  
                      Set your CMAKE_PREFIX_PATH environment variable to the Qt 5 installation prefix. This is the recommended way.
                  

                  I think it needs to be emphasized that :

                  • It needs to point to the lib/cmake subdirectory
                  • It can be used to make several projects point to several different versions

                  Thanks again for your help !

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #12

                    But then you hard-code your path to Qt in your CMakeLists.txt which is not portable at all and you won't be able to run the applications because the PATH is wrong and therefore the DLLs are not found.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2
                    • D Offline
                      D Offline
                      dgsham
                      wrote on last edited by dgsham
                      #13

                      Oh right, thanks for the heads up.

                      EDIT : So the best solution I think would be to use the PATH by default, or set CMAKE_PREFIX_PATH through CMakeUserPresets.json on another machine if necessary

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved