我注意到qmake和CMake之间存在这种不一致:qmake始终链接到发布Qt的版本,而如果构建类型为debug,则CMake将链接到Qt的调试版本。
要演示的小型项目
one.cpp
#include <QApplication>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
return app.exec();
}
one.pro
TEMPLATE += app
CONFIG += debug
QT += core gui widgets
SOURCES += one.cpp
TARGET = one
CMakeLists.txt
project(one)
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_MACOSX_RPATH 1)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
add_executable(one MACOSX_BUNDLE one.cpp)
target_link_libraries(one Qt5::Core Qt5::Gui Qt5::Widgets)
比较两个版本:
使用qmake进行构建:
$ (mkdir build-qmake && cd build-qmake && ~/Qt/5.12.5/clang_64/bin/qmake .. && make)
$ otool -L build-qmake/one.app/Contents/MacOS/one
build-qmake/one.app/Contents/MacOS/one:
@rpath/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.12.0, current version 5.12.5)
@rpath/QtGui.framework/Versions/5/QtGui (compatibility version 5.12.0, current version 5.12.5)
@rpath/QtCore.framework/Versions/5/QtCore (compatibility version 5.12.0, current version 5.12.5)
/System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
使用CMake构建
$ (mkdir build-cmake && cd build-cmake && cmake .. && cmake --build .)
$ otool -L build-cmake/one.app/Contents/MacOS/one
build-cmake/one.app/Contents/MacOS/one:
@rpath/QtWidgets.framework/Versions/5/QtWidgets_debug (compatibility version 5.12.0, current version 5.12.5)
@rpath/QtGui.framework/Versions/5/QtGui_debug (compatibility version 5.12.0, current version 5.12.5)
@rpath/QtCore.framework/Versions/5/QtCore_debug (compatibility version 5.12.0, current version 5.12.5)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
(请注意,它链接到 _debug 版本)
问题:尽管处于调试构建状态,如何使CMake与Qt版本链接?