Exporting static const int data member from shared library

Exporting static const int data member from shared library

Post by Z. Majee » Fri, 03 Oct 2003 12:55:06

I'm building a C++ shared library with CodeWarrior 8.3 on Mac OS X 10.2.6.
In one class I have the equivalent of:


class MyClass {
static const int mem1;


#include "MyClass.h"
const int MyClass::mem1 = 1;

The library is built with the equivalent of:

mwcc -c -fpic -g -gccinc -o MyClass.o MyClass.cpp
mwld -shared -export all -o libMyClass.lib MyClass.o

This sample contains the essence of the real code but unfortunately does not
reproduce the problem. In the sample libMyClass.lib exports MyClass::mem1.
But in the real code, MyClass::mem1 is not exported from libMyClass.lib.

In the real code, looking at the object file MyClass.o with "mwld -dis -show
only,names" I see MyClass::mem1 DEFINED, but "nm libMyClass.lib" shows the
library does not export MyClass::mem1; the symbol is also missing from the
mapfile. So I get an undefined reference error when building an app with the
shared library.

Here's the strange part: if I make mem1 non-const or change its type to
static const char, then the symbol is exported from the library and my app
links without errors.

Any ideas why a static const int data member would not be exported from a
shared library? Or how I could debug this problem; a mapfile has not helped.

By the way,the same code works on Windows, Unix, and Linux, and with gcc and
mwpefcc on Mac OS X.


1. configure Call Library Function for int Do(const int iE, const int iC, WORD mD )

2. About static const members appearing in another static const definitions

I am having this code here.

static const float PI = 3.14159;
static const float INC = 0.4f * PI;

When I compile my program, I get the following error,

error: `MyClass::PI' cannot appear in a constant-expression .

I was wondering why this is not possible here. May be is it because
there is no guaranteed order of initialization among C++ members ?

But when, intuitively, I had defined PI here, and then use PI later in
a subsequent definition. Would it not be possible to use PI
here,(since i can't change PI anymore ) ?

3. problem passing a static const int member by reference

4. Casting int'** to 'const int * const * const' dosn't work, why?

5. How is "static const int" better than "static enum"?

6. const int versus #define - does "const int" take up space in exe?

7. What is Difference between const int i and int const i

8. unsigned int const does not match const unsigned int

9. How to Initialize const static object data members in a header file?

10. Can a class have a non-static const array as a data member?

11. initialization of a const static float data member in a class

12. static const data member

13. const member fn changes static data

14. static const non-integral data members initialized within a class