| | |
Compilation process
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 18
Reputation:
Solved Threads: 0
Hello,
i am a naive in C/C++ and pretty confused about how does the compilation process work in the background, can you explain in detail (or give me a link about it) ? in particular can you please tell me: how are declarations in header file associated with the definitions in the .CPP file??Basically, I don't understand how a header file being included makes a connection with the source file that has the definitions for whatever is in the header file. It is my understanding I could name my header files something completely
unrelated to the source files which contain the definitions and including the header files would still make the defined functions/whatnot usable. How?
i am a naive in C/C++ and pretty confused about how does the compilation process work in the background, can you explain in detail (or give me a link about it) ? in particular can you please tell me: how are declarations in header file associated with the definitions in the .CPP file??Basically, I don't understand how a header file being included makes a connection with the source file that has the definitions for whatever is in the header file. It is my understanding I could name my header files something completely
unrelated to the source files which contain the definitions and including the header files would still make the defined functions/whatnot usable. How?
Last edited by Ashu@sym; Oct 11th, 2007 at 10:02 am.
•
•
•
•
how are declarations in header file associated with the definitions in the .CPP file??
#include "myheader.h" at the top of the cpp file. #include tells the pre-processor to bung the code in "myheader.h" right there.Yes you can call the header whatever you like, but why would you? it's not logical and programming is all about logic.
Why are they separate?
Because if you make flexible reuseable code (see my comments about being logical) it can be pre-compiled into a libray and used directly in other projects without having to be re-compiled for each project. The header file remains in its "plain text" state so the user of your library can see what methods are vailable and what they return etc. without having to skirt arround reams of code.
the compiler process basically is:
Pre-process (#include, #define, #ifndef macros etc...)
Compile .cpp into .o object files
link object files to an .exe or .dll blah...
The content of header file is prepended to source file.
>Basically, I don't understand how a header file being included makes a connection
>with the source file that has the definitions for whatever is in the header file.
It doesn't. There's no connection at all. The header is there to declare names so that you can compile your source. If the names aren't declared, you'll get compilation errors:
But if you declare the name, even if the object itself wasn't defined, the compiler won't complain:
When you try to build this code, the linker will start complaining because x was only declared, never defined. That's a different issue entirely. You can add another file that defines x and everything will work when you compile and link them together (no headers involved):
All a header does is provide declarations. It doesn't know about the linker, or any implementation files. It declares names and doesn't give a hooey about whether they're defined or not. That's the job of the linker. So, you can throw the
The #include direction just replaces itself with the contents of the file you specify, so after preprocessing header.h, file 1 looks just like this (which is functionally identical to what we did before):
That's all there is to it.
>with the source file that has the definitions for whatever is in the header file.
It doesn't. There's no connection at all. The header is there to declare names so that you can compile your source. If the names aren't declared, you'll get compilation errors:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main ( void ) { x = 123; /* This line won't compile */ printf ( "%d\n", x ); return 0; }
C++ Syntax (Toggle Plain Text)
#include <stdio.h> extern int x; /* Declared but not defined */ int main ( void ) { x = 123; /* Now this line compiles */ printf ( "%d\n", x ); return 0; }
C++ Syntax (Toggle Plain Text)
/* File 1 */ #include <stdio.h> extern int x; /* Declared but not defined */ int main ( void ) { x = 123; /* Now this line compiles */ printf ( "%d\n", x ); return 0; }
C++ Syntax (Toggle Plain Text)
/* File 2 */ int x;
extern int x; part into a header and call it good: C++ Syntax (Toggle Plain Text)
/* Header file */ extern int x; /* Declared but not defined */
C++ Syntax (Toggle Plain Text)
/* File 1 */ #include <stdio.h> #include "header.h" int main ( void ) { x = 123; /* Now this line compiles */ printf ( "%d\n", x ); return 0; }
C++ Syntax (Toggle Plain Text)
/* File 2 */ int x;
C++ Syntax (Toggle Plain Text)
/* File 1 */ #include <stdio.h> /* Header file */ extern int x; /* Declared but not defined */ int main ( void ) { x = 123; /* Now this line compiles */ printf ( "%d\n", x ); return 0; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Installing software on Linux: source code (*nix Software)
- Introducing myself and a n00b help question. ;) (C++)
- GUI programming under suse 10.2 (IT Professionals' Lounge)
- code omitted because of length (*nix Software)
- How to get path of current appl (in char) for Mac OS X (C++)
- This all right for Gentoo? (Getting Started and Choosing a Distro)
- C++ conceptual question (C++)
- Compile C code in Linux (C++)
- MASM "HELP NEEDED" (Assembly)
Other Threads in the C++ Forum
- Previous Thread: queue program
- Next Thread: Solution for Big Integer used Linked - List.
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






