Converting a Makefile project to Cmake
Solution 1
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)
Solution 2
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.
Related videos on Youtube
Comments
-
Harrison over 3 years
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.
- cmakelearn
-
Harrison over 10 yearsthanks, made some progress but I get an error about prob2 having an <#include Timing.hpp> in it. It says the library can't be found. So I'm guessing I have to link the library with cmake somehow. I tried TARGET_LINK_LIBRARIES(prob2 timing.hpp) to no avail.
-
David Brown over 10 years@HarrisonNguyen It should be
#include <Timing.hpp>
not<#include Timing.hpp>
. -
Harrison over 10 yearsSorry, that's what I meant.
[ 25%] Building CXX object CMakeFiles/hw1_p2.dir/src/prob2.cpp.o /Users/harrison/Documents/cmake/src/prob2.cpp:14:10: fatal error: 'scottgs/Timing.hpp' file not found #include <scottgs/Timing.hpp> ^ 1 error generated. make[2]: *** [CMakeFiles/hw1_p2.dir/src/prob2.cpp.o] Error 1 make[1]: *** [CMakeFiles/hw1_p2.dir/all] Error 2 make: *** [all] Error 2
-
Harrison over 10 yearsI changed it to #include "Timing.hpp" in prob2.cpp and it seems to be working.
-
whatnick over 10 yearsMuch shorter than the makefile and works for a lot of platforms at the same time.