CMake:Eclipse UNIX Tutorial: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
No edit summary
Line 64: Line 64:
If you edit your CMakeLists.txt file, then you will need to go back out to your terminal and rerun cmake. OR you can create an external program launch configuration and run cmake from within eclipse. To do this select the "Run->External Tools->Show External Tools Dialog..." menu.
If you edit your CMakeLists.txt file, then you will need to go back out to your terminal and rerun cmake. OR you can create an external program launch configuration and run cmake from within eclipse. To do this select the "Run->External Tools->Show External Tools Dialog..." menu.
Create a new "Program" and call it Cmake. in the "Location" text field, type the absolute path to cmake (/usr/local/bin/cmake). If the "Working Directory" area insert the following: "${project_loc}/Build" and in the Arguments section insert the following: "../". In the "Refresh Tab" Select ""The project containing the selected resources". In the "Common" tab check the "External Tools" selection. This will put a shortcut in the "Run" menu for you. Click the Apply Button and then run. cmake will now run on your project directory.
Create a new "Program" and call it Cmake. in the "Location" text field, type the absolute path to cmake (/usr/local/bin/cmake). If the "Working Directory" area insert the following: "${project_loc}/Build" and in the Arguments section insert the following: "../". In the "Refresh Tab" Select ""The project containing the selected resources". In the "Common" tab check the "External Tools" selection. This will put a shortcut in the "Run" menu for you. Click the Apply Button and then run. cmake will now run on your project directory.
== Parsing Errors more efficiently ==
The CDT Error Parser cannot handle error messages that span more than one line, which is the default gcc behavior. In order to
force gcc to generate single line error messages with no line wrapping, add to your CMakeLists.txt:
IF(CMAKE_COMPILER_IS_GNUCC)
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmessage-length=0")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
IF(CMAKE_COMPILER_IS_GNUCXX)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmessage-length=0")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)

Revision as of 13:57, 4 July 2007

Obtaining Eclipse + CDT

Eclipse IDE is an IDE written in Java that was initially for developing java programs. In the years lots of projects have used eclipse as a base for their own applications. One such project is the "C Development Tools" or CDT [http://

Download Eclipse from Here. Download Page

CDT can be downloaded from here. CDT Download

It is important to match CDT with the proper Eclipse version.


CMake Editor Plugin for Eclipse

There is also an editor plug-in for Eclipse available (see above): CMake Editor.

Eclipse + CDT Version Information

CDT 3.1.2 goes with Eclipse 3.2 --> These are the stable versions as of May 2007.
CDT 4.0 is a Major step forward and needs Eclipse 3.3 to run. Both of these projects are in Beta release as of May 2007. The final releases are supposed to be around mid June 2007.
As of June 4 2007, if you want to try the latest versions then Eclipse 3.3RC2 and CDT 4.0 RC2 are the matched pair that you will need.

Getting Eclipse updated with CDT is fairly easy if you download the tar.gz files for both Eclipse and CDT. Untar the Eclipse distribution. This will create a directory called "eclipse". Now untar CDT distribution so that the output from CDT merges with the "eclipse" directory. This is easiest done if both the eclipse distribution and the cdt distribution files are in the same directory. Then merely run tar -xvzf on both tar.gz files.

Basic Tutorial

Eclipse CDT supports projects that already provide a makefile so this is good news for CMake users.

The basic steps to get Eclipse + CDT + CMake working are:

  1) Use CMake to generate makefiles for your project
  2) Create a new project in Eclipse using the top level directory of your project as the "Project Directory". The type of project to create should be a "Makefile C++"
  3) Create an "External Tool" to run cmake from within Eclipse

Eclipse CDT can interoperate with CMake by creating a "Standard Makefile" project, running CMake, and then having CDT to run make on the generated makefiles. Eclipse CDT works better if the build files are somewhere in project directory such as a subdirectory (for example Build).

A typical workflow would be something like this.

In a terminal program "cd" into the top level of your project directory ideally where your CMakeLists.txt file resides. Create a directory called "Build". cd into Build and then run ccmake/cmake using the parent directory as the target for cmake. So, for example if we have a project called "MyGreatProject" located in /Users/mike/Projects, then the terminal commands would look like the following:

[mjackson@Thor:]$ cd /Users/mike/Projects/MyGreatProject
[mjackson@Thor:]$ mkdir Build
[mjackson@Thor:]$ cd Build
[mjackson@Thor:]$ ccmake ../

At this point we have bootstrapped the process somewhat so Eclipse throws less errors when we setup the Eclipse Projects.

Launch Eclipse. Select /Users/mike/Projects as the "Workspace" (using the example from above). Go to "File->New Project" Select C++ Standard Makefile Project Go through the wizard making the appropriate selections and entries. At some point during this wizard there will be a tab that allows you to over ride the standard "make" command invocation. The tab may have a title such as "Make Builder". Uncheck "Use default" and enter the following into the "Build Command" text field.

"make -C ${project_loc}/Build" ( Without the quotes). For multi-core systems and if you want to use multiple compile jobs, then use:

make -j2 -C ${project_loc}/Build (where '2' is the number of jobs to use.Adjust for your own system)

There will also be another tab called "Binary Parsers". Select that tab and be sure that the appropriate binary type is selected for your system. Continue on with the wizard until finished.

Your new project should now be ready. Select the project in the C/C++ Projects view then select the "Project->Build Project" Menu to build your project.

Now, technically you _could_ use a completely out of source build and this will work with eclipse, but eclipse will be unable to find your binaries when finished, which makes the setup of debugging and running your app from Eclipse a bit more tricky.

Creating an External Tool to run cmake

If you edit your CMakeLists.txt file, then you will need to go back out to your terminal and rerun cmake. OR you can create an external program launch configuration and run cmake from within eclipse. To do this select the "Run->External Tools->Show External Tools Dialog..." menu. Create a new "Program" and call it Cmake. in the "Location" text field, type the absolute path to cmake (/usr/local/bin/cmake). If the "Working Directory" area insert the following: "${project_loc}/Build" and in the Arguments section insert the following: "../". In the "Refresh Tab" Select ""The project containing the selected resources". In the "Common" tab check the "External Tools" selection. This will put a shortcut in the "Run" menu for you. Click the Apply Button and then run. cmake will now run on your project directory.

Parsing Errors more efficiently

The CDT Error Parser cannot handle error messages that span more than one line, which is the default gcc behavior. In order to force gcc to generate single line error messages with no line wrapping, add to your CMakeLists.txt:

IF(CMAKE_COMPILER_IS_GNUCC)
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmessage-length=0")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
IF(CMAKE_COMPILER_IS_GNUCXX)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmessage-length=0")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)