12
votes

Here is what I want to do:

  • Typing make all will build my library and the docs for it.
  • Typing make test will build my lib (if necessary), gtest and then my tests
  • Typing make check runs make test if needed and then runs the executable

Right now I've only managed to get the first to work. The problem I'm having is the conditional include of gtest.

Gtest uses CMake which is nice, in theory all I need to do is to include the gtest directory with add_subdirectory but then gtest will always be built.

My structure right now is:

CMakeLists.txt     (Here I add targets for doc and the library)
doc                (my doxygen docs)
include            (my headers)
lib                (where my compiled libraries go)
src                (where my .cpp files go)
test
    CMakeLists.txt (Here I add targets for gest and my tests)
    bin            (where the test executable will go)
    contrib        (where gtest is)
    src            (my tests)

I'm trying to figure out how to add gtest as a dependency to the test-target but not build gtest every time.

I'm really annoyed and the little to none information there is about learning CMake so if anyone know any in depth tutorials (available freely on the interwebs) that would be awesome.

1
if you figure this out, would you be interested in helping me add this to the C++ Library Project Template (code.google.com/p/cpp-library-project-template)? (The subversion code currently uses gmock/gtest and glog, although the released version uses unittest++. Currently, it builds tests all the time, though, and executes the tests when running "make").Michael Aaron Safyan
Wow, feel kinda stupid now. The trick is to do add_subdirectory(contrib/gtest EXCLUDE_FROM_ALL) and add_executable(test EXCLUDE_FROM_ALL foo.cpp)Nicklas A.

1 Answers

8
votes

The trick is to do add_subdirectory(test EXCLUDE_FROM_ALL) and then none of the targets in that CMakeList.txt will added to the ALL target.