Results 1 to 2 of 2

Thread: Qt5 CMake include all libraries into executable

  1. #1
    Join Date
    Mar 2020
    Qt products
    Unix/X11 Windows

    Default Qt5 CMake include all libraries into executable

    I'm trying to build a with Qt 5.14 an application on release mode and everything is working fine inside of Qt Creator, but when I'm trying to run the executable by itself I'm getting an error like this:


    **OS:** Windows 10

    **Qt:** 5.14

    **Cmake:** 3.5




    **What I've tried:**

    1. set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
    2. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -fPIC")
    3. ${ADDITIONAL_LIBRARIES} -static inside of target_link_libraries

    None of the above worked for me and I'm getting the same error whenever I'm trying to run the executable by its self without using Qt Creator.


    **My CMake file:**

    Qt Code:
    1. cmake_minimum_required(VERSION 3.5)
    3. project(Scrollable LANGUAGES CXX)
    7. set(CMAKE_AUTOUIC ON)
    8. set(CMAKE_AUTOMOC ON)
    9. set(CMAKE_AUTORCC ON)
    11. set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
    12. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -fPIC")
    13. set(CMAKE_CXX_STANDARD 14)
    16. find_package(Qt5 REQUIRED Core Widgets Gui Qml Quick Qml)
    18. qt5_add_resources(resource.qrc)
    20. include_directories(${CMAKE_CURRENT_SOURCE_DIR})
    21. include_directories("MoviesInterface")
    23. set(SOURCES
    24. main.cpp
    25. MovieInterface/movieinterfaceomdb.cpp
    26. MovieInterface/moviesinterface.cpp
    27. )
    29. set(HEADERS
    30. MovieInterface/movieinterfaceomdb.h
    31. MovieInterface/moviesinterface.h
    32. )
    35. add_executable(Scrollable ${SOURCES} ${HEADERS} qml.qrc)
    36. qt5_use_modules(Scrollable Core Network)
    37. target_link_libraries(Scrollable
    38. Qt5::Core
    39. Qt5::Gui
    40. Qt5::Widgets
    41. Qt5::Qml
    42. ${ADDITIONAL_LIBRARIES} -static
    43. )
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 816 Times in 805 Posts
    Qt products
    Windows Android

    Default Re: Qt5 CMake include all libraries into executable

    Have you read the section on Deployment in the Qt documentation?

    Running in a development environment (like Qt Creator or Visual Studio) works because the IDE has set up the paths to the DLLs and other binaries required to build and run programs. In a standalone environment, if the DLLs are not searchable via your PATH variable or are not installed as described in the Deployment documentation, the program will not run and will give the error you are seeing.

    All the TARGET_LINK_LIBRARIES() command in your CMakelists file is doing is telling CMake to link your program with the -import libraries- (.lib) to resolve any symbols that will later have to be located in the DLLs that are loaded at runtime. It has nothing to do with telling the executable where to find those DLLs at run time.

    If you specify an "install()" command in your CMakelists file, you can use it to deploy your executable and the DLLs it needs to execute. You can then run it standalone from a command prompt after you have CD'd to that directory.
    Last edited by d_stranz; 25th March 2020 at 20:16.
    <=== 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.

Similar Threads

  1. Replies: 4
    Last Post: 6th March 2019, 20:57
  2. include cmake inside qmake
    By shenakan in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2011, 15:15
  3. how set icon for qt executable project file with cmake and vs 2005?
    By banlinhtienphong in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2011, 08:38
  4. Qt + Eclipse + MinGW + Windows: How to include libraries?
    By jambrek in forum General Programming
    Replies: 3
    Last Post: 19th December 2007, 13:57
  5. Replies: 2
    Last Post: 30th September 2007, 21:23

Tags for this Thread


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.