45

I have a native CMake project, and I want to use the Eclipse IDE with it so that it makes my development easier by providing auto-complete and and other features. I can't seem to find how to import the a CMake project in Eclipse.

PS: I am open for suggestions for other IDE's that work well with CMake.

0

6 Answers 6

55

KDevelop is an awesome IDE with great CMake support.

As for Eclipse - run this:

cd <project_dir>
cmake -G "Eclipse CDT4 - Unix Makefiles" ./

This will produce Eclipse project for you.

5
  • 1
    Note that you should really use a separate directory for building, but this will work.
    – byteit101
    Commented May 3, 2014 at 0:59
  • In my case it created just the CMakeFiles directory, CMakeCache.txt and cmake_install.cmake. :/ I also worked with the tutorial at the cmake.org, same result. Could the problem be that my cmake files define two different projects under same solution? Do you think that it can be the reason? Commented Oct 30, 2014 at 15:50
  • 2
    "Show Hidden Files" was the problem in my case :) Commented Oct 30, 2014 at 17:20
  • 2
    Note that CMake’s CDT project generator apparently is not maintained any more, see my answer below for an alternative (suggested to me by an Eclipse dev).
    – user149408
    Commented Aug 11, 2018 at 16:10
  • 2
    I tried for days making Eclipse and CMake work like I want it to. Just tried KDevelop and it makes a great first impression! Importing and building worked like a charm
    – smcs
    Commented Apr 3, 2019 at 14:05
31

Elaborating on arrowd's answer for Eclipse:

First, choose a directory for the CMake files. I prefer to keep my Eclipse workspaces in ~/workspaces and the source code in ~/src. Data which I need to build or test the project goes in subdirs of the project's workspace dir, so I suggest doing the same for CMake.

Assuming both your workspace and source folders are named someproject, do:

cd ~/workspaces/someproject
mkdir cmake
cd cmake
cmake -G "Eclipse CDT4 - Unix Makefiles" ~/src/someproject

Then, in your Eclipse workspace, do:

File > Import... > General > Existing Projects into Workspace

Check Select root directory and choose ~/workspaces/someproject/cmake. Make sure Copy projects into workspace is NOT checked.

Click Finish and you have a CMake project in your workspace.

Two things to note:

  • I used cmake for the workspace subdir, but you can use a name of your choice.
  • If you make any changes to your build configuration (such as editing Makefile.am), you will need to re-run the last command in order for Eclipse to pick up the changes.
0
12

I just learned that CMake’s CDT project generator appears to be unmaintained and has caused various problems—especially, it seems, with later versions of Eclipse (of which I had my share as well).

The recommendation is to use cmake4eclipse (available from Eclipse Marketplace), which uses CMakeLists.txt as its only source of truth. It also allows you to keep the source tree separate from your workspace.

  • In Eclipse, select File > New > C Project.
  • Enter a project name and choose the root of your source tree as the project location.
  • Select Executable > Empty Project as the project type, and accept the default toolchain.
  • Click Finish, and you have a CMake project in your workspace.
2
  • The project generator didn't work for me either, but this worked!
    – Jesper
    Commented Sep 3, 2019 at 14:48
  • I tried this, but Eclipse does not recognize the flags I have set in CMakeLists.txt, i.e.: set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMYVAR=5") Eclipse error: Symbol 'MYVAR' could not be resolved Any ideas how to solve this? Btw, I tried the same with VS Code with the "CMake Tools" extension - same error...
    – Guti_Haz
    Commented Jun 7, 2020 at 17:49
3

In short, only the CDT generator is currently a viable option. Why? Simply because Eclipse must obtain the preprocessor defines, include paths, and what files are included in the build from somewhere. Otherwise we would end up indexing everything, and badly, without the correct macros being defined. If you have boost in your repo, or a medium or large sized tree, indexing everything simply isn't going to work in the real world - not with speed and reliability of Eclipse's indexer anyway. Since most people probably want to use Ninja for builds these days (who wants to wait 30+ secs for a warm build just to see what is dirty?), that rules out the only way of importing this information via makefiles that is currently supported, unless you want to have to generate two separate build systems every time you configure, which would make automation a real pain (given that cmake re-runs itself when the lists change).

In Eclipse Photon there is a new option to import cmake projects directly. However, at this stage, I'd say it looks mostly useless for anything other than trivial projects, since there doesn't seem to be any way to import an already configured build directory, or anywhere to set variables and arguments that get passed to cmake. I don't yet know how this feature actually works, but it would seem that Eclipse must be parsing through the hierarchy of CMakeLists, following the logic to see what add_subdirectory() calls are made, and what preprocessor defines are set, which is seemingly an approach with no future, given that we have cmake server mode for exactly this, and it will no doubt require reimplementation of almost all of a cmake language parser in Eclipse to make this work.

So far, the only viable solution that seems to meet real world requirements appears to be to use the cmake CDT generator. It can get the above mentioned information directly from inside cmake, and write it into the .cproject file. As previously mentioned, it is not really maintained, and relies on an outdated template for the .cproject, which causes some issues.

My own minimal requirements for cmake support are:

  • Ability to import an already configured project. Large build systems often use scripts to pass numerous variables into cmake on the command line.
  • Only what is part of the build system should be indexed.
  • It must work with out of source tree builds.
  • Must use preprocessor defines and include paths from build system to have working indexing.
  • Must work with Ninja generator (ie. no makefiles generated).

I can't see how to do any of the above with anything except the CDT generator at present, and the only way to achieve a working and refined solution requires either some hacking at cmake's generator, or post processing of the generated .cproject to be selective about what gets indexed (ie. not all of CMAKE_SOURCE_DIRECTORY). This is a another hack (since cmake doesn't have any way to attach post-configure tasks into the configure step, we have to fork a process, and monitor the parent cmake for termination. It is a deliberate decision on behalf of the cmake developers apparently, and perhaps rightly so)

I'd certainly really appreciate information of any better approach. The state of cmake support in Eclipse is pretty dismal for a tool that is supposed to be for C++ development. The indexer (at least when it isn't plagued by its regular lockups that require restarting of Eclipse), is actually amongst the best around, and Eclipse is a very good environment for jumping around the code.

3

Do the follows:

mkdir debug (or release, or any other name)

cd debug
cmake -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT=TRUE -DCMAKE_ECLIPSE_MAKE_ARGUMENTS=-j3 -DCMAKE_ECLIPSE_VERSION=4.1 ../

Make sure you set the right Eclipse version

Then open the folder from Eclipse (Open Projects)

1

One simple solution on my desktop:

  1. In eclipse: New -> New C/C++ Project -> Empty or Existing CMake Project, choose a project name (e.g. project).
  2. Copy all you files of the old directory to the new one (path to your project).

Not the answer you're looking for? Browse other questions tagged or ask your own question.