Skip to main content

Build with CMake in Eclipse


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 Console window.

Take advantage of the CMake -E command 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 Debug configurations:

  • Display the Make Target window using the Window > 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 the Make target field is empty
    • In Build Command, deselect Use builder settings and set the Build command to

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

    • UPDATE 2023: Sander commented that Release/ must be replaced with absolute path ${ProjDirPath}/Release/. I no longer have any way to test this, but I assume Sander is right.
    • 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

    • UPDATE 2023: Again, you probably need to use ${ProjDirPath}/Debug/ in place of  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
          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
          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

Comments

Popular posts from this blog

The Reef Lounge

When I was about four, we vacationed at the Handerly Hotel & Resort in San Diego (formerly the Stardust Motor Hotel).  That place was heaven on earth as far as I was concerned. I loved the pool. Next to the pool there was a bar, and behind the bar was an underwater theater. It was here that I saw one of the most outlandish scenes I have ever witnessed.

Would You Like to Read a Removed Post?

You have been redirected to this page because you were looking for a post that I removed. From time to time I remove a post that I think is outdated, but I could be wrong. If you want to read a deleted post, please tell me by adding a comment to this post. Describe the article by telling me the title, URL or content. Thank you and sorry for the trouble.