Instead creating a new project from scratch, often it is simpler to copy an existing Eclipse CDT project, then change it and go on. To copy-past the a project in Eclipse:
- Select the project in the Project Explorer View (CTRL-C on Windows)
- Then paste it in the Project Explorer View (CTRL-V on Windows), and I can specify the new name:
However, to make that process simpler, a few things have to be done right in the ‘source’ project first.
I’m using the ‘copy-paste’ project in my classes too, where students get a ‘master’ project they can use as a base, multiple times during the labs if necessary.
Linked Files and Folders
Eclipse copies all the files in the project folders. Linked files and folders (see “Link to Files and Folders in Eclipse“) in the project are still linked files and folders. So they will be shared between the original project and the copy one.
Usually I avoid linked files and folders, as they easily can be a source of problems. If using links, then make them project relative, but this means that the copied project needs to match this, or the linked location needs to be updated. If you want to have the
To make projects easy to copy, avoid any linked files or folders: have the source project ‘standalone’, means have the necessary files be physically present in the source project, and not outside.
Artifact Name
With the copied project getting a new name, make sure that the build output (artifact name) is not a hardcoded one. Use
${ProjectName}
instead:
That way, the output file for the new project is matching the new project name.
Refresh Policy
There is an old bug in Eclipse with the Refresh Policy (see “Eclipse Project ‘Refresh Policy’: Broken Incremental Build with External Make?“). Make sure that the resources shown in the ‘Refresh Policy’ is matching your project:
Build and Compiler Settings
The compiler/assembler/linker needs some path and file information. Do *not* use absolute paths like “C:\myProjects\…”. Instead, use the the Eclipse CDT project variable ${ProjDirPath} and have paths surrounded by double quotes (if your project is named ‘Copy of MyProject’ or similar).
Even better, use a relative path like
"../Sources"
as shown below:
The ‘current directory’ of the build tools (at least for the GNU ARM Eclipse integration) is the build directory location:
Using “../Sources” not only makes things relative to the project, it shortens the command line length which avoids a common length problem (see “Solving the 8192 Character Command Line Limit on Windows“).
Oh, and if you have not noticed: I’m using the ‘Linux’ forward slashes in the paths:
"../Sources"
instead of “..\Sources”, that way I keep things compatible between Windows and Linux/Mac :-).
Clean
As with the copy we got all the build things from the earlier project, it is a good idea to make a Project > Clean to get rid of that old stuff:
Now build the project, and it should (hopefully) build just fine.
Debug Launch Configurations
Now this is the most complicated part :-(.
To make sure the debug configurations get copied too, make sure they are stored as files in the projects (see “Sharing Debug Configuration with Eclipse“):
Now they get copied, but having the old names (remember: Eclipse just does the file copy, it has no clue about the content):
So one obvious step would be to rename the files matching the new project name. It is optional because Eclipse CDT only cares about the .launch file extension to recognize something as a debug launch configuration. However, if still the ‘old’ and ‘copied’ project are present in the workspace, renaming avoids confusion.
The new configurations show up, but they have the wrong project name and application file listed:
An easy way is to change them in the above dialog. Another way is to use a text editor or script file to change the following string attribute keys in the launch file:
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="application.elf"/> <stringAttribute key="org.eclipse.cdt.launch.PROJECT_ATTR" value="ProjectName"/>
Summary
With following a few basic things, a copy of an existing Eclipse CDT project is not that hard. Make sure you are not using linked files and folders, that the build artifact and path names are not hard-coded, and things should be much easier. While I have used the NXP Kinetis Design Studio in above screenshots, things are very similar to other Eclipse distributions too.
Happy Copying 🙂
Remember the good old days of burning EPROMs. 🙂
LikeLike
The last time I used the Export feature it recreated the project but also de-linked the linked files and made local copies. This export feature could be usefull in creating archives.
Preserving a project so that it could be recompiled 10 years later would be a challenge.
LikeLike
Hi Chad,
yes, I always prefer ‘standalone’ projects. I don’t understand why the Kinetis SDK is using linked files and folders, it just makes it very hard to use the project for my own ones. And thanks for the tip about File > Export > Archive File file solution: I did not realize that it copies the files and not the links :-). Still it would be necessary to update the compiler/etc search paths, but that’s way better than doing it manually.
We have projects where we indeed gurantee to build and maintain it for 10 years. For this we put away the notebook which is used to build it, because that’s the only way to preserve the whole environment and machinie settings.
To use virtual machines could be a solution, but who knows if that virtual machine will still run in 10 years?
LikeLiked by 1 person
I have VMware VMs that I created in 2008 that still work. 🙂
LikeLike
Bill,
that’s great to know. I was not so lucky with software running on PC machines which got updated.
LikeLike
Hi Erich,
You forgot to mention that one needs to update the ‘refresh policy’ under the C/C++ Build property. If you forget this step you will be compiling every file every time because it is using the old project to find the dependencies.
Brynn
LikeLike
Hi Brynn,
indeed, I have missed that one. I have added it now to the article.
Thanks!
LikeLike
Hello Erich,
Im tring to duplicate this project I have in the IDE but I keep getting this error
E0562300:Duplicate symbol “_main” in “.\src\cg_src\r_cg_main.obj”
Any idea on what could be causing it?
thank you
LikeLike
This means you have two main() functions in your project. Have you checked this?
One is for sure in src/cg_src/r_cg_main.c, but you must have another one. Or you include that r_cg_main.c instead the header file?
LikeLike
Yes I checked. I used findandreplace.io on the whole project. but there is nothing.
What do you mean “instead the header file”?
LikeLike
why not using the search built-in in Eclipse?
What I mean is that instead of
#include “r_cg_main.h”
you have
#include “r_cg_main.c”
somewhere in your code.
LikeLike
I could have used the built-in.
There is no r_cg_main.h file on my project neither a #include “r_cg_main.h”
LikeLike
I suggest you check the project settings/linker settings on this, if they match the original project.
LikeLike