Search code examples
c++qtstatic-linking

How to statically link Qt libraries?


I want to statically link Qt libraries to my program so that It can run in my school computer.We don't have purmision to install anything on those computer thus statically linking is my only chance .

So far this is my cmake file

cmake_minimum_required(VERSION 3.9)

project(Calculator)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -Wall -Wpedantic -Wextra -std=gnu++14 -no-pie -fPIC  -static -lQt5Widgets -lQt5Gui -lQt5Core")

find_package(Qt5Widgets REQUIRED)
include_directories(/usr/include/qt/QtWidgets /usr/include/qt /usr/include/qt/QtGui /usr/include/qt   /usr/include/qt/QtCore /usr/include/qt)

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

add_executable(${PROJECT_NAME} main.cpp calculator.cpp  resources.qrc calculatestring.cpp )

here is what I get when I try to run this cmake file:-

/bin/ld: cannot find -lQt5Widgets
/bin/ld: cannot find -lQt5Gui
/bin/ld: cannot find -lQt5Core

if I remove -static flag ,it compiles fine but only runs on machine on which qt is installed.

when I try to run it on my VirtualMachine with Arch Linux which doesn't have qt installed it give me error:-

error while loading shared libraries : libQt5Widgets.so.5: cannot open shared file: No such file or directory

I want(have no other choice) to run my program run without installing Qt on those machines .

Edit:-

Question likened as Possible Duplicate shows how to link with Qmake. I am using cmake .Also my question is not limited to the Qt.


Solution

  • I assume from your answer you are developing on Linux.

    The problem you are facing does not depend on the building system (cmake, qmake, or else).

    To link your program to a static version of a library you need that library on your development machine. In your case, you have the dynamic version of the library (the .so file), but you don't have the static one (the .a). Hence, when you try to link statically, the linker (ld) says it can't find the file.

    For licensing issues, Qt open source binaries are distributed only as dynamic libraries. If you need to link Qt statically, you have to build it yourself. I invite you to read the official documentation here if you are interested in attempting this.

    However, you may not need to link statically, if your problem is just to get the executable on the target machine without installing extra packages.

    You can deploy your application copying the executable and the .so files needed to the target machine, the same way you would copy an .exe file together with all the .dll files needed, if you were running on Windows.

    You can use the command ldd to obtain the list of dynamic libraries your executable needs.

    However, Linux works in a different way and one can not simply copy over the .so files. You must provide the loader the path where to look for .so files. This can be done setting the LD_LIBRARY_PATH enviromental variable. You will find plenty of examples on the internet on how to set this variable correctly.

    Alternatively, you may also consider to build a snap package, if this is allowed on your target machine.

    Good luck with your school project!