Several small programs in one block :: block project

I am new to Code :: Blocks. For my classes, I program several small C programs (just to try everything). I tried to put them in one project, because they belong to the same topic, but this does not work, because each of them has a main function.

Any ideas how I can somehow connect these files, but independently of each other?

+4
source share
5 answers

let's say that your project contains 3 c files, and each c file is a separate program and contains its own main function:

  • program1.c
  • program2.c
  • program3.c

So, you can write a Makefile (its name on your computer should be "Makfile" ). He will build the program you want separately

Here's what your makefile looks like:

 all: program1 program2 program3 %.o: %.c $(CC) $(CFLAGS) -c -o $@ $^ program1: program1.o $(CC) $(LDFLAGS) -o $@ $^ program2: program2.o $(CC) $(LDFLAGS) -o $@ $^ program3: program3.o $(CC) $(LDFLAGS) -o $@ $^ clean: rm -f *.o program1 program2 program3 

Using the above Makefile, you can create each program separately

Example

 $ make program1 

will build only program1

or you can create all programs with

 $make all 
+1
source

Suppose your source files are called

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

where prog1.c and prog2.c contain the main () function, and mylib.c (with the corresponding header file mylib.h) contains some library functions that must be associated with each program. In the Code :: Blocks project, you need to create several goals. From the menu, select "File / Create / Create Target ..." and name them "prog1" and "prog2". Now add all the source files to the project (if you have not already done so).

Now right-click on prog1.c and select "Properties ..." in the context menu. In the dialog box, select the "Create" tab and make sure that only "prog1" is checked in the "Belongs to targeting" section. 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 select in the user interface, which creates a target for assembly and launch. Note that if you add another target, say, β€œprog3”, you need to go back to the assembly properties of prog1.c and prog2.c and uncheck β€œprog3”.

+7
source

A C program should contain only one core function. Separate all your individual programs as Functions and put them in a single C program, or you can even put it in multiple files and compile them.

You can use switch case to call various functions.

Remove main function from all programs and convert them to functions ..

Call them where ever you need it .. It will help you.

+2
source

It seems to me that you are starting to create some useful utility functions. Therefore, follow the tips offered by raghu-srikanth-reddyce and create separate functions for each small program. However, I would add that it would be better to create a simple C library for yourself to save all of them that you can reference at any time. Most professional programmers keep such libraries.

A simple make file allows you to create the final binary file that you can reference.

Good luck with programming;)

+1
source

If you want to compile in a single file, you do not need a Project File . Just create a new file , then write all the code in one file. Also use the Function and Procedure if you need it.

0
source

All Articles