简体   繁体   English

使用 CMake 分发 Qt5 控制台应用程序

[英]Distribute a Qt5 console application using CMake

I fruitlessly tried to distribute a basic Qt console application by using CMake.我徒劳地尝试使用 CMake 分发一个基本的 Qt 控制台应用程序。

Let's consider these 2 files:让我们考虑这两个文件:

main.cpp主程序

#include <QDebug>

int main(int argc, char *argv[])
{
    qDebug() << "Hello Wolrd!";
    return 0;
}

CMakeLists.txt CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(HelloWorld)

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Widgets REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

qt5_use_modules(${PROJECT_NAME} Widgets)

Next I compile the main.cpp file doing:接下来我编译main.cpp文件:

$ cmake .

-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/HelloWorld

$ make

Scanning dependencies of target HelloWorld_autogen
[ 25%] Automatic MOC for target HelloWorld
[ 25%] Built target HelloWorld_autogen
Scanning dependencies of target HelloWorld
[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/HelloWorld_autogen/mocs_compilation.cpp.o
[ 75%] Building CXX object CMakeFiles/HelloWorld.dir/main.cpp.o
[100%] Linking CXX executable HelloWorld
[100%] Built target HelloWorld

And I get a HelloWorld binary file.我得到一个HelloWorld二进制文件。

The problem comes when I try to execute the latter file on another computer which don't have Qt installed.当我尝试在另一台没有安装 Qt 的计算机上执行后一个文件时,问题就出现了。 I get this bellow error:我收到以下错误:

dyld: Library not loaded: @rpath/QtWidgets.framework/Versions/5/QtWidgets
  Referenced from: /path/to/HelloWorld/./HelloWorld
  Reason: image not found
./test.sh: line 4: 68737 Abort trap: 6           ./HelloWorld

What is missing to make this working as a standalone application?使它作为独立应用程序工作缺少什么?

Environment:环境:

  • MacOs Mojave (10.14.4) MacOs 莫哈韦沙漠 (10.14.4)
  • Qt 5.10.1 Qt 5.10.1

The easiest approach is to link Qt statically.最简单的方法是静态链接 Qt。 Check out the documentation about deploying for macOS .查看有关为 macOS 部署的文档。

Be aware that Qt is licensed under the LGPL, which may have implications for static linking.请注意,Qt 是根据 LGPL 获得许可的,这可能会对静态链接产生影响。 Check out this FAQ for details.查看此常见问题解答了解详细信息。

You could also use bundles and frameworks, but I'm not sure how well that works with console applications.您也可以使用捆绑包和框架,但我不确定它与控制台应用程序的配合情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM