1

I have some *.c files. I want to have two binary from them for example:

a.c b.c -> ab

d.c e.c -> de

The a.c and d.c has main() function.

This is my code:

gcc  a.c b.c -o  ab.o -c
gcc -o ab ab.o
gcc  d.c e.c -o  de.o -c
gcc -o de de.o

I put this code in post-build-steps in codeblocks. The problem is when I compile the project it fails because of two main.

spring_64
  • 31
  • 5
  • 1
    Why do you want to do this in the first place? I suppose that you could define a preprocessor macro and use that to exclude one `main` or the other... The whole notion sounds crazy to me. – paddy Jul 18 '19 at 07:38
  • 1
    What is the problem with having two projects? You will anyway have two executable files. – Rishikesh Raje Jul 18 '19 at 07:48
  • I must have one project. – spring_64 Jul 18 '19 at 07:57
  • 1
    Your code doesn't make sense. You need to have one `.o` per `.c` file: `gcc -c a.c; gcc -c b.c; gcc -c d.c; gcc -c e.c; gcc -o ab a.o b.o; gcc -o de d.o e.o` – melpomene Jul 18 '19 at 08:04
  • This sounds like an [XY Problem](http://xyproblem.info). What are you _actually_ trying to achieve? You cannot have two "main" functions in the same executable, how would the system know which one should be invoked? – Jabberwocky Jul 18 '19 at 08:44
  • I couldn't help but notice the similarity between this question (and its responses) and my recent question asking [the same thing in a completely different environment](https://stackoverflow.com/questions/56914352/building-2-programs-in-a-c-sharp-project). It seems that people who work with IDEs have a very specific idea of what a "project" is, and it's pretty small, whereas people who write their own Makefiles are just using it as an ordinary word, and mean "all the related stuff I'm working on" (usually multiple programs and/or libraries) –  Jul 18 '19 at 12:17

1 Answers1

0

You can use a Makefile and put two binaries on it.

You can find some informations on this topic for example : A Makefile with Multiple Executables

Oliv
  • 120
  • 1
  • 12