简体   繁体   中英

run a program with more than one source files in GNU c++ compiler

I am using DEV GNU c++ compiler on windows 7 OS. I need to know how a program with more than one source file can be compiled. here is example,

#FILE1
void f1()
{
   printf("this is another file under same program");
}

#FILE2

int main()
{
   f1();
   return 0;
}

Actually I need this to test how static, extern class specifier works with more than one file. So only I have to learn now how works with more than one files in a single program in C..

Thanks advance

The technical term for 'multiple files' would be translation units :

g++ file1.cpp file2.cpp -o program

Or you separate compilation and linking

g++ -c file1.cpp -o file1.o
g++ -c file2.cpp -o file2.o

# linking
g++ file1.o file2.o -o program   

But that usually doesn't make sense unless you have a larger project (eg with make ) and want to reduce build times.

最简单的方法是在gcc的命令行上精确调整两个文件:

gcc file1.c file2.c

To preprocess and compile as such:

gcc -c FILE1.c
gcc -c FILE2.c

Then, to link:

gcc -o EXECUTABLE FILE1.obj FILE2.obj

Alternately, you can do both in one step:

gcc -o EXECUTABLE FILE1.c FILE2.c

If it's a C++ program, then replace the gcc by g++ and the .c by .cpp .

It does not interest you, but for the benefit of similar readers who find your question later, FILE1.cpp may be named FILE1.cc or the like, and FILE1.obj may be named FILE1.o , depending on the reader's platform.

It may interest you that, depending on the shell you are using, you might have to write options like -o as /o .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM