Walidix
Walidix

Reputation: 1277

With Eclipse: How to add include paths and libraries for all your C/C++ project

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?

Upvotes: 26

Views: 66674

Answers (8)

jif54
jif54

Reputation: 21

Semi-automagically, it can be done using this perl script:

#!/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";
}

Upvotes: 0

Dominik
Dominik

Reputation: 2541

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":

  1. 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.
  2. 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.

Referencing the libraries works the same way.

Also have a look at:

Upvotes: 11

yehudahs
yehudahs

Reputation: 2668

I faced the same problem and solved it like this : I saw that in the auto-generated makefile there are lines:

-include ../makefile.init
 ...
-include ../makefile.defs
 ...
-include ../makefile.targets

after reading How can I parameterize an Eclipse generated make file I 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:

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:

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...

Upvotes: 1

krenon
krenon

Reputation: 59

For libraries I do the following in the source project:

Project -> properties -> c/c++Build -> c++ linker -> libraries

After this I select libraries (shift + mouse) & copy (ctrl + c)

In the blank project:

Project -> properties -> c/c++Build -> c++ linker -> libraries

ctrl + v (paste)

Works fine with eclipse juno

Upvotes: 5

Nenad Bulatović
Nenad Bulatović

Reputation: 7434

I just copy one project from using eclipse built in Project explorer. Afterwards, just rename source files.

Upvotes: 3

user1914692
user1914692

Reputation: 3073

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.

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_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:

-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?

Upvotes: 1

DiscoStu
DiscoStu

Reputation: 569

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).

You can also use PATH to include static libs. However, for me it's only worked reliably in linux. In OSX it ruins 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.

Upvotes: 7

parvus
parvus

Reputation: 6006

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.

Go to Project > Properties > C/C++ General > Paths and Symbols Then 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...

Upvotes: 16

Related Questions