我有相对复杂的cmake
项目,有多个子项目 - 一个用于第三方软件(还包括其他子项目),一个用于我正在处理的应用程序,另一个用于测试。
project: CMakeLists.txt
|
+---3rdparty: CMakeLists.txt
| |
| +---... (various 3rd party projects as libs)
|
+---application: CMakeLists.txt
|
+---tests: CMakeLists.txt
该应用程序的CMakeLists.txt
看起来像这样:
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wno-reorder")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wno-reorder")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wno-reorder")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wno-reorder")
# -----------------------------------------------------------------------------
# SOURCE FILES
# -----------------------------------------------------------------------------
set(FILTERS_SRC
filters/... # CPP files
)
set(DATAGRAM_PARSERS_SRC
parsers/datagram/datagram1parser.cpp
parsers/dataegram/datagram2parser.cpp
parsers/datagram/datagram3parser.cpp
... # other CPP files
)
set(SOURCE_FILES
main.cpp
mainapplication.cpp
... # other CPP files)
${FILTERS_SRC}
${DATAGRAM_PARSERS_SRC}
)
add_library(filters SHARED
${FILTERS_SRC}
)
add_library(datagramparsers SHARED
${DATAGRAM_PARSERS_SRC}
)
# -----------------------------------------------------------------------------
# SETUP
# -----------------------------------------------------------------------------
find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Qt5XmlPatterns REQUIRED)
# Automatically generate Qt moc files for QObject subclasses.
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories(${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
${CMAKE_SOURCE_DIR}/3rdparty/easyloggingpp/src/)
set(AUTORCC ON)
set(QRC_DIR ${CMAKE_SOURCE_DIR}/resources)
#set(QRC_FILES ${QRC_DIR}/xml.qrc)
target_link_libraries(filters Qt5::Core)
target_link_libraries(datagramparsers Qt5::Core)
# Build executable from the *.cpp source files defined above.
add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${QRC_FILES})
target_link_libraries(${PROJECT_NAME} LL Qt5::Core Qt5::Network Qt5::Xml Qt5::XmlPatterns)
# -----------------------------------------------------------------------------
# These macros are defined in common.cmake
# -----------------------------------------------------------------------------
add_common_targets()
add_common_commands()
add_common_easyloggingpp_defines()
# -----------------------------------------------------------------------------
# Misc. commands
# -----------------------------------------------------------------------------
# Copy CSV file
add_custom_command(
TARGET ${PROJECT_BINARY}
POST_BUILD
COMMAND cp -rp
${CMAKE_SOURCE_DIR}/resources/csv1/*
${CMAKE_BINARY_DIR}/${PROJECT_CONFIG_DIR}
COMMAND cp -rp
${CMAKE_SOURCE_DIR}/resources/csv2/*
${CMAKE_BINARY_DIR}/${PROJECT_CONFIG_DIR}
)
在${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
里面我有一个头文件。我们称之为iparsers.h
,我在每个与DATAGRAM_PARSERS_SRC
相应的CPP文件对应的头文件中使用它。例如:
datagram1parser.h
#include <LL/network/iparser.h>
#include <QObject>
class Datagram1Parser : public QObject, public LL::Network::IParser
{
Q_OBJECT
...
}
datagram1parser.cpp
#include "datagram1parser.h"
#include <QByteArray>
#include <QStringList>
#include <QRegExp>
#include <easylogging++.h>
Datagram1Parser::Datagram1Parser(QObject* parent) : QObject(parent)
{
...
}
...
现在我的CMakeLists.txt
子项目的tests
看起来像这样:
# -----------------------------------------------------------------------------
# SETUP
# -----------------------------------------------------------------------------
enable_testing()
find_package(Qt5Core REQUIRED)
find_package(Qt5Test REQUIRED)
find_package(Qt5Network REQUIRED) # For handling network access (HTTP requests to HAFAS server etc.)
find_package(Qt5Xml REQUIRED) # For processing XML data (from HAFAS server)
find_package(Qt5XmlPatterns REQUIRED) # For validating XML data (from HAFAS server)
# Explicitly set automoc to OFF because macro add_qttest needs moc files to
# be manually created.
set(CMAKE_AUTOMOC OFF)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test-bin)
include_directories(
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/3rdparty/easyloggingpp/src/
)
add_common_easyloggingpp_defines()
# -----------------------------------------------------------------------------
# TARGETS
# -----------------------------------------------------------------------------
set(TEST_DATAGRAM_PARSERS_LIBS LL datagramparsers)
file(GLOB DATAGRAM_PARSERS_HEADERS
"${CMAKE_SOURCE_DIR}/src/parsers/datagram/*.h"
)
message(${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network/iparser.h)
set(TEST_DATAGRAM_PARSERS_SRC
${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network/iparser.h
${DATAGRAM_PARSERS_HEADERS}
)
add_qttest2(testdatagramparsers
testdatagramparsers.h
testdatagramparsers.cpp
TEST_DATAGRAM_PARSERS_SRC
TEST_DATAGRAM_PARSERS_LIBS
)
与微add_qttest2
看起来如下:
macro(add_qttest2 TESTNAME MAIN_H MAIN_CPP OTHER_SRC_FILES LINK_LIBS)
set(COVERAGE_COMMAND ${CTEST_COVERAGE_COMMAND})
set(MOC_FILE)
qt5_wrap_cpp(MOC_FILE ${MAIN_H})
add_executable(${TESTNAME} ${MAIN_CPP} ${${OTHER_SRC_FILES}} ${MOC_FILE})
target_link_libraries(${TESTNAME} Qt5::Test ${${LINK_LIBS}})
add_test(NAME ${TESTNAME}
COMMAND ${TESTNAME}
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(MOC_FILE)
endmacro()
testdatagramparsers
头文件和源文件如下所示:
testdatagramparsers.h
#include <QObject>
#include <QtTest/QTest>
class TestDatagramParsers : public QObject
{
Q_OBJECT
private:
...
private slots:
void initTestCase();
void cleanup();
void testCaseDatagram1();
void testCaseDatagram2();
...
}
testdatagramparsers.cpp
#include "testdatagramparsers.h"
#include "parsers/datagram/datagram1parser.h"
#include "parsers/datagram/datagram2parser.h"
...
我的application
项目编译并运行正常,但是当我尝试编译我的测试目标时,我得到了
致命错误:
LL/network/iparser.h
:没有这样的文件或目录
当依赖性扫描阶段发生时。
我不确定我还能做什么。我甚至打印了${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network/iparser.h
的输出,我检查了它 - 这是正确的路径。
我还有其他测试(从上面的CMakeLists.txt
中删除),工作正常。唯一的区别是他们不使用任何第三方标题。
我假设你的文件是在qazxsw poi。
您没有将${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network
添加到测试项目的include目录中,因此无法找到它。
添加
${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
到测试项目的include_directories(
${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
)
可能会解决这个问题。