PDA

View Full Version : Qt5 CMake include all libraries into executable



Vildnex
25th March 2020, 18:03
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

----------

13361

----------

**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:**


cmake_minimum_required(VERSION 3.5)

project(Scrollable LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -fPIC")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 REQUIRED Core Widgets Gui Qml Quick Qml)

qt5_add_resources(resource.qrc)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories("MoviesInterface")

set(SOURCES
main.cpp
MovieInterface/movieinterfaceomdb.cpp
MovieInterface/moviesinterface.cpp
)

set(HEADERS
MovieInterface/movieinterfaceomdb.h
MovieInterface/moviesinterface.h
)


add_executable(Scrollable ${SOURCES} ${HEADERS} qml.qrc)
qt5_use_modules(Scrollable Core Network)
target_link_libraries(Scrollable
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Qml
${ADDITIONAL_LIBRARIES} -static
)

d_stranz
25th March 2020, 20:01
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.