使用 Eclipse:如何为所有 C/C++ 项目添加包含路径和库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2705947/
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
With Eclipse: How to add include paths and libraries for all your C/C++ project
提问by Walidix
Is it possible to add include paths and libraries to all C/C++ projects? In others words: How can I make them global or copy one C/C++ project build setting to another one?
是否可以向所有 C/C++ 项目添加包含路径和库?换句话说:如何将它们设为全局或将一个 C/C++ 项目构建设置复制到另一个?
回答by parvus
For Eclipse Indigo: There is no possibility to define globally include paths and libraries. But you can export and import them from one project to another.
对于 Eclipse Indigo:无法定义全局包含路径和库。但是您可以将它们从一个项目导出和导入到另一个项目。
Go to Project > Properties > C/C++ General > Paths and SymbolsThen click Export Settings...to save the include paths and/or symbol definitions to a file. In your other project, you can then use Import Settings...
转到Project > Properties > C/C++ General > Paths and Symbols然后单击Export Settings...将包含路径和/或符号定义保存到文件中。在您的其他项目中,您可以使用导入设置...
回答by Dominik
Yes, you can, in 2 steps, more user-friendly as the one mentioned by parvus. Both are done in "Project Properties" -> "C/C++ General" -> "Paths and Symbols":
是的,您可以通过 2 个步骤,像 parvus 提到的那样更加人性化。两者都在“项目属性”->“C/C++ 常规”->“路径和符号”中完成:
- In an existing project where you've already configured the include paths: Highlight the include paths (in the tab "Includes") which you want to copy to the new project and choose "Export". Note that this adds an
[Exp]
tag to each line. - In the new created project: In the "References" tab, tick the checkbox of the just mentioned project and when you switch back to the "Includes" tab, you see that all the paths you've just exported appear there.
- 在已配置包含路径的现有项目中:突出显示要复制到新项目的包含路径(在“包含”选项卡中)并选择“导出”。请注意,这会
[Exp]
为每一行添加一个标签。 - 在新创建的项目中:在“引用”选项卡中,勾选刚才提到的项目的复选框,当您切换回“包含”选项卡时,您会看到刚刚导出的所有路径都出现在那里。
Referencing the libraries works the same way.
引用库的工作方式相同。
Also have a look at:
也看看:
- the Eclipse Juno Documentationto this topic,
- a related StackOverflow question
回答by DiscoStu
I often use the CPATH environment variable to include different directories across all my projects. It's found under: c/c++->build->environment. Be sure to separate each path with a ':' character (not a ';' semicolon).
我经常使用 CPATH 环境变量在我的所有项目中包含不同的目录。它位于:c/c++->build->environment。请务必使用“:”字符(而不是“;”分号)分隔每个路径。
You can also use PATH to include static libs. However, for me it's only worked reliably in linux. In OSX it ruins make.
您还可以使用 PATH 来包含静态库。但是,对我来说,它只能在 linux 中可靠地工作。在 OSX 中,它会破坏 make。
The alternative would be to add these libraries and headers to your environments default search paths; place likes /usr/local/lib. Read up on where your linker searches by default.
另一种方法是将这些库和头文件添加到您的环境默认搜索路径中;地方喜欢/usr/local/lib。阅读链接器默认搜索的位置。
回答by krenon
For libraries I do the following in the source project:
对于库,我在源项目中执行以下操作:
Project -> properties -> c/c++Build -> c++ linker -> libraries
项目 -> 属性 -> c/c++Build -> c++ 链接器 -> 库
After this I select libraries (shift + mouse) & copy (ctrl + c)
在此之后我选择库(shift + 鼠标)& 复制(ctrl + c)
In the blank project:
在空白项目中:
Project -> properties -> c/c++Build -> c++ linker -> libraries
项目 -> 属性 -> c/c++Build -> c++ 链接器 -> 库
ctrl + v (paste)
ctrl + v(粘贴)
Works fine with eclipse juno
与 eclipse juno 一起工作正常
回答by Nenad Bulatovic
I just copy one project from using eclipse built in Project explorer. Afterwards, just rename source files.
我只是从使用项目资源管理器中内置的 eclipse 复制一个项目。之后,只需重命名源文件。
回答by user1914692
I am looking into it long ago. A few posts mentioned one method: Project > Properties > C/C++ General > Paths and Symbols Then click Export Settings.. But it only works for including header files.
我很久以前就在研究它。一些帖子提到了一种方法:项目>属性>C/C++常规>路径和符号然后单击导出设置..但它仅适用于包含头文件。
To specify libraries and library paths, as far as I know, I do not find an easy solution. For example, I am now using ffmpeg libraries. If you use "Makefile", the including and library files can be expressed below:
要指定库和库路径,据我所知,我没有找到简单的解决方案。例如,我现在使用 ffmpeg 库。如果使用“Makefile”,包含和库文件可以表示如下:
FFMPEG_LIBS= libavdevice
libavformat
libavfilter
libavcodec
libswresample
libswscale
libavutil
CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS) LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)
FFMPEG_LIBS= libavdevice
libavformat
libavfilter
libavcodec
libswresample
libswscale
libavutil
CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS) LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS) )
There are many files here. If I include them one by one in an Eclipse C++ project, it is daunting. I do not know why there is no option in Eclipse that we could include library files by using past a long line using ; as a separator, such as:
这里有很多文件。如果我将它们一一包含在 Eclipse C++ 项目中,那是令人生畏的。我不知道为什么 Eclipse 中没有选项可以通过使用过去的长行来包含库文件;作为分隔符,例如:
-lavdevice -lavfilter -lswresample -lswscale -lavformat -lavcodec -ldl -lasound -lSDL -lz -lrt -lavutil -lm
-lavdevice -lavfilter -lswresample -lswscale -lavformat -lavcodec -ldl -lasound -lSDL -lz -lrt -lavutil -lm
Is there any such way, or I have to type the library files one by one, or have to use Makefile?
有没有这样的办法,还是我得把库文件一个一个打出来,还是得用Makefile?
回答by yehudahs
I faced the same problem and solved it like this : I saw that in the auto-generated makefile there are lines:
我遇到了同样的问题并像这样解决了它:我看到在自动生成的 makefile 中有几行:
-include ../makefile.init
...
-include ../makefile.defs
...
-include ../makefile.targets
after reading How can I parameterize an Eclipse generated make fileI realized that you can define variables outside of you project. That mean it can be shared variable to several projects. So I added makefile.defs file outside of the project and declare in it some variables:
阅读如何参数化 Eclipse 生成的 make 文件后,我意识到您可以在项目之外定义变量。这意味着它可以被多个项目共享变量。所以我在项目之外添加了 makefile.defs 文件并在其中声明了一些变量:
MY_INCLUDE = <path to include>...
MY_LIBS_PATH = <path to libs>...
MY_LIBS = -lmylib ... (the libs)
than in all my projects "properties->c/c++ build->build variables" I defined new variable:
比在我所有的项目“properties->c/c++ build->build variables”中我定义了新变量:
my_include = $(MY_INCLUDE)
my_libs_path = $(MYLIBS_PATH)
my_libs = $(MY_LIBS)
And than you can you those variables in the projects. Go to properties->c/c++ build->Settings and choose compiler includes and add $(my_include). Go to linker and choose Libraries and add to search path $(my_libs_path). For some reason the Libraries (-l) can't take my defined env variable so I bypass it by choosing linker and than go to Miscellaneous and add to linker flags $(my_libs). Finally you have to change the order in the command line (when the linker execute) cause the list of libs need to come after the .o files. So to do that you go to linker and tab and there you can see the final command line that is going to be executed. In "expert settings command line pattern" you move the ${FLAGS} to the back of the command line (that moves the $(my_libs) string to be in the back of the command line). THAT ALL. its a lot but you need to do it only once... hope that it helps someone...
而且你可以在项目中使用这些变量。转到 properties->c/c++ build->Settings 并选择编译器包含并添加 $(my_include)。转到链接器并选择库并添加到搜索路径 $(my_libs_path)。由于某种原因,库 (-l) 不能接受我定义的 env 变量,所以我通过选择链接器绕过它,然后转到杂项并添加到链接器标志 $(my_libs)。最后,您必须更改命令行中的顺序(当链接器执行时),因为库列表需要在 .o 文件之后。为此,您转到链接器和选项卡,在那里您可以看到将要执行的最终命令行。在“专家设置命令行模式”中,您将 ${FLAGS} 移动到命令行的后面(将 $(my_libs) 字符串移动到命令行的后面)。这一切。
回答by jif54
Semi-automagically, it can be done using this perl script:
半自动地,它可以使用这个 perl 脚本来完成:
#!/usr/bin/perl
# pipe the (gcc) compile command into this script
# It emits listOptionValue lines to paste into .cproject
#
# To easily find the paste location, set the include path as usual to a string
# that is easily recognizable in the .cproject, e.g. to "MARKER_INCLUDE_PATH".
#
# Make sure eclipse is not running while you do these sorts of hacks and backup
# your .cproject before.
# -----------------------------------------------------------------------------------------
# sample line: <listOptionValue builtIn="false" value="/usr/include/gdk-pixbuf-2.0"/>
while (<>) {
@include_options = m#-I *(.*?) #g;
print join ("\n", @include_options) . "\n";
print join ('"/>' ."\n" . ' <listOptionValue builtIn="false" value="', @include_options);
print '"/>' . "\n";
}