Wes Miller
Wes Miller

Reputation: 2241

Eclipse C++ multiple projects common files

In Eclipse/CDT, I wish to have several C++ projects (projA, projB, projC, etc.) that will all include some .c, .cpp and/or .h files from a common directory. Here is my existing file structure:

Workspace -> projA -> src
          -> common ->src : a.c a.h  b.cpp  c.cpp  d.c  etc.
          -> projB -> src
          -< projC -> src

When I attempt to add a source file in ../common/src to projA, the Add a Source File tool complains that the file must be in the project.

So, how do I include, say ../common/src/a.c into ../projA -> src?

Upvotes: 3

Views: 2070

Answers (2)

mohaps
mohaps

Reputation: 1020

Eclipse has an option called add new source folder and an option in that dialogue to link to existing directory. You can link the "common" folder as a source folder in all of the projects pointing to the same directory.

Right click on project -> New -> Folder -> Advanced -> Link to alternate location

Once folder has been added,

Right click on Folder -> Buld Path -> Use as source folder

Upvotes: 2

user1201210
user1201210

Reputation: 3809

An easy way to do this is to drag the files or root folder from "common" to "projA" while holding Ctrl and releasing it on the target "src" folder. Eclipse will prompt you with the option to copy the files or link them from the original folder. After linking, they will be built as part of the target project (e.g., "projA").

Here's the prompt I get when releasing a folder:

file and folder operation prompt

I think the second option accomplishes what you want (I haven't messed with the third option).

Upvotes: 0

Related Questions