CMake子目录 - 编译测试子项目时,没有包含头文件的此类文件或目录

问题描述 投票:1回答:1

我有相对复杂的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中删除),工作正常。唯一的区别是他们不使用任何第三方标题。

c++ qt cmake qtestlib
1个回答
3
投票

我假设你的文件是在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/ ) 可能会解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.