Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a Makefile project to Cmake

Tags:

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.

like image 722
Harrison Avatar asked Sep 08 '13 06:09

Harrison


People also ask

How do I convert to CMake?

In converting an autoconf based project to CMake, start with the configure.in and Makefile.in files. The Makefile.in file can be converted to CMake syntax as explained in the preceding section on converting UNIX Makefiles. Once this has been done, convert the configure.in file into CMake syntax.

What is difference between make and CMake?

Make (or rather a Makefile) is a buildsystem - it drives the compiler and other build tools to build your code. CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions.


1 Answers

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.

like image 73
user1283078 Avatar answered Oct 03 '22 11:10

user1283078