2

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.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
orayan
  • 165
  • 2
  • 9
  • 4
    Possible duplicate of [Qt static linking and deployment](https://stackoverflow.com/questions/1011197/qt-static-linking-and-deployment) – KYL3R May 03 '18 at 11:32
  • @KYL3R I am using cmake and my question not Qmake . – orayan May 03 '18 at 12:35
  • In this example, your question is limited to Qt. Qt is a bit special story in regards to static linking, so I'd advise you to go official path. – Andrejs Cainikovs May 03 '18 at 12:41

1 Answers1

7

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!

Sergio Monteleone
  • 2,776
  • 9
  • 21
  • Perfect answer sir. One question I have is, am I violating LGPL licence if I copy all shared/dynamic libraries shown in the result of ldd and then re-link the project with with -Wl,-rpath,. (executable looks for all .so libraries in the current directory, no need to set the LD_LIBRARY_PATH)? I mean there are some .so files that don't appear to be part of Qt framework (no ^libqt names in some of them)? – falero80s Dec 31 '22 at 19:54
  • 1
    IANAL, but I think that as long as you are leaving some options to the end user to easily replace the Qt ibraries (in this case, they could replace the .so files in the directory) you should be fine. – Sergio Monteleone Jan 04 '23 at 20:20