eclipse-cdt / cdt

Eclipse CDT™ C/C++ Development Tools
http://eclipse.org/cdt
Eclipse Public License 2.0
305 stars 200 forks source link

Unable to load even simple CMake project into Eclipse CDT #576

Open bkeys opened 1 year ago

bkeys commented 1 year ago

Describe the bug

To Reproduce Steps to reproduce the behavior:

  1. Download https://github.com/jameskbride/cmake-hello-world as a simple example
  2. Extract it
  3. Go to File > New > C/C++ Project
  4. Select "Empty or Existing CMake Project"
  5. Fill out the fields as usual, make sure you are using the same directory as the cmake-hello-world example

Expected behavior To have the build option not greyed out, and to be able to resolve inclusions like #include <iostream>, on my installation it cannot resolve the line using namespace std;.

Screenshots image

Version Information (please complete the following information):

jonahgraham commented 1 year ago

To have the build option not greyed out

Try checking if Project -> Build Automatically is checked, and if so uncheck it to enable the build options.

and to be able to resolve inclusions like #include , on my installation it cannot resolve the line using namespace std;.

CMake projects need the build information to resolve, so a first build is needed to collect the include locations from the compile_commands.json

bkeys commented 1 year ago

It has no active build configurations (nor the ability to set one), and it's unchecked and is still grey. Is compile_commands.json generated by the makefile or by Eclipse CDT? EDIT: I just told it to build the entire project and it got me a compile_commands.json, but it still can't resolve inclusions.

bkeys commented 1 year ago

Here is the content of my compile_commands.json, I don't know how this will resolve the inclusions:


[
{
  "directory": "/home/bkeys/Devel/cmake-hello-world-master/build/cmake.debug.linux.ppc64le",
  "command": "/usr/bin/g++  -I/home/bkeys/Devel/cmake-hello-world-master/Hello  -o CMakeFiles/CMakeHelloWorld.dir/HelloWorld.cpp.o -c /home/bkeys/Devel/cmake-hello-world-master/HelloWorld.cpp",
  "file": "/home/bkeys/Devel/cmake-hello-world-master/HelloWorld.cpp",
  "output": "CMakeFiles/CMakeHelloWorld.dir/HelloWorld.cpp.o"
},
{
  "directory": "/home/bkeys/Devel/cmake-hello-world-master/build/cmake.debug.linux.ppc64le/Hello",
  "command": "/usr/bin/g++    -o CMakeFiles/Hello.dir/Speaker.cpp.o -c /home/bkeys/Devel/cmake-hello-world-master/Hello/Speaker.cpp",
  "file": "/home/bkeys/Devel/cmake-hello-world-master/Hello/Speaker.cpp",
  "output": "Hello/CMakeFiles/Hello.dir/Speaker.cpp.o"
}
]```
jonahgraham commented 1 year ago

I'm not a cmake expert, so not sure where this is failing - sounds like the index isn't refreshing properly once the compile commands are ready. I'll leave it to others in the community to answer those aspects. We have been discussing how to improve cmake integration in #419 FWIW

ghentschke commented 10 months ago

CDT parses the compile_commands.json and determines the compiler settings and include directories. @bkeys : Does it work, when you close the HelloWorld.cpp and reopen it again?

ghentschke commented 10 months ago

I just tested it with Eclipse 2023-09 on Windows 10. I've seen the same issue as @bkeys with the old C/C++ Editor. The include/std indexer problem is gone, when using the new LSP based C/C++ Editor, because clangd is able to determine the std library on the system.

The easiest way to import a Cmake project into Eclipse CDT is to the import function: File > Import > General > Projects from Folder or Archive Select the containing folder via the Directory... button. It should be imported as Cmake project.

The user has to add a .clangd configuration file manually in the projects root directory. This ensures that clangd can find the compile_commands.json file to dertermine the compiler and its settings. The .clangd file should contain at least the relative path to the compile_commands.json. E.g.:

CompileFlags:
  # When using clangd update this entry to point at the desired
  # configuration directory to pick up the compile_commands.json
  CompilationDatabase: build/default

I assume that the reason for the problems in the old editor is maybe caused by CDT cannot find the compile_commands.json.

ghentschke commented 10 months ago

I assume that the reason for the problems in the old editor is maybe caused by CDT cannot find the compile_commands.json.

That's not true. The compile_commands.json can be found and the system includes getting parsed. However I suggest to use the new C/C++ Editor.

razor5k commented 3 months ago

I have same Problem with Kubuntu 22.04 and eclipse 2023-06 and 2024-06. If you include a system header like iostream, the inclusion couldn't be resolved. The project is a simple hello world app with iostream included. There are 2 folders: app build CMakeLists.txt and main.cpp are in the app folder cd to the build folder It's created by cmake with following command: cmake -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ../app Import project with eclipse by pressing "File->Import->General->Existing Projects into Workspace". Choose the build directory and confirm. If you open the main.cpp in the Project explorer via double click to "[Source directory]->main.cpp", then the inclusion couldn't be resolved.

ghentschke commented 3 months ago

@razor5k which C/C++ editor are u using? The old or the new LSP based? Can the project being build successfully?

razor5k commented 3 months ago

It's the old one. The project compiles successfully.

The CMakeLists.txt

cmake_minimum_required(VERSION 3.24)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")

add_executable(${PROJECT_NAME} main.cpp)

The main.cpp

#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
    cout << "Hello world" << endl;
    return 0;
}

Additionally I checked C/C++ Editor (LSP) as well. The iostream couldn't be resolved either.

ghentschke commented 3 months ago

Hm, when I create an defualt HelloWorld project with the wizzard: File -> New -> C/C++ Project -> CMake project it works when the project has been build image image

Please ensure that the indexer is enabled: image

Does the template work on your machine?

razor5k commented 3 months ago

If I create a CMake project by eclipse, it works but if I import an eclipse project created by CMake, the indexer don't work with system includes. Indexers are enabled. The project is imported doing following steps: "File->Import->General->Existing Projects into Workspace" If the project is imported by pressing on ""File->Import->C/C++->Existing Code as Makefile Project" and Linux GCC is chosen, the indexer works properly.

ghentschke commented 3 months ago

Does your imported project has a CMake Nature? (Project Properties->Project Natures)

Which natures does it have at all?

razor5k commented 3 months ago

The Natures are (ordered) CDT-Make Nature Scanner Configuration Nature C++ Nature C Nature

ghentschke commented 3 months ago

Can you please try to add the CMake Nature to the imported project and check if this helps?

razor5k commented 3 months ago

Unfortunately it doesn't change anything. I don't even know if that's an issue with cmake rather than with eclipse cdt.

ghentschke commented 3 months ago

I think it's an CDT import problem. Maybe it makes sense to implement a CMake project import wizard.

razor5k commented 3 months ago

After adding the CMake Nature, following things are happened: I pressed <CTRL>+B to build and a build error is occured. After that the CMake Nature has been removed by eclipse The indexer works now, no unresolved includes are there After pressing <CTRL>+B again, the project builds now (but without CMake Nature)