Linux 将 CMake 项目导入 Eclipse CDT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11645575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing a CMake project into Eclipse CDT
提问by rajat
I have a native CMakeproject, and I want to use the EclipseIDE 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.
我有一个本机CMake项目,我想将EclipseIDE 与它一起使用,以便通过提供自动完成和其他功能使我的开发更容易。我似乎找不到如何在 Eclipse 中导入 CMake 项目。
PS: I am open for suggestions for other IDE's that work well with CMake.
PS:我愿意为其他与 CMake 配合良好的 IDE 提供建议。
采纳答案by arrowd
回答by user149408
Elaborating on arrowd's answer for Eclipse:
详细说明 arrowd 对 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.
首先,为 CMake 文件选择一个目录。我更喜欢将 Eclipse 工作区保留~/workspaces
在~/src
. 我需要构建或测试项目的数据位于项目工作区目录的子目录中,所以我建议对 CMake 做同样的事情。
Assuming both your workspace and source folders are named someproject
, do:
假设您的工作区和源文件夹都命名为someproject
,请执行以下操作:
cd ~/workspaces/someproject
mkdir cmake
cd cmake
cmake -G "Eclipse CDT4 - Unix Makefiles" ~/src/someproject
Then, in your Eclipse workspace, do:
然后,在您的 Eclipse 工作区中,执行以下操作:
File > Import... > General > Existing Projects into Workspace
文件 > 导入... > 常规 > 现有项目到工作区
Check Select root directoryand choose ~/workspaces/someproject/cmake
. Make sure Copy projects into workspaceis NOT checked.
选中Select root directory并选择~/workspaces/someproject/cmake
. 确保未选中将项目复制到工作区。
Click Finishand you have a CMake project in your workspace.
单击完成,您的工作区中有一个 CMake 项目。
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.
- 我用于
cmake
工作区子目录,但您可以使用您选择的名称。 - 如果您对构建配置进行了任何更改(例如编辑
Makefile.am
),您将需要重新运行最后一个命令,以便 Eclipse 获取更改。
回答by user149408
I just learned that CMake's CDT project generator appears to be unmaintainedand has caused various problems—especially, it seems, with later versions of Eclipse (of which I had my share as well).
我刚刚了解到 CMake 的 CDT 项目生成器似乎没有维护,并导致了各种问题——尤其是在 Eclipse 的更高版本中(我也参与其中)。
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.
建议使用 cmake4eclipse(可从 Eclipse Marketplace 获得),它CMakeLists.txt
用作其唯一的真实来源。它还允许您将源树与工作区分开。
- 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 Projectas the project type, and accept the default toolchain.
- Click Finish, and you have a CMake project in your workspace.
- 在 Eclipse 中,选择File > New > C Project。
- 输入项目名称并选择源树的根作为项目位置。
- 选择Executable > Empty Project作为项目类型,并接受默认工具链。
- 单击Finish,您的工作区中有一个 CMake 项目。
回答by emmmphd
One simple solution on my desktop:
我桌面上的一个简单解决方案:
- In eclipse:
New -> New C/C++ Project -> Empty or Existing CMake Project
, choose a project name (e.g. project). - Copy all you files of the old directory to the new one (path to your project).
- 在 eclipse: 中
New -> New C/C++ Project -> Empty or Existing CMake Project
,选择一个项目名称(例如项目)。 - 将旧目录的所有文件复制到新目录(项目路径)。
回答by kdm
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).
简而言之,目前只有 CDT 发生器是一个可行的选择。为什么?仅仅是因为 Eclipse 必须从某处获取预处理器定义、包含路径以及构建中包含哪些文件。否则我们最终会索引所有内容,而且很糟糕,没有定义正确的宏。如果您的存储库或中型或大型树有提升,那么在现实世界中对所有内容进行索引根本行不通 - 无论如何,Eclipse 索引器的速度和可靠性都不行。由于现在大多数人可能想使用 Ninja 进行构建(谁想要等待 30 秒以上的热构建只是为了看看什么是脏的?),这排除了通过当前支持的 makefile 导入此信息的唯一方法,除非你想每次配置都生成两个独立的构建系统,
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.
在 Eclipse Photon 中有一个新选项可以直接导入 cmake 项目。然而,在这个阶段,我想说它对于除微不足道的项目之外的任何东西看起来几乎没有用,因为似乎没有任何方法可以导入已经配置的构建目录,或者在任何地方设置传递给的变量和参数制作。我还不知道这个功能实际上是如何工作的,但似乎 Eclipse 必须通过 CMakeLists 的层次结构进行解析,按照逻辑来查看进行了哪些 add_subdirectory() 调用,以及设置了哪些预处理器定义,这似乎是一种没有前途的方法,因为我们有 cmake 服务器模式,毫无疑问,它需要在 Eclipse 中重新实现几乎所有的 cmake 语言解析器来完成这项工作。
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.
到目前为止,似乎满足现实世界要求的唯一可行解决方案似乎是使用 cmake CDT 生成器。它可以直接从cmake内部获取上述信息,并将其写入.cproject文件。如前所述,它并没有真正维护,并且依赖于 .cproject 的过时模板,这会导致一些问题。
My own minimal requirements for cmake support are:
我自己对 cmake 支持的最低要求是:
- 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).
- 能够导入已配置的项目。大型构建系统通常使用脚本在命令行上将大量变量传递给 cmake。
- 只有构建系统的一部分应该被索引。
- 它必须与源代码树构建一起使用。
- 必须使用预处理器定义并包含来自构建系统的路径才能进行工作索引。
- 必须使用 Ninja 生成器(即不生成 makefile)。
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)
目前,除了 CDT 生成器之外,我看不到如何使用任何其他方法执行上述任何操作,而实现有效和完善的解决方案的唯一方法需要对 cmake 的生成器进行一些黑客攻击,或者对生成的 .cproject 进行后期处理选择索引的内容(即并非所有 CMAKE_SOURCE_DIRECTORY)。这是另一个 hack(因为 cmake 没有任何方法将配置后的任务附加到配置步骤中,我们必须创建一个进程,并监视父 cmake 是否终止。这是代表 cmake 的一个深思熟虑的决定开发人员显然,也许是正确的)
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.
我当然非常感谢任何更好方法的信息。Eclipse 中的 cmake 支持状态对于一个应该用于 C++ 开发的工具来说非常糟糕。索引器(至少当它没有被需要重新启动 Eclipse 的常规锁定所困扰时)实际上是最好的,而且 Eclipse 是一个非常好的代码跳转环境。
回答by Nadav B
Do the follows:
执行以下操作:
mkdir debug
(or release, or any other name)
mkdir debug
(或发布,或任何其他名称)
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
确保您设置了正确的 Eclipse 版本
Then open the folder from Eclipse (Open Projects)
然后从 Eclipse (Open Projects) 打开文件夹