Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple small programs in one code::blocks project

Tags:

c

codeblocks

I am new to Code::Blocks. For my studies I am programming several small programms in C (just to try things out). I tried to put them all in one project, because they belong to the same topic, but that doesn't work because each of them has a main function.

Any ideas how I can put those files together somehow, but not depending on each other?

like image 251
Laura Avatar asked Jan 18 '13 10:01

Laura


1 Answers

Suppose your source files are called

  • prog1.c
  • prog2.c
  • mylib.c
  • mylib.h

where prog1.c and prog2.c each contain a main() function and mylib.c (with the corresponding header file mylib.h) contains some library functions that should be linked to each program. In your Code::Blocks project you need to create multiple targets now. From the menu choose 'File/New/Build target...' and name them 'prog1' and 'prog2'. Now add all of your source files to the project (in case you haven't done so already).

Now right-click on prog1.c and select "Properties..." from the context menu. In the dialog box choose the 'Build' tab and make sure that under "Belongs to target" only "prog1" is checked. Do the same with prog2.c and the target "prog2". For "mylib.c" make sure that both "prog1" and "prog2" are checked.

Now you can easily choose in the UI which build target to build and run. Note that if you add another target, say "prog3", then you have to go to the build properties of prog1.c and prog2.c again and uncheck "prog3".

like image 90
Elmar Zander Avatar answered Oct 04 '22 03:10

Elmar Zander