blastthisinferno
blastthisinferno

Reputation: 539

CMake basic project setup

I'm new to CMake and re-learning C++, so I hope these are appropriate questions. I have a project in directory /projects/A and some .h files in /projects/include/open-source-project-1 that the A project depends on.

Sample Hierarchy:
/projects
  /CMakeLists.txt
  /A
    /CMakeLists.txt
    /a.cpp
  /B
    /CMakeLists.txt
  /include
    /open-source-project-1
      /includeMe.h
    /open-source-project-2
  1. Do I need to use a cmake include_directories() command? If so, in what CMakeLists.txt file do I need to put it in? (I've tried many variations)
  2. Do I need a different cmake command?
  3. If I put that in the top most level CMakeLists.txt, should that take care of all occurences of #include in the .cpp files for the A project or B project?
  4. Is this a typical setup for a c++ project? Does it seem logical?

Upvotes: 2

Views: 1555

Answers (1)

greyfade
greyfade

Reputation: 25677

Your top-level CMakeLists.txt file is not needed unless all of the projects are directly inter-related somehow or have otherwise unresolvable dependencies. Unrelated projects should not include each other, nor do they need a parent list file.

  1. If A and B are separate projects, ./A/CMakeLists.txt and ./B/CMakeLists.txt should contain at least this line:

    include_directories(../include)
    

    Otherwise, if A and B are parts of a larger single project, then it is appropriate to put these lines in the top-level CMakeLists.txt files:

    include_directories(include)
    add_subdirectory(A)
    add_subdirectory(B)
    
  2. Only for separate projects. One invocation of cmake will create one build tree. One project to a build tree is all you need.

  3. No. Only if the top-level lists file contains an add_subdirectory directive will it affect the other list files.
  4. No, this is atypical.

Upvotes: 3

Related Questions