21
votes

I'm learning how to use cmake for this class but the documentation is extremely verbose and dense. A lot of the tutorials are either too simple to be useful (cmake with just one file) or too complicated.

the original Makefile for the project looks like:

# Some optimization settings
# see: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

# Standard all target
all: hw1_p2

# Simple program to do brute-force k-nearest neighbor searches against a signature file
hw1_p2: prob2.o ParseRecord.o Sorter.o Timing.o
        g++ -o hw1_p2 prob2.o ParseRecord.o Sorter.o Timing.o

prob2.o: prob2.cpp utility_templates.hpp
        g++ -Wall -c prob2.cpp

ParseRecord.o: ParseRecord.cpp ParseRecord.hpp utility_templates.hpp
        g++ -Wall -c ParseRecord.cpp

Sorter.o: Sorter.cpp Sorter.hpp
        g++ -Wall -c Sorter.cpp

# Timing class
Timing.o: Timing.hpp Timing.cpp
        g++ -Wall -c Timing.cpp

# Clean code-derived files
clean:
        rm -f *.o hw1_p2 

The project is organized like this

  • cmakelearn
    • CMakeLists.txt
    • build
    • src
      • CMakeLists.txt
      • ParseRecord.hpp
      • Timing.cpp
      • small.dat
      • Makefile
      • Sorter.cpp
      • Timing.hpp
      • utility_templates.hpp
      • ParseRecord.cpp
      • Sorter.hpp
      • prob2.cpp

So far, from the reading that I've done I understand that you need a CMakelists.txt file in every directory. The build folder is there so that I can go into it and run cmake ..

In the top-level CMakelists.txt file I have

cmake_minimum_required (VERSION 2.6)
project (CMAKETEST)
add_subdirectory(src)

and in the src dir CMakelist.txt here is my attempt:

include_directories(${CMAKETEST_SOURCE_DIR}/src)
link_directories(${CMAKETEST_BINARY_DIR}/src)

add_executable(hw1_p2 prob2.cpp ParseRecord.cpp Sorter.cpp Timing.cpp)

I don't even really know where to begin other than that. This comment from a tutorial pretty much sums up my thoughts about cmake and the documentation right now:

Not nearly detailed enough to be useful. Having said that, I don’t like CMake much anyway; too many separate, arbitrary Things you Just Have to Know. At least that’s my impression. Like, ‘add_definitions(-std=c99)’. Really? Does that name jump out at you as how to add flags to a compiler? And is ‘set(VAR a b c)’ any more intuitive than VAR=a b c or some such? Beh on the article, and on CMake. Yet I know CMake is taking the build-world by storm.

Still, I really want to learn and figure this out so any help that you can provide will be much appreciated and helpful. Thanks in advance.

2

2 Answers

8
votes

You do not have to create cmakelists.txt files in every subdirectory. For a project that simple it should be enough to put everything into the toplevel cmakelists.txt

cmake_minimum_required (VERSION 2.6)
project (CMAKETEST C CXX)

add_executable(hw1_p2 src/prob2.cpp src/ParseRecord.cpp src/Sorter.cpp src/Timing.cpp)

this simple cmakelists.txt should get the job done.

6
votes

The add_executable command is for building executables, but you seem to be trying to use it to build object (.o) files as well. Since you are only building one executable you only need one add_executable command:

add_executable(hw1_p2 prob2.cpp ParseRecord.cpp Sorter.cpp Timing.cpp)