VC++ 10 Ignoring preprocessor directives


Hi, I'm having some issues probably because of my lack of experience but I can't see it through. This code has to be with OpenGL and I'm using Visual Visual C++ 2010

I have 2 cpp files A and B, and A has a header file, C included in A and B.

A is just some base code so I can programm and change B at my will. And I had to declare some variables that A and B could read so I declared them at C.

I have preprocessor directives so the structs, functions and vars at C aren't declared twice:
#ifndef GL_FRAMEWORK__INCLUDED
#define GL_FRAMEWORK__INCLUDED

But I'm always having issues with already defined vars (not structs or functions) when linking B

Can someone tell me what am I doing wrong please?

Thanks a lot
What is the contents of that header? If a header contains something that can't be defined multiple times (like non-inlined functions) and you include it in multiple files, the linker will not be too happy with you.
closed account (zb0S216C)
khanser wrote:
And I had to declare some variables that A and B could read so I declared them at C

If you define objects within a header module, you're asking for the multiple definition error. When a header module is included into another header module, the header that included the other obtains a copy of all the objects defined within that header module. For example:

Header_A.h
1
2
3
4
5
6
#ifndef _H_A_
#define _H_A_

int Object( 0 );

#endif 


Header_B.h
1
2
3
4
5
6
#ifndef _H_B_
#define _H_B_

int Object( 0 );

#endif 


Header_C.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef _H_C_
#define _H_C_

#include "Header_A.h"
#include "Header_B.h"

// Including both A, and B modules will result in this:
int Object( 0 ); // Front Header_A.h
int Object( 0 ); // Front Header_B.h

#endif 


Wazzak
Last edited on
Since you are using VS 2010 check out __declspec(selectany), you use it as a prefix to any variables in h-files (that will be included in more than 1 source file) and the VS linker only chooses one of them.

1
2
//some H-File
__declspec(selectany) int n1;
@Framework
It's more like:

Header_C.h
 
     int my_var = 0;


Source_A.cpp
1
2
    #include("Header_C.h");
    someFunc( my_var);



Source_B.cpp
1
2
    #include("Header_C.h");
    someFunc2( my_var);


@L_B The header of the functions are in C.h, but the declaration of the functions is not repeated.

@binarybob350 I will check this out :D

Thanks for your time :)


Topic archived. No new replies allowed.