Ok some basics, you obviously need to install CMake. For *nix and mac, this should be straight forward since its basically a standard package now. Once installed all you basically need to do is to create a CMakeLists.txt file in your project folder, wherever you place CMakeLists is where the ${PROJECT_SOURCE_DIR} variable points to (explained later), so you almost always want to place it in the root of your project directory. For this tutorial we also assume you will have to separate folders for headers and src
Ok first up, create CMakeLists and add these lines to the file (we are going to do a simple project without debug/release and shared compilation of irrlicht just to get the basics). Also create the folder src and include in the project root folder (src will contain .cpp files and include your header files)
Code: Select all
cmake_minimum_required(VERSION 2.6)
PROJECT(example)
Code: Select all
INCLUDE_DIRECTORIES(
"/usr/include/irrlicht"
${PROJECT_SOURCE_DIR}/include
)
Code: Select all
FILE(GLOB SRCS src/*.cpp)
FILE(GLOB HDRS include/*.h)
Code: Select all
ADD_EXECUTABLE(example
${SRCS}
${HDRS}
)
Code: Select all
TARGET_LINK_LIBRARIES(example
"/usr/lib/libIrrlicht.so"
)
Code: Select all
cmake_minimum_required(VERSION 2.6)
PROJECT(example)
INCLUDE_DIRECTORIES(
"/usr/include/irrlicht"
${PROJECT_SOURCE_DIR}/include
FILE(GLOB SRCS src/*.cpp)
FILE(GLOB HDRS include/*.h)
ADD_EXECUTABLE(example
${SRCS}
${HDRS}
)
TARGET_LINK_LIBRARIES(example
"/usr/lib/libIrrlicht.so"
)
Ok so after PROJECT(example) lets add
Code: Select all
SET(CMAKE_CXX_FLAGS_RELEASE "-O3")
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
Code: Select all
IF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
In this case what we are saying, is if the user doesn't specify a build and configuration type (i.e. just runs CMake CMakeLists.txt) then we set the build type to Release. After the above add
Code: Select all
IF(CMAKE_CXX_FLAGS_DEBUG)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/debug)
ELSE(CMAKE_CXX_FLAGS_DEBUG)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/release)
ENDIF(CMAKE_CXX_FLAGS_DEBUG)
What this does is set the binary paths to the seperate folders. If we are a debug build the binary goes to debug folder (same story with release). By default, Project_Binary_Dir is the same as Project_Directory. Your complete CMakeLists.txt should look like this
Code: Select all
cmake_minimum_required(VERSION 2.6)
PROJECT(example)
SET(CMAKE_CXX_FLAGS_RELEASE "-O3")
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
IF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE RelWithDebInfo)
ENDIF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
IF(CMAKE_CXX_FLAGS_DEBUG)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/debug)
ELSE(CMAKE_CXX_FLAGS_DEBUG)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/release)
ENDIF(CMAKE_CXX_FLAGS_DEBUG)
INCLUDE_DIRECTORIES(
"/usr/include/irrlicht"
${PROJECT_SOURCE_DIR}/include
FILE(GLOB SRCS src/*.cpp)
FILE(GLOB HDRS include/*.h)
ADD_EXECUTABLE(example
${SRCS}
${HDRS}
)
TARGET_LINK_LIBRARIES(example
"/usr/lib/libIrrlicht.so"
)
Now the last part of the tutorial, what if we wan't to compile our irrlicht project statically? This is obviously slightly more complicated, since we actually need to add in the dependencies that irrlicht requires. When you install CMake, it creates a directory where CMake modules are found. CMake modules are used for finding packages which are dependencies of programs. On *nix systems, this CMake folder is usually in /usr/share/cmake-<version> or /usr/local/share/cmake-<version> and usually stored in a folder called modules. This is important, since the default value CMAKE_MODULE_PATH points to this path, but it can be changed using set to another folder. Since there is no module for locating the dependancies of irrlicht yet, we have to do it manually (which isn't such a big deal in this case). Place
Code: Select all
FIND_PACKAGE(X11)
FIND_PACKAGE(GLUT)
FIND_PACKAGE(ZLIB)
This makes sense, its basically telling CMake to find those packages. Now since these are standardised packages, there are Cmake package modules for them. However if package doesn't have a CMake module (in this case irrlicht, which is why we had to specificy a manual location to the irrlicht header files and shared objects) we need to specifiy where it is located. FIND_PACKAGE just locates the packages, now we need to actually add them to the include folders and library folders. Change INCLUDE_DIRECTORIES to this
Code: Select all
INCLUDE_DIRECTORIES(
"/usr/include/irrlicht"
${X11_INCLUDE_DIR}
${GLUT_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/include
)
Code: Select all
TARGET_LINK_LIBRARIES(example
${X11_LIBRARIES}
${GLUT_LIBRARIES}
${ZLIB_LIBRARIES}
"/usr/lib/libIrrlicht.a"
)
Code: Select all
cmake_minimum_required(VERSION 2.6)
PROJECT(example)
SET(CMAKE_CXX_FLAGS_RELEASE "-O3")
SET(CMAKE_CXX_FLAGS_DEBUG "g")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g")
IF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE RelWithDebInfo)
ENDIF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
IF(CMAKE_CXX_FLAGS_DEBUG)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/debug)
ELSE(CMAKE_CXX_FLAGS_DEBUG)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/release)
ENDIF(CMAKE_CXX_FLAGS_DEBUG)
FIND_PACKAGE(X11)
FIND_PACKAGE(GLUT)
FIND_PACKAGE(ZLIB)
INCLUDE_DIRECTORIES(
"/usr/include/irrlicht"
${X11_INCLUDE_DIR}
${GLUT_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/include
)
FILE(GLOB SRCS src/*.cpp)
FILE(GLOB HDRS include/*.h)
ADD_EXECUTABLE(example
${SRCS}
${HDRS}
)
TARGET_LINK_LIBRARIES(example
${X11_LIBRARIES}
${GLUT_LIBRARIES}
${ZLIB_LIBRARIES}
"/usr/lib/libIrrlicht.a"
)