15150776
15150776

Reputation: 21

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?

Upvotes: 0

Views: 177

Answers (1)

HighCommander4
HighCommander4

Reputation: 52739

You have a few options:

  1. 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.

  2. Create one project with multiple build configurations, one per executable. See this answer for how to do that.

  3. 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.

Upvotes: 1

Related Questions