Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Files 1

Status
Not open for further replies.

y2k1981

Programmer
Aug 2, 2002
773
0
0
IE
I'm a beginner in C so I need somebody to help me out here. What exactly is an object file. I know that you can create several object files from several .c files and then create the executible file using the object files. But what exactly are they? why can't you just create one .c file and compile that into one .exe.

I know that there's obviously reasons for this, they're just questions I'd like to know the answer to. Can somebody help me out... or point me to a good tutorial that can. Also, if anybody knows a general good but thorough tutorial on C... I'd be 4ever greatful!!!

Not asking for much am I!!!!! :)
 
y2k1981,
Large applications are composed of hundreds of thousands lines of code written by hundreds of programmers.
Try imagining all the code in a sngle file,how will different engineers modify the file while ensuring that no one else is modifying it at the same time,searching for code in the same file will be a horrendous job.
To make programming much more modular we try to limit the size of a single .c file.

There are two steps in making an executable,1) compiling all the code and generating the machine-code 2) Resolving all the unresolved symbols e.g. resolving all the functions which have been called in a particular C file.

the 1)st step produces an object file while both teps produce an executable file. An executable also has to have an entry routine,called main() in C.

I would suggest that U get a copy of K&R(kerningham and ritchie) book on C.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Thank you very much amit for your post. Very insightful... so much so there's dozens of questions running around my head right now!!!

You said about that there are 2 steps to making an executible, compiling the code and generate the machine language. I thought when you compiled the code, you WERE creating the machine language?

Thanks for the book recommendation too!!

:)
 
actually the whole stages of program creation is like following:

1. writting code;

2. compiling. in this step, 2 sub-steps is involved: (a) syntax checking, and (b) compiler your code into object code;

3. link. link the library that your program need to use with your program, and generates complete program.

in step 2, the compiler has to check for your syntax to make sure that is no error. the syntax error can prevent your program to be compiled and you program cannot be created successfully and run.
 
So when you're compiling, is there a temporary object file created before the end result (the executible) is produced?
 
Sure, although 'temporary' is not a very good description.

Normally a number of object-files (1 for each sourcefile compiled) are left over from a build (build=compile+link) which can be used also the next time a build is done.

The object file can be 'reused' if the corresponding source-file hasn't changed.

This can make a build much faster.

/JOlesen
 
Jolesen pointed out a good fact,the speed of the build.As he said you dont have to rebuild the files that haven't been modified.
In addition many compilers allow different files to be build in parallel,i.e it will create different processes to build different object files and when they are all done it will link all of them to genrate an executable.

There are many more tricks you can do with cc. Try out the capital S flag i.e 'cc -S filename.c'. It will genrate a filename.s file for you which is the assembly conversion of the .c file.
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Thanks for all the background info... really appreciate it. What's cc? I'm using the delorie compiler, which is invoked using the gcc command.

Again, many thanks:)
 
Delorie is technically not a compiler. It is a development system/environment which uses the underlying gcc compiler to compile your source code.

You can safely assume that cc and gcc are the same thing.
cc is the traditional C compiler on UNIX machines. The GNU C compiler(which you are using) is called gcc.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top