Martin G
Martin G

Reputation: 18109

Eclipse project linked resources by environment variable

I'm having trouble setting up my Eclipse C++ project. I need to link in source from different directories here and there and in my environment the source I need actually moves sometimes (for example when i always want the latest version of a sub system currently located in a directory like /aaa/bbb/v1.2.3/src). I always know the location of the source I want through linux environment variables, like $SYSTEM1_LATEST_ROOT. Without this I need to update all my projects whenever i should pick the source from a new location.

I cannot find a way to include the environment variables in the paths for linked resources. Include directories work perfectly (these are defined in my .cproject file), for example (.cproject):

<option id="..." name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" valueType="includePath">
    ...
    <listOptionValue builtIn="false" value="${SYSTEM1_LATEST_ROOT}/src"/>
    ...
</option>

For linked resources (defined in the .project file) I know I can use path variables but these are defined inside Eclipse only and I find no way to have them based on environment variables, but only to be relative to my own project location, which is not what I want to do.

In short, I want to link in source code from locations based on environment variables. The variable name is constant, but the actual location (known through the environment variable) of the source is not.

Working example with path relative to project. This is not what i want (.project):

<linkedResources>
    <link>
        <name>System1_src</name>
        <type>2</type>
        <locationURI>PARENT-6-PROJECT_LOC/src</locationURI>
    </link>
</linkedResources>

Non-working example of what i want to do (.project):

<linkedResources>
    <link>
        <name>System1_src</name>
        <type>2</type>
        <locationURI>${SYSTEM1_LATEST_ROOT}/src</locationURI>
    </link>
</linkedResources>

Any suggestions?

Upvotes: 9

Views: 7790

Answers (4)

Paul D Smith
Paul D Smith

Reputation: 749

Very late to this thread but here is what works for me:

  • Define a workspace scoped Path Variable e.g. MYPATH_WS and set this
  • Define a project scoped Path Variable e.q. MYPATH and reference the workspace variable
  • Import your linked resources relative to the project scoped variable.

Now when someone else takes your code, they can set their own workspace scoped variable MYPATH_WS and voila!

I also needed this to work in a GitHub CI and for this I had the build system patch the .project file at build time to replace the referenced <value>$%7BMYPATH_WS%7D</value> with a proper path to wherever the linked resources are in the CI environment e.g. to <value>file:/d:/somewhere/else/resources</value> (it's eclipse so a Windows build in my case!).

Upvotes: 0

Pich
Pich

Reputation: 65

Maybe I got it to work. I add a new folder to the project and I click on Advanced. This is the screen

enter image description here

I add the folder name (virtual name, to be linked to the real folder) and I use this environment variable:

${WORKSPACE_LOC}\my_path_to_the_external_src

Eclipse can resolve the trick. Anyway another step is needed.

Click on the newly added folder -> properties and you have to uncheck the exclude from build:

enter image description here

At this point, the trick is working for me.

It is mandatory to have the environment variable all capital letters!

Hope this helps.

Upvotes: 0

Martin G
Martin G

Reputation: 18109

After waiting for a month i figure it's time I answer with my own findings..

First, the concept of path variables (http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.platform.doc.user/concepts/cpathvars.htm);

"Linked resource target paths can be either defined 
 as absolute paths, or relative to a path variable."

Two options; absolute paths or relative to path variable.

Specifically it says about path variables:

"Each project contain a pre-defined set of path 
 variables available for defining linked resources, 
 including ECLIPSE_HOME, PARENT_LOC, PROJECT_LOC and 
 WORKSPACE_LOC.

New path variables can be defined relative to 
existing path variables by using the ${VAR} syntax. 
For example, a path variable FOO can be defined 
relative to BAR by defining it to "${BAR}../foo"."

In other words, path variables are always relative to the project location in some way, using a liberal interpretation of the word project.

So the next option would be absolute path. An absolute path containing an environment variable would work!

"The linked resource target path can be changed by 
 selecting the Edit... button in the File > Properties > 
 Resource property page of the linked resource. "

Trying this it gets painfully obvious that only path variables are supported as part of the path to the linked resource. And we already know the definition of a path variable.

The answer is...

Eclipse does not contain this very basic feature.

Anyone, please prove me wrong!

Upvotes: 9

Dave
Dave

Reputation: 15016

I thought I'd add my solution to the problem here so others might be able to benefit from it.

While not supported "by default", it turns out that Eclipse can use path variables. The key here is to use its "Linked Resources" feature. Go to Window -> Preferences -> General -> Workspace -> Linked Resources and add your path variables there.

enter image description here

Upvotes: 4

Related Questions