Eclipse CDT C/C++:包含来自另一个项目的头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9272882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:43:31  来源:igfitidea点击:

Eclipse CDT C/C++: Include a header file from another project

c++ceclipseinclude

提问by Tobber

I have two c++ projects in Eclipse CDT mainand shared. In sharedi have a header called calc.h. I want to use this header in main, so i did the following:

我在 Eclipse CDT mainshared 中有两个 c++ 项目。在共享中,我有一个名为calc.h的标头。我想在main 中使用这个标题,所以我做了以下事情:

  • added #include "calc.hto the relevant files in main
  • In main's properties -> Project referencesi checked of shared
  • 添加#include "calc.hmain中的相关文件
  • main 中properties -> Project references我检查了共享

I hoped this would work, but I get a fatal error: calc.h: No such file or directorywhen compiling, so the project reference somehow doesn't work.

我希望这会奏效,但我fatal error: calc.h: No such file or directory在编译时得到了一个,所以项目参考不知何故不起作用。

I can get it to work by manually adding shared's source folder in main's properties->C/C++ Build->Setting->GCC C++Compiler->Includes, but my i have a bad feeling that this will become cumbersome on larger projects more complex dependencies. I'll therefore hoped that Eclipse could handle this via project references.

我可以通过手动得到它的工作将共享“中的源文件夹中的主要properties->C/C++ Build->Setting->GCC C++Compiler->Includes,但我的我有一种不好的预感,这将成为较大的项目更加复杂的依赖关系繁琐。因此,我希望 Eclipse 可以通过项目引用来处理这个问题。

Am I missing something or is manually the only way?

我错过了什么还是手动是唯一的方法?

采纳答案by Adrien BARRAL

You are right, this his the way to do it !

你是对的,这是他的方式!

I use Eclipse CDT on large project, but I don't use the eclipse compilers settings. There are some drawbakcs to use the CDT compilers settings :

我在大型项目中使用 Eclipse CDT,但我不使用 Eclipse 编译器设置。有一些缺点可以使用 CDT 编译器设置:

  • As you said, on large project, this is cumbersome.
  • If you want to compile you project on a platform which doesn't have eclipse (when you deploy you application), that is not straightforward.
  • 正如您所说,在大型项目中,这很麻烦。
  • 如果您想在没有 Eclipse 的平台上编译您的项目(当您部署应用程序时),这并不简单。

I use CMake to manage my eclipse projects. When I start a new project, I do the following steps :

我使用 CMake 来管理我的 Eclipse 项目。当我开始一个新项目时,我执行以下步骤:

  • In a terminal : Create a folder for your new project
  • With your favorite text editor (vim, emacs, Text edit, kate ...) create the CMakeLists.txt of your project. You don't have to create an exaustive CMakeLists, just a small CMakeLists four your first files
  • Then ask cmake to generate the eclipse project thanks to : cmake -G "Eclipse CDT4 - Unix Makefiles"
  • Open eclipse, and click on File->Import, and choose "General/Existing project into workspace", then you can choose the folder created in the first step, and your project is ready to use in eclipse.
  • 在终端中:为您的新项目创建一个文件夹
  • 使用您最喜欢的文本编辑器(vim、emacs、Text edit、kate ...)创建项目的 CMakeLists.txt。你不必创建一个详尽的 CMakeLists,只需一个小的 CMakeLists 四个你的第一个文件
  • 然后让 cmake 生成 eclipse 项目,感谢: cmake -G "Eclipse CDT4 - Unix Makefiles"
  • 打开eclipse,点击File->Import,选择“General/Existing project into workspace”,然后就可以选择第一步创建的文件夹,你的项目就可以在eclipse中使用了。

CMake is THE compiler configuration tool to manage projects... If you don't know it I encourage you to discover it.

CMake 是用于管理项目的编译器配置工具......如果你不知道它,我鼓励你去发现它。

Cheers !

干杯!