Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 (e.g. with make) and want to reduce build times.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...