CMake无法在其他目录中找到源文件

时间:2015-07-15 15:55:47

标签: cmake

我试图将一个目录(/ source)中的文件包含到另一个目录(/ test)中的测试用例中,但是cmake无法找到/ source中的任何文件。

我的项目结构:

PrettyDD/
    + CMakeLists.txt
    + external/
        + CMakeLists.txt
        + gmock-1.7.0/
    + source/
        + CMakeLists.txt
        + parser.cpp
        + parser.h
        + main.cpp
        + ...
    + test/
        + CMakeLists.txt
        + test1.cpp

错误消息:

marcus@lean-machine:~/Programming/PrettyDD/build$ cmake ../ -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles"
-- Could NOT find Threads (missing:  Threads_FOUND) 
-- Could NOT find Threads (missing:  Threads_FOUND) 
-- Configuring done
CMake Error at test/CMakeLists.txt:12 (add_executable):
  Cannot find source file:

    parser.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

/test/CMakeLists.txt

set(GMOCK_DIR "${CMAKE_SOURCE_DIR}/external/gmock-1.7.0"
    CACHE PATH "The path to the GoogleMock test framework.")

set_property(TARGET gtest APPEND_STRING PROPERTY COMPILE_FLAGS " -w")

include_directories(SYSTEM ${GMOCK_DIR}/gtest/include
                           ${GMOCK_DIR}/include)
include_directories(SYSTEM "${PROJECT_SOURCE_DIR}/source")


function(add_gmock_test target)
    add_executable(${target} ${ARGN})
    target_link_libraries(${target} gmock_main)

    add_test(${target} ${target})

    add_custom_command(TARGET ${target}
               POST_BUILD
               COMMAND ${target}
               WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
               COMMENT "Running ${target}" VERBATIM)
endfunction()

add_gmock_test(tests test1.cpp parser.cpp)

我已通过使用CMake ${PROJECT_SOURCE_DIR}/source验证message()指向正确的目录。

如果我在parser.cpp中明确指定了../source/parser.cppadd_gmock_test())的路径,那么它就能找到该文件。

如果我首先在parser.cpp中使用/source/CMakeLists.txt(及其依赖关系)构建库,并将其添加到链接gmock_main的位置,我也可以成功运行测试。

为什么CMake无法在parse.cpp中找到/source或任何其他文件?我做错了什么?

在DarthB的回答后编辑:

离开parser.cpp add_gmock_test(tests test1.cpp)会出现以下错误:

marcus@lean-machine:~/Programming/PrettyDD/build$ make
[ 55%] Built target PrettyDD
[ 66%] Built target gmock
[ 83%] Built target gmock_main
[ 88%] Built target gtest
[ 94%] Built target gtest_main
Linking CXX executable tests
CMakeFiles/tests.dir/test1.cpp.o: In function `Parser_Construct_Test::TestBody()':
/home/marcus/Programming/PrettyDD/test/test1.cpp:7: undefined reference to `Parser::Parser()'
collect2: error: ld returned 1 exit status
test/CMakeFiles/tests.dir/build.make:86: recipe for target 'test/tests' failed
make[2]: *** [test/tests] Error 1
CMakeFiles/Makefile2:316: recipe for target 'test/CMakeFiles/tests.dir/all' failed
make[1]: *** [test/CMakeFiles/tests.dir/all] Error 2
Makefile:86: recipe for target 'all' failed
make: *** [all] Error 2

这就是test1.cpp的样子:

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "parser.h"

TEST(Parser, Construct) {
  Parser parser;
}

parser.cpp:s构造函数:

Parser::Parser() {
    createFunctionMap();
}

parser.h中声明为:

class Parser {
public:
    Parser();
    ...

构造函数应该没有任何问题,因为程序本身运行正常。那我为什么会收到这个错误?

1 个答案:

答案 0 :(得分:1)

include_directories()仅更改包含目录的编译器参数,这意味着您可以使用

在测试代码中的某处包含“parser.h”
#include "parser.h" 

和... / PrettyDD / source搜索此文件。

我认为parser.cpp不包含测试代码。所以它不应该是你的测试项目的一部分:

add_gmock_test(tests test1.cpp)

应该够了。否则,您应该重新考虑您的项目结构。

或者您也可以使用:

add_gmock_test(tests test1.cpp ../source/parser.cpp)

编辑:

关于评论中的问题。几乎每个cmake命令的参数都是相对于$ {CMAKE_CURRENT_SOURCE_DIR}或$ {CMAKE_CURRENT_BINARY_DIR}。据我所知,cmake命令没有“include_directories”,但在列出源文件时我们也可以使用taget_sources()命令。

这意味着如果CMakeLists.txt与源文件是同一个文件夹,它应该写入:

target_sources(tests parser.cpp)

在'source'文件夹的CMakeLists.txt文件中。

可能存在问题,因为目标还不知道。我只在包含源的项目的子文件夹中使用了target_sources()。因此我使用类似于以下的结构:

    • MYLIB
      • SRC
      • INC

其中src和inc文件夹还包含一个CMakeLists.txt文件,该文件使用target_sources()命令显式列出源文件。

相关问题