DEV-C + + compiling multiple C or C + + files at the same time


DEV-C + + compiles multiple C files at the same time:

To investigate the compilation of multi-source file programs and the use of header files:
If the function of a program is placed in different programs, it must be a constant definition instruction that is available for each file:
Define a * * *. H file to store function prototypes and constant definitions
Add “* *”. H “to the files to be compiled together

Compilation steps:

1. Create a new C blank project

2. Add the files to be compiled together to the above new project

3. Just compile and run, the same with C + +

The principle of C / C + + multi file compilation

In the case of a single file (only one. H and. C /. CPP), we only need to compile the file, for example:

$ gcc main.c -o main

But in more cases, a project needs to be divided into multiple source files, such as main. C, A.C, B.C, etc. How to compile in this case? First, the method in a.c must be invoked in main.c. The A.H header file must be included. The function declaration in the header file ensures the correctness of the function call of main.c. Now, let’s execute the compile multiple file command:

$ gcc main.c a.c b.c -o main

The whole compilation result is as follows: the compiler first compiles the source file as shown in main. C A.C B.C main.obj a. Obj b.obj target file, and then link the function methods to be used main.obj Finally, it is packaged into the executable main, which is the principle of separating compilation and linking.

How to compile a project composed of multiple C + + source files

When I write data structure jobs, because I write some data structures that need to be used, the source files are as follows:
Main.cpp, Stack.cpp, Stack.h, Queue.cpp, Queue.h
But when I write the source file in the IDE and want to compile it by myself, I find that I can’t handle multiple source files. After finding the data, I have the following solutions:

1. Compile multiple source files in the same statement at the same time

g++ -Wall -g Main.cpp Stack.cpp Queue.cpp -o StackOut

Generate executable stackout

In the G + + compiler, – wall is a parameter that allows all useful warnings that GCC can provide, – G is a parameter that tells G + + to generate debugging information that can be used by the gun debugger to debug your program. The specific usage of each parameter is detailed here and here.

2. Compile multiple source files separately

g++ -Wall -g -c Stack.cpp
g++ -Wall -g -c Queue.cpp
g++ -Wall -g -c Main.cpp
g++ -g Stack.o Queue.o Main.o -o StackOut

When compiling a single file, I tried it. The order is arbitrary. If I change it Stack.cpp , then:

g++ -Wall -g -c Stack.cpp
g++ -g Stack.o Queue.o Main.o -o StackOut


3. Write makefile

CC = g++  
CFLAGS = -Wall -g  

all: clean Stack.o Queue.o Main.o StackOut  

Stack.o : Stack.h  
    ${CC} ${CFLAGS} -c Stack.cpp

Queue.o : Queue.h  
    ${CC} ${CFLAGS} -c Queue.cpp  

Main.o : Stack.h Queue.h  
    ${CC} ${CFLAGS} -c Main.cpp  

StackOut : Stack.o Queue.o Main.o  
    ${CC} ${CFLAGS} Stack.o Queue.o Main.o -o StackOut
    rm -rf *.o  
    rm -rf StackOut

This article about how to compile multiple C or C + + files at the same time by DEV-C + + introduces this. For more information about compiling multiple C or C + + files, please search the previous articles of developepaer or continue to browse the following articles. I hope you can support developepaer in the future!