Skip to main content

History: Building Your Projects With CMake

Preview of version: 29

Image

This section is an introduction on how to use CMake to build your own projects and have them link against Ogre. For more detailed information about CMake, refer to the official CMake website: http://www.cmake.org

Introduction

This is a simple CMake script for use in your own projects.
The sample script uses the Ogre Wiki Tutorial Framework, but you can adapt it easily.

Prerequisites

Be sure to visit the Prerequisites page!

Note for Windows:
If you don't have Boost installed, you want to set the environment variable BOOST_ROOT to OGRE_HOME/boost_1_42

Ogre Wiki Tutorial Framework

Get the Ogre Wiki Tutorial Framework and put the four files in a directory:

  • ogreapp
    • CMakeLists.txt (see below)
    • BaseApplication.h
    • BaseApplication.cpp
    • TutorialApplication.h
    • TutorialApplication.cpp

CMakeLists.txt

Info Red

This CMake script assumes the following:

  • Windows: the environment variable OGRE_HOME is set to point to the root directory of your Ogre SDK.
  • Linux: Ogre is installed in the standard path: /usr/local.

For MinGW, the post-build copy needs to be fixed.

Copy to clipboard
#/* #----------------------------------------------------------------------------- #Filename: CMakeLists.txt #----------------------------------------------------------------------------- # #This source file is part of the # ___ __ __ _ _ _ # /___\__ _ _ __ ___ / / /\ \ (_) | _(_) # // // _` | '__/ _ \ \ \/ \/ / | |/ / | #/ \_// (_| | | | __/ \ /\ /| | <| | #\___/ \__, |_| \___| \/ \/ |_|_|\_\_| # |___/ # Tutorial Framework # http://www.ogre3d.org/tikiwiki/ #----------------------------------------------------------------------------- #*/ cmake_minimum_required(VERSION 2.6) project(OgreApp) if(WIN32) set(CMAKE_MODULE_PATH "$ENV{OGRE_HOME}/CMake/;${CMAKE_MODULE_PATH}") set(OGRE_SAMPLES_INCLUDEPATH $ENV{OGRE_HOME}/Samples/include ) endif(WIN32) if(UNIX) set(CMAKE_MODULE_PATH "/usr/local/lib/OGRE/cmake/;${CMAKE_MODULE_PATH}") set(OGRE_SAMPLES_INCLUDEPATH /usr/local/share/OGRE/samples/Common/include/ ) endif(UNIX) if (CMAKE_BUILD_TYPE STREQUAL "") # CMake defaults to leaving CMAKE_BUILD_TYPE empty. This screws up # differentiation between debug and release builds. set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: None (CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE) endif () set(CMAKE_DEBUG_POSTFIX "_d") set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/dist") find_package(OGRE REQUIRED) if(NOT "${OGRE_VERSION_NAME}" STREQUAL "Cthugha") message(SEND_ERROR "You need Ogre 1.7 Cthugha to build this.") endif() find_package(OIS REQUIRED) if(NOT OIS_FOUND) message(SEND_ERROR "Failed to find OIS.") endif() # Find Boost if (NOT OGRE_BUILD_PLATFORM_IPHONE) if (WIN32 OR APPLE) set(Boost_USE_STATIC_LIBS TRUE) else () # Statically linking boost to a dynamic Ogre build doesn't work on Linux 64bit set(Boost_USE_STATIC_LIBS ${OGRE_STATIC}) endif () if (MINGW) # this is probably a bug in CMake: the boost find module tries to look for # boost libraries with name libboost_*, but CMake already prefixes library # search names with "lib". This is the workaround. set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_FIND_LIBRARY_PREFIXES} "") endif () set(Boost_ADDITIONAL_VERSIONS "1.42" "1.42.0" "1.41.0" "1.41" "1.40.0" "1.40" "1.39.0" "1.39" "1.38.0" "1.38" "1.37.0" "1.37" ) # Components that need linking (NB does not include header-only components like bind) set(OGRE_BOOST_COMPONENTS thread date_time) find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS} QUIET) if (NOT Boost_FOUND) # Try again with the other type of libs set(Boost_USE_STATIC_LIBS NOT ${Boost_USE_STATIC_LIBS}) find_package(Boost COMPONENTS ${OGRE_BOOST_COMPONENTS} QUIET) endif() find_package(Boost QUIET) # Set up referencing of Boost include_directories(${Boost_INCLUDE_DIR}) add_definitions(-DBOOST_ALL_NO_LIB) set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${Boost_LIBRARIES}) endif() set(HDRS ./BaseApplication.h ./TutorialApplication.h ) set(SRCS ./BaseApplication.cpp ./TutorialApplication.cpp ) include_directories( ${OIS_INCLUDE_DIRS} ${OGRE_INCLUDE_DIRS} ${OGRE_SAMPLES_INCLUDEPATH} ) add_executable(OgreApp WIN32 ${HDRS} ${SRCS}) set_target_properties(OgreApp PROPERTIES DEBUG_POSTFIX _d) target_link_libraries(OgreApp ${OGRE_LIBRARIES} ${OIS_LIBRARIES}) # post-build copy for win32 if(WIN32 AND NOT MINGW) add_custom_command( TARGET OgreApp PRE_BUILD COMMAND if not exist .\\dist\\bin mkdir .\\dist\\bin ) add_custom_command( TARGET OgreApp POST_BUILD COMMAND copy \"$(TargetPath)\" .\\dist\\bin ) endif(WIN32 AND NOT MINGW) # if win32, copy the Ogre DLLs and the media over if(WIN32) install(TARGETS OgreApp RUNTIME DESTINATION bin CONFIGURATIONS All) install(DIRECTORY $ENV{OGRE_HOME}/media DESTINATION ../ ) install(FILES $ENV{OGRE_HOME}/bin/release/OgreMain.dll $ENV{OGRE_HOME}/bin/release/RenderSystem_Direct3D9.dll $ENV{OGRE_HOME}/bin/release/RenderSystem_GL.dll $ENV{OGRE_HOME}/bin/release/Plugin_OctreeSceneManager.dll $ENV{OGRE_HOME}/bin/release/cg.dll $ENV{OGRE_HOME}/bin/release/OIS.dll $ENV{OGRE_HOME}/bin/release/Plugin_CgProgramManager.dll $ENV{OGRE_HOME}/bin/release/Plugin_ParticleFX.dll $ENV{OGRE_HOME}/bin/release/Plugin_BSPSceneManager.dll $ENV{OGRE_HOME}/bin/release/Plugin_CgProgramManager.dll $ENV{OGRE_HOME}/bin/release/Plugin_PCZSceneManager.dll $ENV{OGRE_HOME}/bin/release/Plugin_OctreeZone.dll $ENV{OGRE_HOME}/bin/release/resources.cfg $ENV{OGRE_HOME}/bin/release/plugins.cfg DESTINATION bin CONFIGURATIONS Release RelWithDebInfo ) install(FILES $ENV{OGRE_HOME}/bin/debug/OgreMain_d.dll $ENV{OGRE_HOME}/bin/debug/RenderSystem_Direct3D9_d.dll $ENV{OGRE_HOME}/bin/debug/RenderSystem_GL_d.dll $ENV{OGRE_HOME}/bin/debug/Plugin_OctreeSceneManager_d.dll $ENV{OGRE_HOME}/bin/debug/cg.dll $ENV{OGRE_HOME}/bin/debug/OIS_d.dll $ENV{OGRE_HOME}/bin/debug/Plugin_CgProgramManager_d.dll $ENV{OGRE_HOME}/bin/debug/Plugin_ParticleFX_d.dll $ENV{OGRE_HOME}/bin/debug/Plugin_BSPSceneManager_d.dll $ENV{OGRE_HOME}/bin/debug/Plugin_CgProgramManager_d.dll $ENV{OGRE_HOME}/bin/debug/Plugin_PCZSceneManager_d.dll $ENV{OGRE_HOME}/bin/debug/Plugin_OctreeZone_d.dll $ENV{OGRE_HOME}/bin/debug/resources_d.cfg $ENV{OGRE_HOME}/bin/debug/plugins_d.cfg DESTINATION bin CONFIGURATIONS Debug ) endif(WIN32)


