home > research > software > CMake

CMake

This short tutorial explains the structure of CMakeLists.txt files used in our projects.
If you need to start a new project, ask monica for a template.

The project structure

We usually try to have a structure that looks as follow (more info here).

/projectHome
	CMakeLists.txt
	src/
		CMakeLists.txt
		main.cpp
		...
	build/	
	lib/
		CMakeLists.txt
			/lib1
			   CMakeLists.txt
				a.cpp
				a. h 
				....
			/lib2
			....
	test/
		CMakeLists.txt
		test1.cpp
		test2.cpp             


CMakeLists.txt in the root directory

The main CMakeLists.txt (in the root) has the following structure:

cmake_minimum_required(VERSION 2.6)

project(helloworld)
 
add_subdirectory (src) 
add_subdirectory (lib)


Let's explain the different commands:
cmake_minimum_required(VERSIONE 2.6)

states that CMake required versione must be >= 2.6

project(helloworld)

states that the name of our project is "helloworld". CMakeLists files in this project can refer to the root source directory of the project as ${helloworld_SOURCE_DIR} and to the root binary directory of the project as ${helloworld_BINARY_DIR}.

add_subdirectory (src) 
add_subdirectory (lib)

recurse into the "src" and "lib" subdirectories.

CMakeLists.txt in the libX directories

add_library(First a.cpp b.cpp c.cpp)

This creates a library called libFirst which includes the source files "a.cpp", "b.cpp" and "c.cpp".

CMakeLists.txt in the src directories

# Make sure the compiler can find include files from our Hello library. 
include_directories (${HELLO_SOURCE_DIR}/Hello) 

# Make sure the linker can find the Hello library once it is built. 
link_directories (${HELLO_BINARY_DIR}/Hello) 

# Add executable called "helloDemo" that is built from the source files 
# "demo.cxx" and "demo_b.cxx". The extensions are automatically found. 
add_executable (helloDemo demo.cxx demo_b.cxx) 

# Link the executable to the Hello library. 
target_link_libraries (helloDemo Hello) 


include_directories (${HELLO_SOURCE_DIR}/Hello) 

link_directories (${HELLO_BINARY_DIR}/Hello) 

add_executable (helloDemo demo.cxx demo_b.cxx) 

target_link_libraries (helloDemo Hello) 
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki