Click here to Skip to main content
15,892,768 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Spint control Pin
RockyJames22-Sep-06 20:23
RockyJames22-Sep-06 20:23 
GeneralRe: Spint control Pin
_AnsHUMAN_ 22-Sep-06 21:01
_AnsHUMAN_ 22-Sep-06 21:01 
GeneralRe: Spint control Pin
RockyJames22-Sep-06 23:56
RockyJames22-Sep-06 23:56 
GeneralRe: Spint control Pin
Hamid_RT23-Sep-06 8:56
Hamid_RT23-Sep-06 8:56 
Question[DESPERATED]Linking probs... Pin
Lord Kixdemp22-Sep-06 14:10
Lord Kixdemp22-Sep-06 14:10 
AnswerRe: [DESPERATED]Linking probs... Pin
Jörgen Sigvardsson22-Sep-06 14:28
Jörgen Sigvardsson22-Sep-06 14:28 
GeneralRe: [DESPERATED]Linking probs... Pin
Lord Kixdemp22-Sep-06 14:45
Lord Kixdemp22-Sep-06 14:45 
GeneralRe: [DESPERATED]Linking probs... Pin
Jörgen Sigvardsson22-Sep-06 15:18
Jörgen Sigvardsson22-Sep-06 15:18 
Include guards will only guard against multiple inclusions of the same header file in a single .cpp-file.

When the compiler runs, it compiles your .cpp-files, one by one. The compilation process is divided into two phases: preprocessing and C++ compilation. The preprocessor expands all preprocessor directives (#include is one such directive) before it passes the preprocessed source code to the compiler, which in turn generates object code.

When all files have been compiled, the linker comes into play. The linker will join all the object code files produced by the compiler, into a single file (.exe, .dll).

Now that you know the basics of the compilation and linking process, it will not be hard to understand why the include guards cannot help you in the case of pwnz. Suppose we have this include file:
// inc.h
#ifndef __INC_H__
#define __INC_H__
int x;
#endif
And a source file:
// src1.cpp
#include "inc.h"
and a second source file:
// src2.cpp
#include "inc.h"
.


  1. The preprocessor processes the file src1.cpp file. It will replace the #include-line with the contents of inc.h. The net effect of this would be a single line in src1.cpp, declaring an int variable with name x. No problems so far.
  2. The preprocessor sends the processed code to the compiler, which sees the variable declaration. No problems so far. An object code file is produced: src1.obj
  3. The preprocessor processes the file src2.cpp. As with src1.cpp, its contents will be the same (because it includes the same include file). No problems so far.
  4. Same thing here as in 2). The compiler sees the variable declaration. Since src1.cpp and src2.cpp are compiled independently, there is no name collision. The variable declared in src1.cpp is not seen by the compiler in src2.cpp. No problems here either. src2.obj is produced.
  5. All source files have been compiled, so it's time for the linker. The linker examines all object files before it attempts to join them into an .exe-file. What it looks for is to make sure that names are not colliding. In this case, the linker will see that both src1.obj and src2.obj declares a variable with the name x. That is an error, because x is no longer unique - which memory location does x refer to? It can be one of two - and that is something the linker does not like! That is also the error you had in your project.

If you keep in mind that the linker doesn't like ambigous names, and that #include directives are basically "copy'n'paste" operations, you won't run into these problems. If you want to share a variable between two or more source files, you should do something like this:
// inc.h
#ifndef __INC_H__
#define __INC_H__
extern int x; // Notice the extern keyword
#endif // __INC_H__
And:
// src1.cpp
#include "inc.h"
int x; // this is the actual declaration of the variable
and finally
// src2.cpp
#include "inc.h"
The actual declaration is done in only one source file. The "extern" keyword is a heads up for the compiler that the variable name is reserved, and that it is located in some source file. The compiler then puts a filler value in each instance which you use the variable x. The linker will then fill in the filler value in each instance with the real location of x (in this case, the x that is declared in src1.cpp).

A final note about the inclusion guards. Since the compiler and preprocessor process each source file separately, any #define made during the processing of one source file (or any of its included files), are "reset" before the next source file is processed.

I hope I made sense. Good luck with your project! Smile | :)


--
A Stern Warning of Things to Come

GeneralRe: [DESPERATED]Linking probs... Pin
Lord Kixdemp22-Sep-06 16:52
Lord Kixdemp22-Sep-06 16:52 
GeneralRe: [DESPERATED]Linking probs... Pin
Jörgen Sigvardsson22-Sep-06 22:33
Jörgen Sigvardsson22-Sep-06 22:33 
GeneralRe: [DESPERATED]Linking probs... Pin
Lord Kixdemp23-Sep-06 12:08
Lord Kixdemp23-Sep-06 12:08 
GeneralRe: [DESPERATED]Linking probs... Pin
Jörgen Sigvardsson23-Sep-06 12:11
Jörgen Sigvardsson23-Sep-06 12:11 
GeneralRe: [DESPERATED]Linking probs... Pin
Lord Kixdemp23-Sep-06 12:33
Lord Kixdemp23-Sep-06 12:33 
AnswerRe: [DESPERATED]Linking probs... Pin
ThatsAlok22-Sep-06 17:35
ThatsAlok22-Sep-06 17:35 
Questionwhat is the use of "pOldPen" here? Pin
bloodwinner22-Sep-06 11:16
bloodwinner22-Sep-06 11:16 
AnswerRe: what is the use of "pOldPen" here? Pin
David Crow22-Sep-06 11:23
David Crow22-Sep-06 11:23 
GeneralRe: what is the use of "pOldPen" here? Pin
bloodwinner22-Sep-06 11:30
bloodwinner22-Sep-06 11:30 
GeneralRe: what is the use of "pOldPen" here? [modified] Pin
Rob Caldecott22-Sep-06 12:21
Rob Caldecott22-Sep-06 12:21 
GeneralRe: what is the use of "pOldPen" here? Pin
Jörgen Sigvardsson22-Sep-06 14:25
Jörgen Sigvardsson22-Sep-06 14:25 
GeneralRe: SaveState()? Pin
PJ Arends22-Sep-06 16:07
professionalPJ Arends22-Sep-06 16:07 
GeneralRe: SaveState()? Pin
Jörgen Sigvardsson22-Sep-06 22:27
Jörgen Sigvardsson22-Sep-06 22:27 
GeneralRe: SaveState()? Pin
Rob Caldecott22-Sep-06 23:47
Rob Caldecott22-Sep-06 23:47 
GeneralRe: SaveState()? Pin
Hamid_RT23-Sep-06 8:57
Hamid_RT23-Sep-06 8:57 
QuestionWhich slider is active in dialog? Pin
Oliver12322-Sep-06 11:10
Oliver12322-Sep-06 11:10 
AnswerRe: Which slider is active in dialog? Pin
David Crow22-Sep-06 11:21
David Crow22-Sep-06 11:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.