Importing a CMake project into Eclipse CDT

前端 未结 6 1326
Happy的楠姐
Happy的楠姐 2021-01-30 16:49

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

相关标签:
6条回答
  • 2021-01-30 17:27

    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.

    0 讨论(0)
  • 2021-01-30 17:33

    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.

    0 讨论(0)
  • 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 讨论(0)
  • 2021-01-30 17:40

    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.
    0 讨论(0)
  • 2021-01-30 17:40

    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)

    0 讨论(0)
  • 2021-01-30 17:50

    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).
    0 讨论(0)
提交回复
热议问题