CMake 子目录 - 编译测试子项目时没有包含 header 文件的此类文件或目录
CMake subdirectory - No such file or directory for included header file when compiling test subproject
我有一个相对复杂的 cmake
项目,有多个 sub-projects - 一个用于第三方软件(其中还包括更多 sub-projects),一个用于我正在处理的应用程序和一个用于测试。
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/
我有一个 header 文件。我们称它为 iparsers.h
,我在每个 header 文件中使用它,对应于 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)
{
...
}
...
现在我的 tests
子项目的 CMakeLists.txt
看起来像这样:
# -----------------------------------------------------------------------------
# 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
header 和源文件如下所示:
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
项目编译并运行良好,但是当我尝试编译我的测试目标时,我得到
fatal error: LL/network/iparser.h
: No such file or directory
依赖项扫描阶段发生时。
我不确定我还能做什么。我什至打印了 ${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network/iparser.h
的输出并检查了它 - 这是正确的路径。
我还有其他测试(从上面的 CMakeLists.txt
中删除)工作得很好。唯一的区别是他们不使用任何第 3 方 headers.
我假设你的文件在 ${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network
.
您没有将 ${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
添加到测试项目的包含目录中,因此无法找到它。
添加
include_directories(
${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
)
您的测试项目的 CMakeLists.txt
可能会解决这个问题。
我有一个相对复杂的 cmake
项目,有多个 sub-projects - 一个用于第三方软件(其中还包括更多 sub-projects),一个用于我正在处理的应用程序和一个用于测试。
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/
我有一个 header 文件。我们称它为 iparsers.h
,我在每个 header 文件中使用它,对应于 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)
{
...
}
...
现在我的 tests
子项目的 CMakeLists.txt
看起来像这样:
# -----------------------------------------------------------------------------
# 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
header 和源文件如下所示:
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
项目编译并运行良好,但是当我尝试编译我的测试目标时,我得到
fatal error:
LL/network/iparser.h
: No such file or directory
依赖项扫描阶段发生时。
我不确定我还能做什么。我什至打印了 ${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network/iparser.h
的输出并检查了它 - 这是正确的路径。
我还有其他测试(从上面的 CMakeLists.txt
中删除)工作得很好。唯一的区别是他们不使用任何第 3 方 headers.
我假设你的文件在 ${CMAKE_SOURCE_DIR}/3rdparty/LL/include/LL/network
.
您没有将 ${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
添加到测试项目的包含目录中,因此无法找到它。
添加
include_directories(
${CMAKE_SOURCE_DIR}/3rdparty/LL/include/
)
您的测试项目的 CMakeLists.txt
可能会解决这个问题。