History: Building Ogre3D 1.9 Statically in Mac OS X (Yosemite)
Preview of version: 62
- «
- »
The idea of this tutorial is to help you build Ogre3D 1.9 statically in Mac OS X (Yosemite). Additionally this tutorial will explain you how to run your first application from scratch using CMake and the QtCreator IDE.
Table of contents
Prerequisites
- You need to have a basic C++ background.
- Basic knowledge about how to use a terminal in Mac OS X (create folders, list directory contents, run commands, etc.).
- An Internet connection (this sounds obvious, but it’s better to clarify 😊 ).
- Have the following software installed (there are no specific restrictions regarding the versions used, but it's better to use the latest):
- Xcode.
- Mercurial.
- QtCreator.
- CMake.
- When this tutorial refers to the terminal, it's refering to the Mac OS X Terminal.
- When entering a command in the terminal, this tutorial refers to the current path location with the following notation:
$> cd /folder1/folder2 /folder1/folder2 $> pwd /folder1/folder2
Building Ogre
First, create a folder where the whole Ogre3D project will live:
$> sudo mkdir -p /opt/dev
You can create this folder in another location if you wish. This is not a blocker. The important thing is that you keep in mind where the folder was created to use it from now on.
Downloading Ogre3D’s Dependencies
Create the folder where the dependencies will reside:
/opt/dev/ $> mkdir dependencies
Go to the following link and download ogre3D’s dependencies:
Unzip the file you downloaded. This will contain a folder called Dependencies. Copy the content of that folder to the following location:
/opt/dev/dependencies
You should have something like this:
These dependencies include:
- Boost: It’s a set of open source libraries that extend the functionality of C++.
- Freeimage: it’s an Open Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today's multimedia applications.
- Freetype: It’s is a freely available software library to render fonts.
- OIS: It’s a code library for constructing a human-computer interface with input devices such as a keyboard, mouse or game controller.
- zlib: It’s a software library used for data compression.
- zziplib: it offers the ability to easily extract data from files archived in a single zip file.
The previous downloaded dependencies are static libraries (end up with the extension .a). Normally in other platforms when you build a library for 32 and 64 bits you end up with two different library files (one for 32 and other for 64). Here in Mac OS X, you can build for 32 AND 64 bits and you end up with a single file. That single file is what is called a “flat” binary or a “universal” binary. In order to check if a library is for 32 bits or 64 bits or both, just run the following command in a terminal:
/opt/dev/dependencies/lib/Release $> lipo -info libOIS.a Architectures in the fat file: libOIS.a are: x86_64 i386
Downloading and Building Ogre3D
it’s always better to use the latest stable version from the project’s repository. To do that you should use Mercurial (Mercurial is a distributed version control system). To validate if Mercurial is installed correctly, in a terminal write:
$> hg Mercurial Distributed SCM basic commands: add add the specified files on the next commit annotate show changeset information by line for each file clone make a copy of an existing repository commit commit the specified files or all outstanding changes diff diff repository (or selected files) export dump the header and diffs for one or more changesets forget forget the specified files on the next commit init create a new repository in the given directory log show revision history of entire repository or files merge merge working directory with another revision pull pull changes from the specified source push push changes to the specified destination remove remove the specified files on the next commit serve start stand-alone webserver status show changed files in the working directory summary summarize working directory state update update working directory (or switch revisions) use "hg help" for the full list of commands or "hg -v" for details
To download the Ogre3D's code, write in a terminal:
$> cd /opt/dev /opt/dev/ $> hg clone https://bitbucket.org/sinbad/ogre ogre3d-1.9.0
The last part of the previous command (ogre3d-1.9.0) will be the name of the folder where the source code will be downloaded. The download process will take something like 20 minutes (although it depends of your Internet connection).
If everything went okay, the folder should be around 460 MB:
opt/dev/$> du -sh /opt/dev/ogre3d-1.9.0/ 461M /opt/dev/ogre3d-1.9.0/
Now it's time to build Ogre3D statically (before running the cmake command, validate that your paths are consistent. After that, just build):
/opt/dev/ogre3d-1.9.0 $> mkdir build /opt/dev/ogre3d-1.9.0 $> cd !$ /opt/dev/ogre3d-1.9.0/build $> cmake -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -v -stdlib=libc++ -std=c++11" -D BOOST_ROOT=/opt/dev/dependencies/include/boost -D BOOST_INCLUDEDIR=/opt/dev/dependencies/include/boost -D BOOST_LIBRARYDIR=/opt/dev/dependencies/lib -D OGRE_DEPENDENCIES_DIR=/opt/dev/dependencies -D OGRE_STATIC=1 -D OGRE_BUILD_SAMPLES=1 -G Xcode ..
Here is the explanation for every command:
| Command | Description |
|---|---|
| CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -v -stdlib=libc++ -std=c++11” | Defines support for C++11. |
| BOOST_ROOT=/opt/dev/dependencies/include/boost | Defines the location of the Boost base folder. |
| BOOST_INCLUDEDIR=/opt/dev/dependencies/include/boost | Defines the location of the Boost’s include folder. |
| BOOST_LIBRARYDIR=/opt/dev/dependencies/lib | Defines the location of the Boost’s library folder. |
| OGRE_DEPENDENCIES_DIR=/opt/dev/dependencies | Defines the location of the Ogre3D’s dependencies folder. |
| OGRE_STATIC=1 | Defines that Ogre3D will be built statically. |
| OGRE_BUILD_SAMPLES=1 | Defines that the Ogre3D’s samples will be built. |
| -G Xcode | Specify a makefile generator (Xcode in this case). |
| .. | The couple of dots indicate the location of the CMakeLists.txt file (which is the file that cmake needs to create all the make files). |
After building Ogre3D you’re going to end with the following files:
/opt/dev/ogre3d-1.9.0/build $> ls -l OGRE.* OGRE.build: total 0 drwxr-xr-x 5 USER wheel 170 Dec 8 19:14 Debug drwxr-xr-x 6 USER wheel 204 Dec 8 19:22 Release OGRE.xcodeproj: total 5400 -rw-r--r-- 1 USER wheel 2763182 Dec 8 19:05 project.pbxproj drwxr-xr-x 4 USER wheel 136 Dec 8 19:14 project.xcworkspace drwxr-xr-x 3 USER wheel 102 Dec 8 19:14 xcuserdata
The OGRE.xcodeproj file is the Xcode project configuration file for the Ogre3D’s construction. Go to Finder and double click over that file. This will open Xcode.
Note: This tutorial will use Xcode only to build Ogre3D. Afterwards you’re not going to use it anymore.
This is what you’re going to see when Xcode opens with the Ogre3D project:
Go to Product → Scheme → Edit Scheme.. and change the Build Configuration to Release:
Select the install scheme and change the Build Configuration to Release:
Select again the ALL_BUILD scheme:
Close the dialog by clicking the Close button. Now in order to start the building process, in the menu principal select Product -> Build or press Command + B.
To view the building log, select View → Navigators → Show Report Navigator. The building progress can be viewed in the blue progress bar in the Xcode’s top center window area:
The whole building process is going to take something like 20 minutes in a computer with Processor: 1.3 GHz Intel Core i5 and Memory: 4 GB 1600 MHz DDR3.
Running the Ogre3D’s Samples
Now that the building process has finished you can run the Ogre3D’s samples. Open the following file in Finder:
/opt/dev/ogre3d-1.9.0/build/bin/Release/SampleBrowser.app
You should be able to see the sample browser application called SampleBrowser.app. Additionally, you can see the sample browser’s log file in the following path:
/Users/YOURUSERNAME/Library/Application Support/Ogre/Xalafu/ogre.log
Using QtCreator
Now that Ogre3D is finally compiled, let’s take a look at the IDE that we’re going to use during this tutorial: QtCreator. This IDE includes:
- Cross-platform.
- Also used for projects non-related to Qt (Qt is a cross-platform application framework).
- Visual Debugger.
- Support for CMake.
- Excellent Vim plugin.
- Good integration with tools: Git, Subversion, Mercurial.
Before using QtCreator for the first time, you need to create a Build Kits in QtCreator (assuming that you just have installed QtCreator and don’t have any Build Kits configured). Open QtCreator's preferences or simply hit Command + . On the left panel select the Build and Run option:
Select the Add button (on the upper-right side). A sub-dialog will be shown, fill the Kits name information:
Creating a Project (that will use CMake)
From the File menu select the option New File or Project…. You will see a dialog like this:
Select the option Non-Qt Project from the Projects list and subsequently select the Plain C++ Project (CMake Build) option. Afterwards click the Choose… button (located at the bottom-right side of the dialog). Afterwards set the project’s name and location:
The information used for this tutorial in the previous dialog (the folder ogreprojects was created beforehand):
Name: tutorial1 Create in: /opt/dev/ogreprojects
Then click the Continue button. In the following dialog select a version control if you are going to use one. This tutorial won't use any:
Then click the Done button. You now need to define the Build Location folder in the following dialog:
/opt/dev/ogreprojects/tutorial1/build
Click the Continue button. In the following dialog you will run the project’s cmake configuration file. Click the Run CMake button and you should see something like this:
From the Build menu select the Build All option. The Compile Output window (located at the bottom) will tell you the building information. If everything went okay, from the Build menu select the Run option. You should see the programs output in the Application output (located at the bottom) window:
Integrating Ogre3D in your Project
Now that you have a basic C++ project running, the idea is to integrate the Ogre3D libraries inside the project.
Your project has two files:
- CMakeLists.txt (project configuration file)
- main.cpp (source code file)
Here is the current code for both:
CMakeLists.txt:
project(tutorial1) cmake_minimum_required(VERSION 2.8) aux_source_directory(. SRC_LIST) add_executable(${PROJECT_NAME} ${SRC_LIST})
main.cpp:
#include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; }
Before changing anything on those files, let's reorganize the projects files:
/opt/dev/ogreprojects/tutorial1 $> ls -l total 40 -rw-r--r-- 1 USER wheel 329 Dec 10 21:10 CMakeLists.txt drwxr-xr-x 9 USER wheel 306 Dec 10 21:10 build drwxr-xr-x 3 USER wheel 102 Dec 10 21:09 main.cpp /opt/dev/ogreprojects/tutorial1 $> mkdir src /opt/dev/ogreprojects/tutorial1 $> mv main.cpp src/
Now perform the following changes in the CMakeLists.txt file:
PROJECT(tutorial1) CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++11") MESSAGE(STATUS ${PROJECT_SOURCE_DIR}/src) FILE(GLOB_RECURSE SRCS "${PROJECT_SOURCE_DIR}/src/*.cpp") FILE(GLOB_RECURSE HDRS "${PROJECT_SOURCE_DIR}/src/*.h") ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS} ${HDRS})
- Line 4: This helps to activate the C++11 support.
- Line 6: Printing the Project source dir only for informative purposes.
- Line 8-9: This is the CMake way to include the source code files in your project. From now on every source code should go inside the src folder. CMake will search recursively inside this folder.
- Line 11: This is the CMake way to say: "Create an executable using the project's source code...and name the executable file just like the project's name".
Run the CMake command, you should see something like this:
-- /opt/dev/ogreprojects/tutorial1 -- Configuring done -- Generating done -- Build files have been written to: /opt/dev/ogreprojects/tutorial1/build
Build and run your project. You shouldn't get any errors.
Binaries and App Bundles
In the terminal do:
$> cd /opt/dev/ogreprojects/tutorial1/build /opt/dev/ogreprojects/tutorial1/build $> ls -l total 144 0 drwxr-xr-x 9 USER wheel 306 Dec 10 21:29 . 0 drwxr-xr-x 7 USER wheel 238 Dec 10 21:30 .. 16 -rw-r--r--@ 1 USER wheel 6148 Dec 10 21:30 .DS_Store 56 -rw-r--r-- 1 USER wheel 26419 Dec 9 20:11 CMakeCache.txt 0 drwxr-xr-x 13 USER wheel 442 Dec 10 21:30 CMakeFiles 16 -rw-r--r-- 1 USER wheel 5044 Dec 10 21:29 Makefile 8 -rw-r--r-- 1 USER wheel 1441 Dec 9 20:11 cmake_install.cmake 32 -rwxr-xr-x 1 USER wheel 15228 Dec 10 21:10 tutorial1 16 -rw-r--r-- 1 USER wheel 5350 Dec 10 21:29 tutorial1.cbp
Do you see the file called tutorial1?, that is the project's executable (or binary). In order to execute it, in the terminal run:
/opt/dev/ogreprojects/tutorial1/build $> ./tutorial1 Hello World
The project has now a executable and you're able to run it. But things need to change a little bit in order to adapt in the Mac OS X's way of handling executables. In the Mac OS X platform you usually use what is called an Application Bundle (a.k.a app bundle). An app Bundle is a (special) directory that allows related resources such as an application's executable and its graphics to be grouped together, appearing as a single file to the user.
In order for the project to create an app bundle instead of a plain binary we need to change something in the CMakeLists.txt file:
ADD_EXECUTABLE(${PROJECT_NAME} MACOSX_BUNDLE ${SRCS} ${HDRS})