Eclipse projects have the nice features that they can link to files and folders: so instead of having the physical file, it is just a pointer to a file. This is very cool as that way I can point to shared files, or keep files in a common place referenced from projects, and so on.
As with most things in Eclipse, there is not a single way how to do things. So I’m showing in this post several ways how to link to files and folders.
Creating a New Link
Select the folder/project where to create a link to a file. Use the context menu with New > File (or the menu File > New > Other > General > File):
That opens a dialog to create a new file. I can select which project/folder I want to use for this:
Next, click on ‘Advanced’, enable ‘Link to file in the file system’ and Browse to the file you want to link to:
This will set the link to the file:
As an absolute path (C:\….) is probably not really what you want, there are Eclipse Path Variables you can use:
💡 Note that these are Eclipse PATH variables (not Eclipse BUILD variables), see “Eclipse Build Variables“.
To use the Path Variable for the link, use the ‘Extend…’ Button and then select the file, then press OK:
This now uses a PATH variable for that link. Note that it shows as well to which file it resolves:
Press Finish, and the link will be created. Note the small ‘arrow’ in the icon (see “Icon and Label Decorators in Eclipse“) to show a linked file:
Modifying a Link
If you right-click on that linked file and select ‘Properties’ of it, you can see that it is really a Linked File, with the link information, and you can change/Edit that link any time:
As for link to files, its possible to create ‘link to folders’. It works the same way: select the folder, then use File > New > Folder:
- Use default location: this creates a normal folder.
- Virtual Folder: this does not create a physical folder, but a virtual ‘container’ where I can place links or other virtual folders. This is useful to organize links and virtual folders, without the need for a physical folder.
- Linked Folder: like linked files, this links to a folder.
💡 The cool thing about linked (source) folders is: when I add new files to that folder where it links to, the projects with that linked folder to it will ‘see’ the extra files too, and that way new files are automatically added to the project. I do this many times, and it is like a ‘library’ folder for me: I can add a new source file to that ‘library’ folder, and every project linking to that folder will automatically have it added. 🙂
Linked files and linked folders can be deleted from the project too. In that case, the destination of the link is *not* deleted, only the link:
Using Drag & Drop
As mentioned at the beginning: there are multiple ways to do the same thing in Eclipse. Instead using the top menu, or using the context menu, I can use drag & drop. To create links, I need to hold the CTRL key:
💡 Notice that during the drag&drop with CTRL key pressed the icon gets a ‘+’ to show copy/linking mode.
When I drop the file: I get the usual choices how I want to create the link:
Drag&Drop of files and folders do not work inside Eclipse. What works as well under Windows is to drag&drop a file or folder from the Windows Explorer :-).
Links to files and folders are a cool thing in Eclipse, and they can be created with menus or even simpler with drag&drop. This is not limited to inside Eclipse: I can drag&drop from outside with the Windows Explorer and that way can link to files and folders everywhere 🙂
Happy Linking 🙂
One of my projects is quite complex, on-going for years now, and ported to different micros and code on the PC. It seems whenever I share files like this (or by just adding a path like /stuff/common/C/xyz to the projects, over time things wind up out of sync and no longer build or work everywhere.
I come back two years later and try to build for one of the targets and find things have diverged so much it takes ages to update and get things configured with conditional compile, etc. to work again.
I tried using a continuous build server with CruiseControl but ran into problems getting command line build working in (the old classic) Codewarrior. Finally just gave up.
I have used Eclipse *path* variables for the links to make it portable as much as possible. The challenge is if absolute links are used, they easily get out of sync if projects get moved.
That has worked fine for me. However, if the project is really huge and complex, I think the only good way to manage it is with using build scripts and make files: it is not easy to setup in the first place, but easier to manage then .project and .cproject files in Eclipse.
Pingback: How to Add Existing Files to Eclipse Projects | MCU on Eclipse
Hi Erich. I added a linked folder to a project and now i’d like to use the #include statement to reference those files. I don’t know if it’s the best way to do that, because only works if I put the entire path. There’s another way to do? (or a correct way?). Thank you, regards.
the correct way is to add that folder to the compiler include path settings, see
Hi Wrich, I did put my folder location to Compiler Include, Linker and event in the C++ project Path… but still have the message C5200 telling Header File Not Found… the only way I can have it found is if I put the same path set into above places in the #include directive in my code.. which is not what I want ! Can you help ?
that might depend if you include the file with or with “…”?
Let say the file is there :
my Workspace here :
And my project is here :
If I write “C:\Users\EVH\Documents\Projet\M2S\SOLUTIONS\HVAC\COMM15\Sources\” inside the Linker/compiler input setting AND the same path like this when the file is needed
it works. But every other guess did not ! I tried
#include “RS485.h” and
do i need to set #include “../../M2S/SOLUTIONS/HVAC/COMM15/Sources/RS485.h”
oups.. cannot edit a post… #include is the part missing from previous post
oups.. cannot edit a post… #include RS485.h is the part missing from previous post seems like the symbols beetween the texte RS485.h is not printed when posting !!!
I recommend that you have a look at the compiler command line options when you compile that file. Could it be that you have set a special ‘per file’ option and that your overall project options are not used for every file (see https://mcuoneclipse.com/2013/06/12/traps-and-pitfalls-no-hexbins19-file-created-with-gnu/). You need to have the path to your include file present in the -I C compiler option, plus in the -I C++ compiler option in case you are using C++.
Thank’s it worked.. Had to set to default my linked file.
I am trying to debug my code which is written for Cortex M4. I am using GNU ARM compiler and JLink GDB debugger.
I am adding source files to my project using drag and drop. If I opt for “copy files” option during import, debugging is fine and current execution (Program counter) is getting mapped to code in source files properly.
But if I opt for “Link to files” option during import, during debugging current execution (PC) is not getting mapped to source files i.e., I am seeing “No source available for 0x0” as depicted here https://imgur.com/a/ww64L
Can you help me to overcome this.
I think this is not related to linked files, but due a small glitch in the GDB. Can you do an assembly step (see https://mcuoneclipse.com/2012/10/27/assembly-instruction-stepping/)? I think then the source file will show up.
It shows “no source file for 0x0” because at the time the source view was looking for the source, the CPU had a program counter of zero, and there is no source at address zero (there is the reset vector at this address).
Thanks for quick response.
I tried single step assembly. It works fine. But corresponding source files do not show up in source view window.
Please check http://imgur.com/a/zOsTb
Let me know if you need more information.
Have you make sure you have re-compiled all your project files (Project > Clean, see https://mcuoneclipse.com/2012/03/12/no-rule-to-make-target/)?
And I think you might have disabled debugging information. Check your project settings (C/C++ Build > Settings > Tool Settings > Debugging > Debug level) that it is using -g3 (maximum)
I am actually making use of a sample project from http://gnuarmeclipse.github.io/tutorials/hello-arm/
Then I remove hello arm source files from project, then add my own source files and an .elf file generated externally ( i.e., not build by eclipse project environment)
Then I do necessary changes in Debug configurations like ‘Disable auto build’, specifying .elf path in ‘Load symbols’, ‘Load executable’ etc
Things work fine i.e., source files get mapped while debugging only if I add it by ‘copy files’ option. Keeping every thing same but just adding source files by ‘Link to files’ option, source files don’t get mapped. The reason I want ‘Link to files’ options is to avoid unnecessary copying of files in the eclipse project folder.
So I think, the steps you suggested do not make any difference.
Anyway I have tried to rebuild it, it did not help.
(Note: I am using debug settings as in http://gnuarmeclipse.github.io/debug/jlink/)
Actually I am making use of sample from from gnuarmeclipse.github.io/tutorials/hello-arm/
I build above project once then I remove hello arm related source files and add my own source files. I also add an .elf file which is generated from command line using GCC ARM compiler.
I am following Debug configuration as in http://gnuarmeclipse.github.io/debug/jlink/. Then I do necessary changes in Debug configuration like “Disable auto build”, specifying my own .elf file path in “Load symbols”, “Load executable” etc.
Things work fine (i.e., source files get mapped during debugging) if I add source file by “copy files” option. But it does not work if I add source files by “Link to files” option.
So I think suggestions made by you for rebuilding and debugging level setting do not make any difference.
Anyways I tried to rebuild the project. It did not help.
Here is what happens: the target stops, the debugger gets the current program counter. Based on the program counter, it knows which object file it is, and uses that information to get the source file. The source file (and path) is present in the .elf file (debug information). You can see the paths to the files if you use the ‘Executables’ view in Eclipse: can you check the information for your .elf file with this view if it makes sense where the source files are?
I was creating eclipse project in C drive (default work space location) and my source files were in D drive. Today I created eclipse project in D drive itself, just at the folder level where my source files project was present. Then I added all the source files through “Link to files” option. It worked!!!
Thanks for your continuous support.
Pingback: Tips for Making Copy of Eclipse CDT Projects Easier | MCU on Eclipse
Pingback: SDK Project Generator for Kinetis and KSDK – Baremetal and RTOS
Pingback: Transforming Eclipse Linked Projects into Standalone Projects | MCU on Eclipse
many thanks you save my days.
Thanks, good to know this article was usfule for you 🙂
Pingback: assert(), __FILE__, Path and other cool GNU gcc Tricks to be aware of | MCU on Eclipse
I work in Atollic True Studio. When i get referece for files – it’s all good. But if i get referece for folder, compiler don’t do “arm-atollic-eabi-gcc -c .. ” and “arm-atollic-eabi-gcc -o ..” for files in this folder. I added files of this folder in Properties – C/C++ Build — Settings – Tool Settings — Directories, but this didn’t help.
Can you check that the folder is not ‘excluded from build’, see https://mcuoneclipse.com/2014/07/22/exclude-source-files-from-build-in-eclipse/
I hope this helps,
I tried it, but it didn’t help. I wont to include. There is excluding..
sounds like a bug in Atollic? But this is such a basic feature in Eclipse I doubt they can have broken it?
It isn’t Atollic’s bug probably. I got errors like “undefined reference to `NameFunction'”
LikeLiked by 1 person
There could be many other reasons too. Are you sure your file with that ‘NameFunction’ object gets linked? Check the command line passed to the linker if it is really there.
Then make sure your ‘NameFunction’ is not static: you only can link with external linkage functions.
I hope this helps,
“Are you sure your file with that ‘NameFunction’ object gets linked?”
I think linker don’t tnow about file with ‘NameFunction’. That’s the problem.
I wrote above “compiler don’t do “arm-atollic-eabi-gcc -c .. ” and “arm-atollic-eabi-gcc -o ..” for files in this folder”..
“Then make sure your ‘NameFunction’ is not static: you only can link with external linkage functions.”
I try to interconnect old sources. They are working and they aren’t static.
LikeLiked by 1 person
I think it should call arm-none-eabi-gcc, right?
I think it would make sense if you post your problem with more details on the STM (as they bought Atollic) forum (here it is not easy to share screenshots, etc).