include - .h and static consts, strange behavior

C

Christopher Pisz

I normally never define a global in a header file, but some preexisting
code I am working on does the following:

-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 (100);
#define someGlobal2 (157);
//etc.

#endif


-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip
private
SomeType[someGlobal] m_anArray;
};
 
D

Dombo

Op 17-Mar-12 20:04, Christopher Pisz schreef:
I normally never define a global in a header file, but some preexisting
code I am working on does the following:

-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 (100);
#define someGlobal2 (157);
//etc.

#endif


-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip
private
SomeType[someGlobal] m_anArray;
};
-----

I get a compile error that someGlobal is undefined. I think what was
done is legal, at least on whatever standard VC 2008 uses. What am I
missing?

A 1 or a 2.
 
C

Christopher Pisz

A 1 or a 2.

Whoops, that was just a typo while posting. There is a 1 there.


-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 (100);
#define someGlobal2 (157);
//etc.

#endif


-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip
private
SomeType[someGlobal1] m_anArray;
};
 
V

Vianney Lançon

Le 17/03/2012 20:04, Christopher Pisz a écrit :
I normally never define a global in a header file, but some preexisting
code I am working on does the following:

-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 (100); <== error
#define someGlobal1 100
#define someGlobal2 (157); <== error #define someGlobal2 157
//etc.

#endif


-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip
private <== error private:
SomeType[someGlobal] m_anArray; <== error
SomeType m_anArray[someGlobal];
 
C

Christopher Pisz

SomeType m_anArray[someGlobal1];

-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 100
#define someGlobal2 157
//etc.

#endif

-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip

private:

SomeType m_anArray[someGlobal1];
};


Compiler still says someGlobal1 is undefined.
 
V

Victor Bazarov

SomeType m_anArray[someGlobal1];

-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 100
#define someGlobal2 157
//etc.

#endif

-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip

private:

SomeType m_anArray[someGlobal1];
};


Compiler still says someGlobal1 is undefined.

Perhaps if you lose the 'static', you get it resolved... Not sure about
C, though (it's been many years), consider asking in the C group (down
the hall to the left).

V
 
G

Geoff

I normally never define a global in a header file, but some preexisting
code I am working on does the following:

-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 (100);
#define someGlobal2 (157);
//etc.

#endif


-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip
private
SomeType[someGlobal1] m_anArray;
};
-----

I get a compile error that someGlobal1 is undefined. I think what was
done is legal, at least on whatever standard VC 2008 uses. What am I
missing?

__cplusplus is implementation defined in C++ programs only.
What makes a file a C++ program in Visual C++?
Answer: The file name extension is .cpp
 
T

Tobias Müller

Christopher Pisz said:
SomeType m_anArray[someGlobal1];

-------
//a.h
// Someone wrote a comment that this header must be C compilable

// I assume MSVC defines this
#ifdef __cplusplus
static const long someGlobal1 = 100;
static const long someGlobal2 = 100;
// etc.

#else
#define someGlobal1 100
#define someGlobal2 157
//etc.

#endif

-------
//b.h

#include "a.h"

class MyClass
{
public:
// Snip

private:

SomeType m_anArray[someGlobal1];
};


Compiler still says someGlobal1 is undefined.

Those are two header files. Header files are never compiled directly but
included in .cpp files. So what does the cpp files look like?
And did you really verify that this minimal example doesn't compile? (and
not just the original source code where you extracted those lines).

Tobi
 
C

Christopher Pisz

Those are two header files. Header files are never compiled directly but
included in .cpp files. So what does the cpp files look like?
And did you really verify that this minimal example doesn't compile? (and
not just the original source code where you extracted those lines).

Tobi

I made a new console project day before yesterday. It is a bit different
than the original source. The minimal example compiled OK.

I then tried adding another factor to the minimal example, in that the
one header was in a separate project. It still compiled OK.

So, I am left thinking that my minimal example isn't really an example
of what is going on at all and that the compile error in the original
code is due to some other unknown factor. It being unknown, I can't make
an example of it. So therefore, I suppose this topic is dead.

Apologies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top