Introduction

Most of the code we develop here uses CMake as its build system.  That is because CMake is highly portable (works across many platforms).

However, CMake can also generate projects for various Integrated Development Environments (IDEs) such as XCode and Eclipse.  This post will show how to use CMake to generate projects that are fully functional within XCode or Eclipse.

Generating Eclipse Projects

Step 1 – Generating the Eclipse Project

Create a directory somewhere where you will build the code.

cmake -G "Eclipse CDT4 - Unix Makefiles" [source-dir]

where [source-dir] is the path to where you have the source files located. Note that you can add any other flags to CMake as you normally would. Or you can run ccmake rather than cmake.

Step 2 – Importing into Eclipse

From within eclipse, choose File -> Import and choose Existing Projects into Workspace (under the “General” category). Then navigate to the location where you generated the build system in step 1 above and click “Finish”

You should now have a functional build within Eclipse. That means that you can have code completion, refactoring, building, debugging, and all of the other cool integrated tools in Eclipse.

Generating XCode Projects

Create a directory somewhere where you will build the code.

cmake -G XCode [source-dir]

where [source-dir] is the path to where you have the source files located.

At this point, the XCode project will be created. You can then open it from Finder or from the command line.

More information

To learn more about the project generators available on your platform, type

cmake --help

and look for the Generators section of the output.  It may look something like the following:

Generators

The following generators are available on this platform:
  Unix Makefiles              = Generates standard UNIX makefiles.
  Ninja                       = Generates build.ninja files (experimental).
  Xcode                       = Generate Xcode project files.
  CodeBlocks - Ninja          = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  CodeLite - Ninja            = Generates CodeLite project files.
  CodeLite - Unix Makefiles   = Generates CodeLite project files.
  Eclipse CDT4 - Ninja        = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles
                              = Generates Eclipse CDT 4.0 project files.
  KDevelop3                   = Generates KDevelop 3 project files.
  KDevelop3 - Unix Makefiles  = Generates KDevelop 3 project files.
  Kate - Ninja                = Generates Kate project files.
  Kate - Unix Makefiles       = Generates Kate project files.
  Sublime Text 2 - Ninja      = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles
                              = Generates Sublime Text 2 project files.
Categories: Uncategorized