Run a program containing multiple source files in the GNU C compiler

I use DEV GNU c compiler on Windows 7 operating system. I need to know how to compile a program with multiple source files.
This is an example,

#FILE1
void f1()
{
printf("this is another file under same program");
}< br />
#FILE2

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

Actually I need this to test how static, extern class specifiers can be used with multiple files. Therefore, I now only need to understand how to use multiple files in a single program in C.

Thank you

The technical term for “multiple files” is 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 it usually doesn’t make sense unless you have a larger project (e.g. make) and want to reduce build time.

I am on Windows 7 The DEV GNU c compiler is used on the operating system. I need to know how to compile a program with multiple source files.
This is an example,

#FILE1< br />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 specifiers can be used with multiple files. Therefore, I Now just need to understand how to use multiple files in a single program in C.

Thank you

“Multiple files” The technical term is 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 it usually doesn’t make sense unless you have a larger project (e.g. make) and want to reduce the build time.

Leave a Comment

Your email address will not be published.