我有以下项目结构:
CMakeLists.txt
main.cpp
Build/
OrderBook/
CmakeLists.txt
OrderBook.cpp
OrderBook.hpp
以下是TLD CMakeLists.txt的内容:
cmake_minimum_required(VERSION 3.5)
project(OrderBookDemo)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(SQLite3 REQUIRED)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
set(SOURCE_FILES main.cpp)
add_executable(OrderBookDemo ${SOURCE_FILES})
include_directories(OrderBook)
add_subdirectory(OrderBook)
target_link_libraries(OrderBookDemo OrderBook DBHandler Qt${QT_VERSION_MAJOR}::Widgets SQLite::SQLite3 )
以下是OrderBook文件夹中CMakeLists.txt的内容:
project (OrderBook)
add_definitions(-std=c++14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(HEADER_FILES OrderBook.hpp)
set(SOURCE_FILES OrderBook.cpp)
add_library(OrderBook STATIC ${SOURCE_FILES} ${HEADER_FILES})
有趣的是,当我将
#include <sqlite3.h
添加到 OrderBook.hpp
时,项目成功编译。
但是,当我将
#include <QApplication>
添加到OrderBook.hpp
时,使用cmake --build .
编译时出现错误,错误是:
In file included from /Users/shreyashonnalli/desktop/Group Project/GroupRepo/DBHandler/DBHandler.cpp:8:
In file included from /Users/shreyashonnalli/desktop/Group Project/GroupRepo/DBHandler/DBHandler.hpp:14:
/Users/shreyashonnalli/desktop/Group Project/GroupRepo/OrderBook/OrderBook.hpp:11:10: fatal error: 'QApplication' file not found
#include <QApplication>
^~~~~~~~~~~~~~
1 error generated.
此外,当我将
#include <QApplication>
和 #include <sqlite3.h
添加到 main.cpp
时,它仍然可以编译。这告诉我外部库可供 TLD 中的 main.cpp
使用,但只有 sqlite3 可供 OrderBook/
文件使用。
为什么会发生这种情况?如何使 Qt 也可用于子目录?
请忽略代码中的
DBHandler
部分,因为它与问题的基本表示无关。
因为 QCoreApplication 位于 Qt::Core 模块中,所以除了 Qt::Widgets 之外,您还需要找到 Qt Core 并将演示链接到 Qt::Core 这样
#include <QApplication>
就可以解决