Skip to content

Setting up a Project (CMake)

bvanevery edited this page Apr 4, 2020 · 20 revisions

This tries to be a shorter and easier version of http://urho3d.github.io/documentation/1.7.1/_using_library.html.

1. Copying and Creation of Needed Files

1.1. Copy required files and folders

Create a new empty folder which will be your project folder.

Then, in that folder, copy the files and folders listed below from the Urho3D engine source tree:

  • CMake/
  • cmake_generic.bat (or .sh for non windows)
  • .bash_helpers (on a Linux environment)

1.2. Copy required build scripts

Copy one or more of the other cmake_*.bat (Windows) or cmake_*.sh (Unix/Linux) files from Urho, depending on the build system you want to use. If you are for example using Codeblocks copy cmake_codeblocks.bat / cmake_codeblocks.sh.

1.3. Create folder structure

Create a bin folder with a CoreData and a Data folder inside. You may want to copy both from Urho to get same basic materials.

If your resources are located somewhere else on your system, then you can setup the following variable:

URHO3D_PREFIX_PATH=/path/to/your/resources/

Both Data and CoreData folders shall then be in the path /path/to/your/resources/.

In such a case, there is no need to have a both Data and CoreData under your bin folder.

1.4. Create cmake build file

Create a CMakeLists.txt and fill it with:

# Set project name
project (SampleProject)
# Define target name
set (TARGET_NAME SampleProject)

######################################

# Set CMake minimum version and CMake policy required by UrhoCommon module
cmake_minimum_required (VERSION 3.10.2)
if (COMMAND cmake_policy)
    # Disallow use of the LOCATION target property - so we set to OLD as we still need it
    cmake_policy (SET CMP0026 OLD)
    # Honor the visibility properties for SHARED target types only
    cmake_policy (SET CMP0063 OLD)
endif ()
# Set CMake modules search path
set (CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake/Modules)
# Include Urho3D Cmake common module
include (UrhoCommon)
# Define source files
define_source_files ()
# Setup target with resource copying
setup_main_executable ()

Replace SampleProject with your project name.

Note that UrhoCommon used to be called "Urho3D-CMake-common".

1.5. Create source files.

CMake needs at least one .cpp file to set the compiler and the linker correctly.
Your folder could now look like:

The Autoload folder may not be existing in the bin folder of your Urho because it's a rather new feature. Ressources in the Autoload folder are loaded when the application starts and not only when they are requested.

You might want to put your source code in a dedicated folder like src for example. In such a case, then you need to tell cmake to look at this folder to find your source files by completing the CMakeLists.txt file like this:

...
include (UrhoCommon)

file (GLOB SRC_CPP_FILES src/*.cpp)
file (GLOB SRC_H_FILES src/*.h)
define_source_files (GROUP EXTRA_CPP_FILES ${SRC_CPP_FILES} EXTRA_H_FILES ${SRC_H_FILES})
...

The define_source_files macro (defined in the UrhoCommon.cmake file) takes two extra parameters that are useful to pass additional files:

  • EXTRA_CPP_FILES
  • EXTRA_H_FILES

2. Prepare Build

This has to be done every time you add or remove source files or the project files created by CMake will not be able to build correctly.

If you have installed your Urho3D copy into a non standard location, then you can help cmake finding it by setting the following variable:

URHO3D_HOME = /path/to/your/Urho3D/non/standard/path/

Option 1: use a terminal and use the CMake script to generate the project build files:

You have to use the same options you used to build Urho (unless unimportant ones like "URHO3D_SAMPLES").

Option 2: You can also use the CMake GUI:

Note for Mac OS X and C++11 users: There is an issue with the default STL not fully supporting C++11. You need to switch the STL to libc++ with -stdlib=libc++ like

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")

3. Build

You can now open the created project file, add code and build it:

You can find code for a simple project in the next tutorial:
First Project