Build with CMake in Eclipse

From:http://www.voom.net/use-cmake-with-eclipse#create-eclipse-make-targets


Build with CMake in Eclipse

by JOHN MCGEHEE on JANUARY 17, 2011

Cross-platform CMake can generate a wide variety of build systems.  The CMake 2.8 project generator for  Eclipse does not work, so you must create the project and configure it to build with GNU Make.  Here’s how to do it on Linux.

Create an Eclipse Project

Create an Eclipse CDT (C/C++ Development Tooling) project using the File > New > C++ Project command for your C++ project, or File > New > C Project for a C project.

Do not create the project using the CMake Eclipse project generator.

Create Eclipse Make Targets

Tip: The Make Target feature of Eclipse CDT can be used to execute any command, not just build commands. The standard output from the command appears in the Eclipse Consolewindow.

Take advantage of the CMake -Ecommand to create cross-platform Make Targets. Note that the Build command field accommodates only a single command–a semicolon separated list of multiple commands does not work.

The conventional approach to using CMake with Eclipse is to create an external tool in Eclipse. However, a Make Target is simpler, and because it is stored in the Eclipse .project file, you can check it into your version control system and it will work in every one of your working copies, on every computer.

Create a Make Target for each configuration that you want to build.  Here I assume that you have the usual Release and Debugconfigurations:

  • Display the Make Target window using theWindow > Show View > Make Target menu command. It should appear on the right, with the Outline window.
  • Select the folder for the project for which you want to add CMake.  CMake will run with this folder as its working directory.
  • Right click on the folder and select New from the context menu.  The Create Make Target dialog will appear.
    • Type Target name CMake Release
    • In Make target, deselect Same as the target name, and make sure that theMake target field is empty
    • In Build Command, deselect Use builder settings and set the Build commandto

      cmake -E chdir Release/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Release

    • Click OK
  • Repeat, this time for Target name CMake Debug, and Build command,

    cmake -E chdir Debug/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Debug

  • Create the Release/ and Debug/ directories

    mkdir Release Debug

Set Up the Eclipse CDT Builder

Next, set up the CDT builder to run the Makefiles that CMake builds.

  • Right click on a CDT project.  In the context menu, select Properties.
  • On the left, select C/C++ Build
    • Set Configuration to Release
      • Choose the Builder Settings tab
        • Deselect Use default build command
        • Specify the Build command:

          make -C ${ConfigName}

        • Deselect Generate Makefiles automatically
        • Make the Build directory field blank
        • The form should appear as shown below:Eclipse C/C++ Builder Settings set up for CMake

          Eclipse C/C++ Builder settings for CMake

          Click to Enlarge

      • Choose the Behavior tab
        • Select Build (Incremental build) and specify the target name all
        • Select Clean and specify the target name clean
        • The form should appear as shown below:Eclipse C/C++ Build Behavior Settings set up for CMake

          Eclipse C/C++ Build Behavior settings for CMake

          Click to Enlarge

    • Set Configuration to Debug
      • Choose the Builder Settings tab
        • Set all values exactly the same as the Release configuration
      • Choose the Behavior tab
        • Set all values exactly the same as the Release configuration
  • Click OK

Build the Project

Use CMake to generate an out-of-source GNU Make build system:

  • In the Make Targets window, double click on CMake Release or CMake Debug to generate the GNU Make build system in Release/ or Debug/, respectively
  • If necessary, edit your CMakeLists.txt control files
  • Delete the contents of the corresponding build directory. For example:

    rm -r Release/*

    and repeat.

    Actually, for minor edits to your CMakeLists.txt control files, you need not delete the build directory. However, I cannot tell you exactly what the threshold for “minor edits” is.

Now, build the project the usual way with Eclipse:

  • Select the configuration to build (Release of Debug) with the Project > Build Configurations > Set Active command
  • Build with the Project > Build Project command
  • Edit your source code files, and repeat
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值