34
votes

How to configure Eclipse "Helios" with plugin CDT for cmake?

cmake all 
CMake Error: The source directory "D:/javaworkspace/workspace/Planner/Debug/all" does not    exist.

Eclipse always wants to use 'all' option and I don't know how to stop its to not use it.

I've seen that in "Build behavior" section, in "Preference" there have been 'all' option. I erased this, but it still works wrong (this same error).

7
You should use eclipse to generate CDT project files. It's not make replacement.arrowd

7 Answers

40
votes

In Eclipse-CDT you do not create cmake projects but you import cmake projects. This is what you should do:

  1. Say the source of your CMake project named "Planner" is located in D:/javaworkspace/src/Planner

  2. Create a folder (the folders NEED to be parallel to each other): D:/javaworkspace/build/Planner

  3. Go to the folder D:/javaworkspace/build/Planner and run CMake using the Eclipse generator:

     cmake ../../src/Planner -G"Eclipse CDT4 - Unix Makefiles"
    

    This will generate the make files for your Planner project.

  4. To import this in Eclipse do the following:

    File -> Import -> C/C++ -> Existing code as Makefile project

    and select D:/javaworkspace/build/Planner (the build output folder with the make files) as the "Existing Code location"

However, looking at your paths it seems to me that you are working on Windows. In windows CMake can generate Visual Studio projects. If you want to use CMake I suggest first creating a "hello world" project using CMake (remember, Eclipse does not create CMake projects, you have to create a CMakeLists.txt file by hand)

26
votes

What worked best for me is cmake4eclipse. It's a plugin that is available via the marketplace.

Portions from the cmake4eclipse help text:

CMake for CDT requires an existing C/C++ project to work with. It allows to use cmake as the generator for the makefiles instead of the generator built-in to CDT. See Enabling CMake buildscript generation for details.

To set up a new project with existing source code, please follow these steps:

  • Check out your source code.
  • Open the new C/C++ project wizard ("File" => "New" => "C Project" or "File" => "New" => "C++ Project").
    • Make sure the project location points to root directory of your checked out files
    • For the project type, select Executable. You may also select Shared Library or Static Library, it does not matter, that information comes from your CMakeLists.txt, but CDT requires it. Do not select Makefile project here!
    • Finish project creation.
  • Open the "Project Properties" dialog.
    • Select the "C/C++ Build" node and the "Builder Settings" tab and make sure Generate Makefiles automatically is checked.
    • Select the "Tool Chain Editor" node and set "CMake Make Builder" as the current builder.
    • If your top level CMakeLists.txt does not reside in the root directory of your checked out files, select the "C/C++ General" "Path and Symbols" node and the "Source Location" tab. Then adjust the source folder to point to the directory containing your CMakeLists.txt file. This will tell the CDT indexer to scan the header files in your project.
  • Pro Tip: Add a CMakeLists.txt file in the root directory of your checked out files, if you miss the Binaries or Archives folder in the C/C++ Projects View. Build the project. This will invoke cmake to generate the build scripts, if necessary, and then will invoke make.

Do not try to import an Eclipse project that you created manually using cmake -G Eclipse CDT4 - Unix Makefiles, as that will put you on the classic Makefile project route!

The best thing about this plugin is that it can use the cmake exported compiler options to index your project.

  • Open the "Project Properties" dialog.
    • Select the "C/C++ General" node and the "Preprocessor Includes Paths, Macros etc." tab. Select "CMAKE_EXPORT_COMPILE_COMMANDS Parser" and move it to the top of the list.
    • Hit "OK" to close the dialog. Make sure to trigger one build now and recreate the index.

Provider tab

This fixed all the nasty indexer problems that were annoying me when I only used "CDT GCC Build-In Compiler Settings" and added stuff like "-std=c++14" to "Command to get the compiler specs".

6
votes

Using CMAKE in Eclipse Makefile project(on win):

1) create new "Makefile Project with Existing Code"

2) modify builder settings(Project Properties->C/C++ Build->Builder Settings):

  • Build command: cmd /c "mkdir ${PWD} & cd /D ${PWD} && ${CMAKE} -G "Unix Makefiles" ${ProjDirPath} && make"

  • Build directory: ${workspace_loc:/IoT_SDK}/build/${ConfigName}

That's all!

4
votes

In addition to accepted answer you should specify eclipse version.

Eclipse Luna (4.4):

cmake -G"Eclipse CDT4 - Unix Makefiles" -D_ECLIPSE_VERSION=4.4 ../../src/Planner

Eclipse Kepler (4.3):

cmake -G"Eclipse CDT4 - Unix Makefiles" -D_ECLIPSE_VERSION=4.3 ../../src/Planner
1
votes

Another completely different method.

  • Manually create an empty Eclipse project.

  • Link source directory via Project Properties -> C/C++ General -> Paths and Symbols -> Source Location.

  • In the Debug and Release build directories create 'Make Targets' (using the 'Make Targets View'). Leave 'Make Target' field empty and set the 'command' field to cmake -G "Unix Makefiles" <path/to/the/sources>.

  • When you double-click on the 'Make Target' that you've created, it should trigger cmake invocation inside the Debug/Release dirs.

  • In 'Project Properties -> C/C++ Build' disable built-in makefile generator.

Now normal build should work. It will also pick up any changes in the CMakeLists.txt files (to the extent that CMake can).

Maybe a more detailed description: https://stackoverflow.com/a/38140914/4742108

1
votes

Whatever method I have used the external includes has not been indexed.

This is because CMAKE used the "response files" which hides all includes inside those files, instead use it directly (-IPATH_TO_SOME_DIR)

To disable the "response file" I have used following commands:

SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES NO)
SET(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES NO)

To force the CMAKE to show gcc/g++ execution I have modified the make build target to "all VERBOSE=1". Note that this is not needed when you are using cmake4eclipse.

I have eclipse in version 2018-12 (4.10.0) with CDT in version 9.6.

0
votes

This is an old question, but deserves some updates.

First is that the person who made cmake4eclipse added the parsing functionality into CDT. The feature/bug was marked as "Resolved-Fixed" on 2020-08-05 (Aug 5, 2020), so it should go into the version of Eclipse due out next month. See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=559674

The other thing to point out is that you can manually trigger parsing on files:

Configuration Options / Index source and header files opened in editor

Configuration Options / Index source and header files opened in editor