Cmake simple tutorial using library

On cmake

  1. CMake is a build system that uses a single script (defined in a CMakeLists.txt file) to generate appropriate platform-specific build scripts.
  2. Building complex projects is where CMake really shines — CMake is a cross-platform Makefile generator! Simply put, CMake automatically generates the Makefiles for your project

On sturecture of the program

  1. Directory include holds header files, whose extension is .h
  2. Directory src holds source files, whose extension is .cpp
  3. Directory build contains the final binary executable and any temporary files that are required for the build. It contains Makefile which refers to the files in the src and include directories. The project can then be built from the build directory using the make command. Using build directory can enhance flexibility on cross platform use for the reason that we can easily remove the generated file by command rm -r build/* and recreate them on other platforms.
  4. Script CMakeLists.txt to specify the building configuration

On creating a library

.Tree structure
├── build
├── CMakeLists.txt
├── include
│ └── HelloClass.h
└── src
└── HelloClass.cpp

include/HelloClass.h

#include<string>
using namespace std ;

class HelloClass
{
private:
	string name ;
public:
	HelloClass(string name );
	void sayHello();
} ;

src/HelloClass.cpp

#include "HelloClass.h"
#include <iostream>
#include <string>
using namespace std ;

HelloClass::HelloClass( string name ): name( name ){}

void HelloClass::sayHello()
{
	cout<<"Hello "<<name<<endl ;
}

CMakeLists.txt

cmake_minimum_required( VERSION 2.8.9 )
project( hello_world )
set( CMAKE_BUILD_TYPE Release )

#Bring the headers, such as HelloClass.h into project
include_directories( include )

#Mannually add the sources using the set command as follows:
#set( SOURCES src/main.cpp src/HelloClass.cpp)

#However, the file( GLOB ... ) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")

add_library(hello SHARED ${SOURCES})

#Set the location for library installation, /usr/lib in this case
#This is not really necessary 
#Use "sudo make install" to apply
install( TARGETS hello DESTINATION /usr/lib )

Building command in directory build

Cmake simple tutorial using library

Shared library in libhello.so

Cmake simple tutorial using library

Test on library

Structure of test
Cmake simple tutorial using library

src/main.cpp

#include "HelloClass.h"

int main(){
	HelloClass hello("Kangkang") ;
	hello.sayHello() ;
	return 0 ;
}

CMakeLists.txt

#Set minimum version of CMake for this project, Which is major version 2
#minor version 8, and patch version 9 in this example
cmake_minimum_required( VERSION 2.8.9 )
#Set project name 
project( TestLibrary )

#Refere to shared library
#Command set is set a variable that contains libhello.so
set( LINK_LIBS libhello.so )
#bring the library directory
link_directories( lib/build )

#Bring the header directory
include_directories( lib/include )

#Bring source files 
file( GLOB SOURCES src/*.cpp )

#Build source files SOURCES into executable file which is named hello
#The first argument is the name of the executable file 
#The second argument is the sources to be built
add_executable( hello ${SOURCES} )
#Link dependences to executable file hello
target_link_libraries(hello ${LINK_LIBS})

Result

Cmake simple tutorial using library

Compile commands in bash.sh

rm -r build/*
rm -r lib/build/*

echo "----------Build library"
cd lib/build
cmake ..
make

echo "----------Build target main function"
cd ..
cd ..
cd build
cmake ..
make

Reference

  1. Basic concept on C++ class
  2. derekmolloy simple tutorial on cmake
  3. Official cmake tutorial
  4. Github repository of this tutorial