0

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?

15150776
  • 21
  • 7
  • You might want to try the CLion IDE https://www.jetbrains.com/clion/specials/clion/clion.html?gclid=Cj0KCQjw9ZDeBRD9ARIsAMbAmob1pWOsm3FvzdD_Ht69xnpiAKVVk-XMkFTsRB9aOB7kk9vwoJhEMOMaAtQdEALw_wcB&gclsrc=aw.ds&dclid=CK_PgqDdid4CFdNUGAodv5gHBg – Niklas Rosencrantz Oct 16 '18 at 00:42
  • @NiklasRosencrantz: A couple of things to note about CLion: (1) it's a commercial product, although there is a way for students to [apply for a free version for school work](https://www.jetbrains.com/student/), and (2) my understanding is its project model is currently tied to using the CMake build system. – HighCommander4 Oct 16 '18 at 01:10
  • At the risk of violating SO policy's dislike of product comparisons, I'd venture to suggest that Visual Studio targetting Linux hosts is a far better dev experience than can be had using Eclipse on Linux. Last time I tried Eclipse (quite recently), I found it slow, debugging was poor, and it was a dire memory hog. I switched to VS, haven't looked back. And it's free for most uses too! – bazza Oct 16 '18 at 20:43

1 Answers1

1

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.

HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • Apologies if a stupid question, but how would I use a simple makefile with the command line? The only way I know how to compile is using gcc and then running the compiled file. – 15150776 Oct 16 '18 at 09:24
  • @15150776: Makefiles are just a way to automate running the compiler commands, so you don't have to type out the entire compiler command each time. See my edit for an example. – HighCommander4 Oct 16 '18 at 19:35
  • 1
    You don't have to use the command line for this. Eclipse has a `Build Targets` *view* that you can add entries to from your `Makefile`. See this answert for details: https://stackoverflow.com/questions/47943231/is-all-is-default-in-eclipse-makefile/47944286#47944286 – Galik Oct 20 '18 at 06:13