5

假设我有一个具有以下目录结构的项目:

myproject
├── .git [...]
├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    ├── foo.cc
    └── foo.h

如果src/foo.cc我在其中包含头文件#include "foo.h",然后在其上运行 Google 的cpplint.py,它会抱怨

src/foo.cc:8:  Include the directory when naming .h files  [build/include] [4]

所以我把它包括为#include "./foo.h". 现在我收到另一个投诉:

src/foo.cc:8:  src/foo.cc should include its header file src/foo.h  [build/include] [5]

但是,如果我将它包含为#include "src/foo.h",编译器将找不到它,我当前的 CMake 设置。这就是我的两个 CMakeLists.txt 文件的样子:

CMakeLists.txt:

project(myproject)
add_subdirectory(src)

src/CMakeLists.txt:

set(SRCS foo.cc)
add_executable(foo ${SRCS})

我使用 CMake 的方式从根本上是错误的吗?我应该src/CMakeLists.txt完全删除该文件,并使用CMakeLists.txt完整路径指定基础中的所有源文件吗?

还是我应该简单地忽略 cpplint 的抱怨,因为它们并不真正适合 CMake 项目的设置方式?

4

1 回答 1

1

添加include_directories(${CMAKE_SOURCE_DIR})您的顶级 CMakeLists.txt,就像 Wander 建议的那样:

project(myproject)
include_directories(${CMAKE_SOURCE_DIR})
add_subdirectory(src)
于 2018-06-12T21:38:14.450 回答