CMake with Eclipse:UNIX Makefile Generator

File System Setup

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 source code as the "Project Directory".
  3. Create an "External Tool" to run cmake from within Eclipse (Optional)

Note Regarding Out-Of-Source Builds: Eclipse CDT will work better if the the build directory is located within the source directory. Doing so makes it easier to configure Eclipse to Debug and Run your program. Itis possible to store your build directory completely outside of the source directory however setting up the ability to debug and run your targets will be slightly more complicated. Regardless of how you set things up, it is currently not possible to use the same source directory with multiple Eclipse projects. Eclipse stores a .project and .cproject file in the root of each project (your source tree in this case) and keeps track of active projects. It will not allow you to reuse a source directory twice (as it views them as project directories).

A typical project setup workflow would be something like this.

Initial Setup of your Project In a terminal program, "cd" into the top level of your project (source) directory ideally where your CMakeLists.txt file resides. Create a directory called "Build". cd into Build and then run 'ccmake' or 'cmake' using the parent directory as the target for cmake. Let us use the 'Example' directory that is a part of the CMake source distribution as the source folder for this tutorial.

[mjackson@Thor:]$ cd CMake-2.4.6/Example
[mjackson@Thor:]$ mkdir Build
[mjackson@Thor:]$ cd Build
[mjackson@Thor:]$ cmake ../

Basic Project Setup
Image showing the basic Project setup for our tutorial.

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

Eclipse Project Setup

Launch Eclipse and go to "New->C++ Project" Menu

New Project Menu


Select the MakeFile Project type, choose Other Toolchain and click Next

Project Wizard Makefile Selection


Select the Advanced Settings Button

Project Wizard Setting Project Type


Uncheck the Use Default Build Command
Insert the following in the Build Command Text Field:

make -C ${ProjDirPath}/Build VERBOSE=1
make -C ${ProjDirPath}/build VERBOSE=1 -j2

(or whatever path gets you to your build directory) "In the picture below, the name of the project is 'MXATools'" Project Wizard Advanced Options


Build Location: ${workspace_loc:/TrinityCore build}


Open the "C/C++ Build" node, and select "Settings".
 In the Right Side pane select the binary format for your system.
Click on the OK Button. Your project is now ready.

Project Wizard Setting binary Parser


Our Finished Project showing off 
some of the C++ semantic highlighting capabilities
Eclipse showing the CMakeEditor Plugin
Project Wizard
CMakeEditor Screen Shot

Creating an External Tool to run cmake (Optional)

In the course of normal development it is common to edit your CMakeLists.txt files. The makefile should pick up this difference and rerun cmake again. Most of the time this is perfectly sufficient and works just fine. Other times however this is NOT sufficient so having a convenient way to run CMake from within Eclipse is handy.

Times when this step is needed:

  • You do major edits to the CMakeLists.txt files and you want to be sure that cmake is picking everything up.
  • You would like to start from a clean build state with nothing in the build directory
  • You are developing your CMakeLists.txt file and you just want to see the outcome of running cmake on your project but do NOT need to actually build your project. For example you are configuring a file and just want to see the newly created/configured file


Creating an External Tool
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).
In the "Working Directory" test field insert the following: "${workspace_loc}/[NAME OF YOUR PROJECT DIRECTORY/Build" and in the Arguments section insert the following: "../"


Working Directory:   ${workspace_loc:/TrinityCore/build}


Arguments :          ../ -DSERVERS=true -DSCRIPTS=true  -DTOOLS=1 -DUSE_SCRIPTPCH=true   -DUSE_COREPCH=true -DWITH_WARNINGS=1 -DWITH_COREDEBUG=true -DPREFIX=/home/trinity/server/bin -DCONF_DIR=/home/trinity/server/etc  -DLIBSDIR=/home/trinity/server/lib


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.

Creating A Make Target To Run CMake (Optional)

Creating an external tool is one solution for running CMake from within Eclipse. However, a Make Target is even better for a couple of reasons:

  1. Make Targets are stored in the Eclipse .project file. Check this file into your version control system, and your Make Target will work in every one of your working copies, on every computer.
  2. It is a lot less complicated

You can create a Make Target in just a few easy steps:

  1. Find the Make Targets window (or open it using the menu command Window->Show View->Make Targets). It should appear on the right, with the Outline window.
  2. Select the folder that you want to use as the working directory in which to start the CMake command. Usually the project folder is what you want. Right click that folder and select Add Make Target
  3. Choose whatever name you want, like Run CMake
  4. Leave the Make Target field empty
  5. Set the build command to cmake -E chdir Build/ cmake -G "Unix Makefiles" ../
  6. That's it. The Make Target feature of Eclipse CDT is very useful because it can be used to execute any command, not just build commands. Take advantage of the CMake -E option to make your Make Targets portable across platforms.

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)

Automatic Discovery of Include directories (Optional, but handy)

Eclipse relies on default include paths such as /usr/include to find and index header files. One can manually enter more include paths from the "Project Properties" dialog but there is a much easier way for Eclipse to pick up on any 'non-default' include paths needed by your project. <p>

Eclipse has a mechanism in place to 'Discover' these locations. The mechanism works by parsing the compiler invocation looking for "-I" arguments then adds these directories to the list of include paths to index. In order for this discovery process to work you need to build your project at least once with the CMAKE_VERBOSE_MAKEFILE to true either by setting the variable in your CMakeLists.txt file 
SET(CMAKE_VERBOSE_MAKEFILE ON)
OR
by setting the "make" command to the following:
make -C ${workspace_loc}/[NAME OF YOUR PROJECT DIRECTORY/Build VERBOSE=1

Multiple Make Jobs for Multi-Core Hardware

If you have a multi-core hardware system (Intel Core Duo or the like) you can use the following to invoke more than one compile job:

make -C ${workspace_loc}/[NAME OF YOUR PROJECT DIRECTORY/Build -j2

Where '2' is the number of jobs to have make run.

True Out of Source Builds

You can still do out of source builds if you want. The make command would be the following if you have created your build directory at the same level as your project directory and called it "MyProjectBuild"

make -C ${workspace_loc}/[NAME OF YOUR PROJECT DIRECTORY/../MyProjectBuild

While this will work, what you will be missing is the list of binaries in your project view which makes setup of Debugging and Running your executable easier. All this can still be setup through custom run and debug commands in eclipse. I will leave that as an exercise to the reader.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值