Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

2.4.1 Creating object files from source files

The command-line option -c is used to compile a source file to an object file. For example, the following command will compile the source file 'main.c' to an object file:

$ gcc -Wall -c main.c

This produces an object file 'main.o' containing the machine code for the main function. It contains a reference to the external function hello, but the corresponding memory address is left undefined in the object file at this stage (it will be filled in later by linking).

The corresponding command for compiling the hello function in the source file 'hello_fn.c' is:

$ gcc -Wall -c hello_fn.c

This produces the object file 'hello_fn.o'.

Note that there is no need to use the option -o to specify the name of the output file in this case. When compiling with -c the compiler automatically creates an object file whose name is the same as the source file, but with '.o' instead of the original extension.

There is no need to put the header file 'hello.h' on the command line, since it is automatically included by the #include statements in 'main.c' and 'hello_fn.c'.


 
 
  Published under the terms of the GNU General Public License Design by Interspire