How to organise a C project in Eclipse /w multiple binaries? - c

I'm in the process of converting a project to Eclipse CDT, it consists of 2 (static) libraries and produces about 12 binaries, a few of the binaries have 2-3 different build configurations, and is built by scons
How should I structure this in an Eclipse workspace ? 1 project for everything ? 1 project for each of the binaries/libs ? Something else ?

I'd suggest you use CMAKE for this problem, It should be able target target the Eclipse build system. If not, it can generate a normal 'make' config for you. It is far better to go down this route since its more portable in the long term, and writing a hierarchical build system is quite straight forward.

I personally have used Eclipse CDT before, but only in makefile mode i.e. to build anything I'd manually run the makefile. Basically I used Eclipse as a glorified editor. Here's how I worked things:
Everything part of the otherall solution came under the same workspace. Each library/binary was its own directory and project, so that I could make each as required. I also had a separate folder (project) for tests with a makefile that built all the test exes I wanted to run so I could do valgrinds on simple bits of it.
As I said, I used make and not Eclipse CDT's built-in building routines - to that end I'd say it really doesn't matter how you structure it - do whatever makes sense / conforms best to the UNIX principles.

Related

How to setup eclipse with C for university work

I'm fairly new to eclipse but have figured out how to use it with Java.
However, we are now moving on to C and I am having a hard time using it. I just want to use eclipse for my labs - i.e. to create / compile / test / run C exercises or tasks that have been set.
I created a new 'Labs' C project and have been creating the files ex1.c, ex2.c etc in the src folder. Eclipse doesn't like this due to more than one main across multiple files, but the files aren't related and each is supposed to have their own main.
Can someone advise me as to whether there is a better way to setup / organise my workspace for this labwork or alternatively how to compile / run single files at a time in eclipse?
You have a few options:
Create one project per executable (the projects can be in the same workspace). This is pretty self-explanatory, but it might get annoying if you have a lot of executables. Also, if you need to share code between your executables, you'd have to create a separate project for the shared code and set up dependencies.
Create one project with multiple build configurations, one per executable. See this answer for how to do that.
Use Eclipse for navigating and editing your code only, and build (and run/debug) your executables from the command line. This way, the organization of the Eclipse project(s) is irrelevant, and you can build from the command line however you want (at this stage, a simple Makefile is probably the easiest).
I prefer option #3, but to some extent it's a matter of taste; if you like to do everything including building from the IDE, go with #1 or #2.
EDIT: A simple Makefile might look like this:
ex1 : src/ex1.c
gcc -o ex1 src/ex1.c
ex2 : src/ex2.c
gcc -o ex2 src/ex2.c
...
put into a file named makefile, and you would then run make to build. (If you're on Windows you would write ex1.exe instead of just ex1.)
Have a look at a tutorial like this one to understand how Makefiles work.

How to use cmake on the machine which cmake is not installed

I am using the cmake to build my project. However, I need to build this project on a machine that I do not have the permission to install any software on it. I thought I can use the generated makefile but it has the dependencies on CMake,and says cmake:command not found.Is there any solution that force the generated makefile do not have any cmake related command such as check the system version? Thanks
Is there any solution that force the generated makefile do not have any cmake related command such as check the system version?
No. There is no incentive for cmake to provide such an option, because the whole point of the cmake system is that the cmake program examines the build machine and uses what it finds to generate a Makefile (if you're using that generator) appropriate to the machine. The generated Makefiles are tailored to the machine, and it is not assumed that they would be suitable for any other machine, so there is no reason to suppose that one would need to use one on a machine that does not have cmake. In fact, if you look at the generated Makefiles you'll find all sorts of dependencies on cmake.
Depending on the breadth of your target machine types, you might consider the Autotools instead. Some people dislike them, and they're not a good choice if you want to support Microsoft's toolchain on Windows, but they do have the advantage that an Autotools-based build system can be used to build software on machines that do not themselves have the Autotools installed.
one easy solution is to use static libraries and the 'static' parameter in the command line.
Then you should be able to drop the executable on the target machine and run it.

cmake install multiple version of the same library

I am trying to have a scheme with my library that is coherent and usable/reusable.
I work in a team where we work with continuous integration but sometimes I need to use old version of the same library. That's because some part of the software are not updated for using the new version.
I'm actually in the middle of a headache understanding how to use cmake for having something like this:
PATH/Library/Processor/Library_X/Version/static_library_and_includes
Where Library is a common name where to put my stuff
Processor could be attiny24, atmega, lxpXXXX, etc
Library_X is the name of the library
Version a progressive number from 0 to X
static_library_and_includes the static libraries built within that cmake module and the include files needed for using it.
How can I do this using cmake?
I work with different microprocessor crosscompiling with gcc. This is not a problem.
I work with static library, this is not a problem.
I can install them in the right directory. Not a problem
I can't ask the executable to link to the right .a file. Sometimes cmake pick the right one, sometimes not.
Can you please give me a hint on how you guys do it?
thank in advance
Andrea
See the search paths here: https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure .
CMake will find packages in directories named name*, so you can install to <prefix>/FizzBuzz-1.0.0 and <prefix>/FizzBuzz-2.0.0.
As long as each as a correct ConfigVersion.cmake file, it should do what you want.

How can I link the source path of a compiled library to a different location in Eclipse?

I've installed the msp430-gcc compiler and associated tools to do some open-source msp430 development at home using Eclipse. I'm developing on a slightly older Macbook Pro running OS X Lion and installed the tools using MacPorts. I'm running Eclipse 3.7.2 with the CDT and GCC Cross Compiler Support plug-ins. I have a simple empty main() written that compiles and links just fine.
The ELF parser lets me view the contents of the ELF binary just fine with the exception of one component; when I try to view the contents of the startup code in crt0.S, it gives me a blank file. When I click on the crt0.S component of the ELF, the filename bar at the bottom of the Eclipse window shows "/opt/local/var/macports/build/_Volumes_work_mports_dports_cross_msp430-gcc/msp430-gcc/work/gcc-4.6.3/gcc/config/msp430/crt0.S". This makes sense because of my MacPorts install of msp430-gcc. crt0.S is archived into /opt/local/lib/gcc/msp430/4.6.3/libcrt0.a on my machine.
What I want to be able to do is tell Eclipse to look elsewhere for the source files for the libraries that are automatically linked when I build with the msp430-gcc toolchain. This would presumably include everything in /opt/local/lib/gcc/msp430/4.6.3/. I started by downloading the source for mspgcc-20120406 (the version in my MacPorts install) and applying the gcc patchfile to an empty directory tree. This created the gcc/config/msp430 directory, including the crt0.S and crt0ivtbl.S files.
What I have had no luck accomplishing is telling Eclipse to look in ~/Developer/mspgcc-20120406/gcc-4.6.3/config/msp430/ instead of the path that's in the already-built libcrt0.a. I tried playing around with the Project Preferences->Paths and Symbols->Source Location window, but didn't have much luck. I searched through this website and on Google and the closest thing I came up with was this question but it doesn't "smell" like the right answer.
I would like to avoid solutions that involve moving the library source into my project. I'd rather have a solution that will work for multiple projects.
All help is greatly appreciated! Thanks in advance.
Try and check if the Eclipse linked resource could help you declare that external directory from within your Eclipse project here.
Linked resources are files and folders that are stored in locations in the file system outside of the project's location. These special resources can be used to add files and folders to your project that for some reason must be stored in a certain place outside of your project. For example, a linked folder can be used to store build output separately from your source files.
I find interesting how you can define that linked resource:
Linked resource target paths can be either defined as absolute paths, or relative to a path variable.
Since you can define it relative to (for instance) your Eclipse project location PROJECT_LOC, you can then setup your resource in a way which won't change between two environments.

Setting Default NetBeans Options (-std=c99, -Wall) for C programs

I have NetBeans 6.9 installed and working fine on Ubuntu Linux 11.10. My goal is to set compiler options like -Wall and -std=c99 to be used by default. Currently, I have to right click on my project -> Properties -> C Compiler -> Warning Level to "More Warnings" and add -std=c99 to Additional Options. This is obviously a pain when creating many projects, and I'm sure there is a way to make all of this the default.
I found this thread which relates closely to my question. However, the only answer involves installing Code::Blocks and MSYS 1.0.11, which doesn't make much sense to me. I don't see how installing another IDE will help me, and MSYS seems unnecessary, as I am already using Linux.
I have tried every reasonable search term I can imagine, and am very surprised how little info I have turned up. It seems like most everyone who uses NetBeans should have to change this type of thing at some point. Terms I'm searching for: 'NetBeans -std=c99 default', 'NetBeans set default compile options', 'how to make NetBeans use c99 by default' and 'Code::Blocks settings into NetBeans'.
Here are some different things that you can do:
Copy and share the configuration files between projects, so you won't have to set every setting.
Create a default project, and configure however you like it. When you want to create a new project, just copy the default project and rename it to something else.
Modify your toolchain properties.
For details, see here:
NetBeans settings for GCC
You can create a Project Template Module. I was aware this is possible but didn't try it before, and this link is explaining how to do it;
https://blogs.oracle.com/seapegasus/entry/tip_create_your_own_project
(First you need NetBeans with platform SDK, not just C/C++ Bundle.)
I followed steps;
Open/Create your project with settings you want to use.
Create a NetBeans module project, choose a Code Base Name, i gave my.templates.ctemplate1
Add "Module Development > Project Template" to the module project. Choose your C project in "Select Project" step.
At next step select the Category as C/C++ and give a name for template i used MyCTemplate1.
Right click on module project and select "Create NBM", it will create my-templates-ctemplate1.nbm in the build folder of the module project.
Tools > Plugins > Downloaded , click "Add Plugin" and point to the nbm file in the build folder. Select install, it will give warnings because it's not signed.
When you want to create a new C/C++ project you will see your template in the wizard which will have all the settings as your first C project has.
Enjoy other possibilities; files, folders in original project will be there.
Have you tried editing your netbeans.conf file (for me, it's in /usr/local/netbeans-7.0.1/etc/netbeans.conf) and adding the options you need to the netbeans_default_options line? From looking at my version of the file, you might need to prepend -J to the switches (hence -Wall would become -J-Wall.)

Resources