Replace OgreApp with any name you want.
CMAKE_MODULE_PATH points to where you installed the Ogre3D cmake modules.
OGRE_SAMPLES_INCLUDEPATH points to where you installed the Ogre3D Samples framework include directory.
This usually happens when you run make install.

resources.cfg & plugins.cfg

Linux

Copy 'plugins.cfg' and 'resources.cfg' from the Ogre media directory - usually '/usr/local/share/OGRE' - to your build directory:

Copy to clipboard
cp /usr/local/share/OGRE/plugins.cfg /path/to/OgreApp/build_directory/plugins.cfg cp /usr/local/share/OGRE/resources.cfg /path/to/OgreApp/build_directory/resources.cfg

Windows

The INSTALL project will copy the Ogre DLLs, resources.cfg, plugins.cfg and the media over.

How To Use It

After having created your OgreApp source directory, and populated it with the four files from the Ogre Wiki Tutorial Framework and the CMakeLists.txt file, you are now ready to build it using CMake.

  1. Open CMake-Gui, choose your source directory ('ogreapp') and a build directory ('ogreapp_build').
  2. Click 'Configure', choose a generator.
  3. Click 'Configure' again if CMake ran without fatal errors.
  4. Click 'Generate'.
  5. Navigate to your 'ogreapp_build' directory.

