- OpenSceneGraph 3.0: Beginner's Guide
- Rui Wang Xuelei Qian
- 596字
- 2021-03-27 00:35:41
Time for action—building applications with CMake
Before constructing your own project with CMake scripts, it could be helpful to keep the headers and source files together in an empty directory first. The second step is to create a CMakeLists.txt
file using any text editor, then and start writing some simple CMake build rules.
- The following code will implement a project with additional OSG headers and dependency libraries. Please enter them into the newly-created
CMakeLists.txt
file:cmake_minimum_required( VERSION 2.6 ) project( MyProject ) find_package( OpenThreads ) find_package( osg ) find_package( osgDB ) find_package( osgUtil ) find_package( osgViewer ) macro( config_project PROJNAME LIBNAME ) include_directories( ${${LIBNAME}_INCLUDE_DIR} ) target_link_libraries( ${PROJNAME} ${${LIBNAME}_LIBRARY} ) endmacro() add_executable( MyProject main.cpp ) config_project( MyProject OPENTHREADS ) config_project( MyProject OSG ) config_project( MyProject OSGDB ) config_project( MyProject OSGUTIL ) config_project( MyProject OSGVIEWER )
- We have only added a
main.cpp
source file here, which is made up of the "Hello World" example and will be compiled to generate an executable file namedMyProject
. This small project depends on five major OSG components. All of these configurations can be modified to meet certain requirements and different user applications, as explained in the following chapters. - Next, start
cmake-gui
and drag yourCMakeLists.txt
into the GUI. You may not be familiar with the CMake scripts to be executed, at present. However, the CMake wiki will be helpful for further understanding: http://www.cmake.org/Wiki/CMake. - Follow the step-by-step instructions provided in the last chapter to create and build a Visual Studio solution or a makefile.
- The only point is that you have to ensure that your CMake software version is equal to or greater than 2.6, and make sure you have the
OSG_ROOT
environment variable set. Otherwise, thefind_package()
macro may not be able to find OSG installations correctly. The following image shows the unexpected errors encountered because OSG headers and libraries were not found in the path indicated byOSG_ROOT
(or the variable was just missed): - Note that, there is no
INSTALL
project in the Visual Studio solution, or anymake install
command to run at this time, because we don't write such CMake scripts for post-build installations. You could just run the executable file in the build directory directly.
What just happened?
CMake provides easy-to-read commands to automatically find dependencies for user projects. It will check preset directories and environment variables to see if there are any headers and libraries for the required package.
The environment variable OSG_ROOT
(OSG_DIR
is OK, too) will facilitate in looking for OSG under Windows and UNIX, as CMake will first search for valid paths defined in it, and check if there are OSG prebuilt headers and libraries existing in these paths.
Pop quiz—configuring OSG path options yourselves
Your CMake may not be able to find the OSG headers and development files for special reasons, for instance, the headers and libraries may be placed in different places, or you just intend to use a distribution different from the one set by OSG_ROOT
or OSG_DIR
.
Can you set CMake options yourselves at this time? There are often three options in each OSG-related group (OPENTHREADS
, OSG, OSGDB
, and so on), such as OSG_INCLUDE_DIR, OSG_LIBRARY
, and OSG_LIBRARY_DEBUG
. What do they mean, in your opinion?
Have a go hero—testing with different generators
Just try a series of tests to generate your project, using Visual Studio, MinGW, and the UNIX gcc compiler. You will find that CMake is a convenient tool for building binary files from source code on different platforms. Maybe this is also a good start to learning programming in a multi-platform style.