gpt4 book ai didi

c++ - 从 qmake 迁移到 cmake

转载 作者:行者123 更新时间:2023-11-28 01:47:34 26 4
gpt4 key购买 nike

我已遵循本指南 http://doc.qt.io/qt-5/gettingstartedqt.html一切正常。但是现在我想把这个项目从 qmake 转换到 cmake;这是 CMakeLists.txt

project(Notepad)

# Application Version, managed by release script
set(APPLICATIONS_VERSION_MAJOR "0")
set(APPLICATIONS_VERSION_MINOR "0")
set(APPLICATIONS_VERSION_MICRO "1")
set(APPLICATIONS_VERSION "${APPLICATIONS_VERSION_MAJOR}.${APPLICATIONS_VERSION_MINOR}.${APPLICATIONS_VERSION_MICRO}")

# Minimum required software
set(CMAKE_MIN_REQUIRED_VERSION "3.7.2")
set(QT_MIN_REQUIRED_VERSION "5.7.1")
set(KF5_MIN_REQUIRED_VERSION "5.31.0")
set(ECM_MIN_REQUIRED_VERSION "5.31.0")

cmake_minimum_required(VERSION "${CMAKE_MIN_REQUIRED_VERSION}" FATAL_ERROR)

# Configure ECM
find_package(ECM "${ECM_MIN_REQUIRED_VERSION}" REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH})

include(FeatureSummary)
include(ECMSetupVersion)

# Generate header with version number
ecm_setup_version("${APPLICATIONS_VERSION}" VARIABLE_PREFIX NOTEPAD
VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/notepad_version.h"
)

# Find Qt modules
find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
Core
Gui
Widgets
)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11

set(notepad_SRCS main.cpp notepad.h notepad.cpp)

add_executable(notepad ${notepad_SRCS})

target_link_libraries(notepad
Qt5::Widgets
Qt5::Core
Qt5::Gui
)

feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)

我得到了这个输出:

/documenti/deglans/Programmazione/QtCreator/Notepad/build> make -j4
Scanning dependencies of target notepad_automoc
[ 25%] Automatic uic for target notepad
Generating ui header ui_notepad.h
[ 25%] Built target notepad_automoc
Scanning dependencies of target notepad
[ 50%] Building CXX object CMakeFiles/notepad.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/notepad.dir/notepad.cpp.o
[100%] Linking CXX executable notepad
CMakeFiles/notepad.dir/notepad.cpp.o: In function `Notepad::Notepad(QWidget*)':
/documenti/deglans/Programmazione/QtCreator/Notepad/notepad.cpp:28: undefined reference to `vtable for Notepad'
/documenti/deglans/Programmazione/QtCreator/Notepad/notepad.cpp:28: undefined reference to `vtable for Notepad'
CMakeFiles/notepad.dir/notepad.cpp.o: In function `Notepad::~Notepad()':
/documenti/deglans/Programmazione/QtCreator/Notepad/notepad.cpp:33: undefined reference to `vtable for Notepad'
/documenti/deglans/Programmazione/QtCreator/Notepad/notepad.cpp:33: undefined reference to `vtable for Notepad'
CMakeFiles/notepad.dir/notepad.cpp.o: In function `Notepad::tr(char const*, char const*, int)':
/documenti/deglans/Programmazione/QtCreator/Notepad/notepad.h:28: undefined reference to `Notepad::staticMetaObject'
collect2: error: ld returned 1 exit status
CMakeFiles/notepad.dir/build.make:123: recipe for target 'notepad' failed
make[2]: *** [notepad] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/notepad.dir/all' failed
make[1]: *** [CMakeFiles/notepad.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
*** Errore: Codice di uscita 2 ***

这是使用 cmake 时出错的代码部分:

Notepad::Notepad(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Notepad)
{
ui->setupUi(this);
}

Notepad::~Notepad()
{
delete ui;
}

请注意,使用 qmake 和 QtCreator 我没有问题,当我尝试使用 KDevelop 和 cmake 时出现问题。

最佳答案

根据 Lorenz 的建议,我通过添加此行 set(CMAKE_AUTOMOC ON) 解决了这个问题。所以 CMakeLists.txt 变成:

project(Notepad)

# Application Version, managed by release script
set(APPLICATIONS_VERSION_MAJOR "0")
set(APPLICATIONS_VERSION_MINOR "0")
set(APPLICATIONS_VERSION_MICRO "1")
set(APPLICATIONS_VERSION "${APPLICATIONS_VERSION_MAJOR}.${APPLICATIONS_VERSION_MINOR}.${APPLICATIONS_VERSION_MICRO}")

# Minimum required software
set(CMAKE_MIN_REQUIRED_VERSION "3.7.2")
set(QT_MIN_REQUIRED_VERSION "5.7.1")
set(KF5_MIN_REQUIRED_VERSION "5.31.0")
set(ECM_MIN_REQUIRED_VERSION "5.31.0")

cmake_minimum_required(VERSION "${CMAKE_MIN_REQUIRED_VERSION}" FATAL_ERROR)

# Configure ECM
find_package(ECM "${ECM_MIN_REQUIRED_VERSION}" REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH})

include(FeatureSummary)
include(ECMSetupVersion)

# Generate header with version number
ecm_setup_version("${APPLICATIONS_VERSION}" VARIABLE_PREFIX NOTEPAD
VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/notepad_version.h"
)

# Find Qt modules
find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
Core
Gui
Widgets
)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON) # <<<<<<<-----------------------------

set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11

set(notepad_SRCS main.cpp notepad.h notepad.cpp)

add_executable(notepad ${notepad_SRCS})

target_link_libraries(notepad
Qt5::Widgets
Qt5::Core
Qt5::Gui
)

feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)

关于c++ - 从 qmake 迁移到 cmake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44310780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com