What you should do next depends on what generator you're using.

Visual Studio

  1. Open the 'OgreApp' solution and build the 'BUILD_ALL' project.
  2. Build the 'INSTALL' project - you only need to build this once to copy the Ogre DLLs, configuration files and media over.

MinGW

  1. Open a command prompt and issue the command: mingw32-make
  2. Install the Ogre files by doing a mingw32-make install

GCC on Linux

  1. In a console, issue the command: make
  2. And to install: make install (This install is local! No need to be root)

Possible Improvements

Custom configuration files

Instead of just blindly copying the configuration files from Ogre, one could generate leaner versions, with only the minimally required plugins and media.

Better Detection of Boost

The CMake script should handle the situation where Boost is only available in the Ogre SDK directory, ie when it's not installed on your machine.

History

Information Version
Sat 28 of Dec, 2024 15:43 GMT-0000 paroj 80
Thu 08 of Apr, 2021 21:22 GMT-0000 paroj 79
Thu 08 of Apr, 2021 21:22 GMT-0000 paroj 78
Thu 08 of Apr, 2021 21:20 GMT-0000 paroj 77
Sun 18 of Jun, 2017 11:18 GMT-0000 paroj 76
Fri 20 of Jan, 2017 01:47 GMT-0000 paroj 75
Sun 24 of Jan, 2016 13:33 GMT-0000 azmeuk 74
Sun 10 of Jan, 2016 16:35 GMT-0000 azmeuk 73
Sat 07 of Nov, 2015 11:45 GMT-0000 azmeuk 72
Sat 07 of Nov, 2015 11:44 GMT-0000 azmeuk 71
Thu 15 of Oct, 2015 19:02 GMT-0000 Sauermann Added v1.10 Windows framework 70
Tue 31 of Mar, 2015 06:09 GMT-0000 kabbotta 69
Tue 31 of Mar, 2015 05:59 GMT-0000 kabbotta 68
Tue 31 of Mar, 2015 05:56 GMT-0000 kabbotta 67
Tue 31 of Mar, 2015 05:54 GMT-0000 kabbotta 66
Tue 31 of Mar, 2015 05:54 GMT-0000 kabbotta 65
Tue 31 of Mar, 2015 05:53 GMT-0000 kabbotta 64
Tue 31 of Mar, 2015 05:53 GMT-0000 kabbotta 63
Fri 13 of Dec, 2013 05:57 GMT-0000 c6burns 62
Tue 10 of Dec, 2013 19:10 GMT-0000 c6burns added zip with changes for 1.9 61
Tue 13 of Nov, 2012 22:18 GMT-0000 jazztickets boost path was wrong for ogre 1.81 60
Thu 01 of Dec, 2011 21:48 GMT-0000 TcR1oo565 59
Tue 02 of Aug, 2011 14:07 GMT-0000 jacmoe 58
Sun 31 of Jul, 2011 23:34 GMT-0000 jacmoe 57
Thu 10 of Feb, 2011 10:53 GMT-0000 jacmoe 56