gdc and gcc object linking issues

Andrej Mitrovic andrej.mitrovich at gmail.com
Wed Jun 6 11:20:55 PDT 2012


This is a bit more related to C++ than D, it has to do with wrapping.
I've got 4 files:

test.h:
class Class
{
public:
    static int statField;
};

test.cpp:
#include "test.h"
extern "C" __attribute__((dllexport))
int getStatField()
{
    return Class::statField;
}

test.d:
extern(C) int getStatField();
class Class
{
    static int statField()
    {
        return getStatField();
    }
}

main.d:
import test;
void main()
{
    int x = Class.statField();
}

This is how I compile it (XP32):
g++ -m32 -g -I. -c test.cpp -o test_cpp.o
gdc -m32 -g -I. -c test.d -o test_d.o
gdc -m32 -g -I. -o main.exe test_cpp.o test_d.o main.d -lstdc++

But I get a linker error:
test_cpp.o: In function `getStatField':
D:\dev\code\d_code\testcpplink/test.cpp:5: undefined reference to
`Class::statField'
collect2: ld returned 1 exit status

If I change the static int field to a static function (and add
parenthesis for the function call) the linking works fine, e.g.:

test.h:
class Class
{
public:
    // was: static int statField;
    static int statField() { return 1; }
};

test.cpp:
#include "test.h"
extern "C" __attribute__((dllexport))
int getStatField()
{
    // was: return Class::statField;
    return Class::statField();
}

But for variables it doesn't link. What am I doing wrong?


More information about the Digitalmars-d-learn mailing list