5

Assume I have a project of the following directory structure:

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

If in src/foo.cc I include the header file like #include "foo.h" and then run Google's cpplint.py on it, it complains with

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

So I include it as #include "./foo.h". Now I get another complaint:

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

However, if I include it as #include "src/foo.h", the compiler won't find it, with my current setup of CMake. This is how my two CMakeLists.txt files look like:

CMakeLists.txt:

project(myproject)
add_subdirectory(src)

src/CMakeLists.txt:

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

Is the way I'm using CMake somehow fundamentally wrong? Should I remove the src/CMakeLists.txt file entirely, and specify all source files in the base CMakeLists.txt with their full path?

Or should I simply ignore cpplint's complaints, as they don't really fit to how CMake projects are to be set up?

Georg P.
  • 2,785
  • 2
  • 27
  • 53
  • What happens when you pass `--root=src` to cpplint.py? – Wander Nauta Nov 05 '16 at 14:16
  • I tried it, but strangely, it doesn't change anything. Also, I understand that cpplint.py treats the one directory containing the .git folder as base directory. It sounds reasonable to me, and I don't think I should overwrite it with the --root option. – Georg P. Nov 05 '16 at 14:21
  • Hmm. Looking at the [style guide](https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes), Google expects you to add the `src` directory to your include path. You should probably put an `include_directories` call in one of your CMakeLists. – Wander Nauta Nov 05 '16 at 14:26
  • 1
    You mean putting `include_directories(../)` into the src/CMakeLists.txt? Hm, yes, that works for both, CMake and cpplint.py. It seems slightly awkward to me though. Nevertheless, thanks for your support! Put your comment as answer and I'll accept it. – Georg P. Nov 06 '16 at 13:09
  • I guess it would be `.` from `src`, but you're right, it is a bit awkward. I've been digging some more, but I can't really find a definite source on how cpplint expects projects to be laid out: all of Google's own projects seem to do things differently: separate `include` directory or not, subdirs under `src` or not... Maybe someone else can clarify. – Wander Nauta Nov 06 '16 at 13:59

1 Answers1

2

Add include_directories(${CMAKE_SOURCE_DIR}) in your top level CMakeLists.txt, like Wander suggested:

project(myproject)
include_directories(${CMAKE_SOURCE_DIR})
add_subdirectory(src)
cphurley82
  • 64
  • 7