【问题标题】:Import CMake Project into an existing QT Project .pro将 CMake 项目导入现有 QT 项目 .pro
【发布时间】:2020-01-13 09:09:39
【问题描述】:

我有一个正在开发的 QT 项目,它有几个子模块,我在项目的 .pro 文件中将它们列为:

if(!include(module/my_module.pri)){
    error("module my_module not found")
}

我尝试了什么:

好吧,我找到了一个我打算使用的库,它是一个 CMake 项目libcrashreporter-qt我只是为了完整性而链接项目 - 这个库没有任何问题(据我所知)

问题:

我想我会尝试包含 CMakeLists.txt 文件,就像我会包含 .pri 文件一样:

if(!include(libcrashreporter-qt/CMakeLists.txt)){
    error("module libcrashreporter-qt not found")
}

libcrashreporter-qt 文件夹位于我项目的根目录中

一些代码:

这样做时,我会收到一大堆编译器错误:

$project_dir/libcrashreporter-qt/CMakeLists.txt:1: 'project' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:2: 'cmake_minimum_required' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:4: Extra characters after test expression.
$project_dir/libcrashreporter-qt/CMakeLists.txt:5: 'cmake_policy' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:6: 'endif' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:10: 'find_package' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:12: Opening parenthesis without prior test name.
$project_dir/libcrashreporter-qt/CMakeLists.txt:16: 'string' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:17: 'string' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:18: 'endif' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:20: 'add_subdirectory' is not a recognized test function.
$project_dir/libcrashreporter-qt/CMakeLists.txt:21: 'add_subdirectory' is not a recognized test function.

CMake 文件内容 (注意,我稍微更改了文件,试图通过删除多行 if 语句来解决构建问题并注释了 option 行)

project(libcrashreporter-qt)
cmake_minimum_required(VERSION 3.1)

if(POLICY CMP0071)
    cmake_policy(SET CMP0071 NEW)
endif()

#option(ENABLE_GPL_CODE OFF)

find_package(Qt5 COMPONENTS Core Network Widgets)

if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_FLAGS) OR (UNIX AND NOT APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
    # Breakpad uses GNU compiler extensions like typeof.
    # Luckily these features are not used on OSX, so we can build the
    # crashreporter there with the normal C++11 standard.
    string(REPLACE "-std=c++11" "-std=gnu++11" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
    string(REPLACE "-std=c++14" "-std=gnu++14" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
endif()

add_subdirectory(3rdparty)
add_subdirectory(src)

这让我相信 Qt .pro 文件兼容性和相互使用的 CMake 项目存在问题。

问题:

如何访问此 CMake 项目的库函数,并与我的项目一起编译它(即不更改 CMake 项目本身),或者如何最好地将 CMake 项目转换为兼容的 @987654332 @项目

【问题讨论】:

  • qmake 和 cmake 是两种不同的技术,所以你不能加入它们。唯一的选择是为外部库创建一个 .pro / .pri。

标签: qt cmake qt5 qt-creator qmake


【解决方案1】:

AFAIK CMake 和 QMake 不能在一个项目中混合使用,它们是两个不同的构建系统,如果您想在 QMake 项目中使用 CMake 项目,您有“两个”选项:

  1. 如果您想使用源文件(.cpp 和 .h),甚至在您的项目中修改它们,您必须将它们导入您的 QMake 项目(您可以在 QtCreator 中轻松完成)并编译所有文件你的项目
  2. 如果您使用的 CMake 库不需要任何更改(大多数情况下,当您使用第三方库时就是这样)并且您只想将其导入您的项目并将其链接到您的可执行文件,您只需运行并编译 CMake 项目(与您自己的 QMake 项目分开),然后使用该项目的输出动态或静态库链接到您的 QMake 项目

【讨论】:

  • 感谢您的回答,希望有“import cmake as .p​​ro”选项...
【解决方案2】:

据我所知,在 Qt 中,您可以为额外目标指定自定义编译器设置,因此,如果您需要在构建目标 Qt 应用程序之前重新构建 CMake 项目,您可以尝试使用这种方式

这里有一个可能的解决方案:

https://stackoverflow.com/a/15766946/2333496

【讨论】:

    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2012-07-09
    • 2013-10-14
    相关资源
    最近更新 更多