Board index » cppbuilder » For the compiler/linker to keep a class.

For the compiler/linker to keep a class.


2006-08-09 05:59:35 PM
cppbuilder25
Hi all,
I know this forum is not for technical questions, but I could not find
one that matches the area I'm looking at. If you figure out a best
place, please tell me.
Anyway, here is my problem:
I'm using the #pragma start to mimick the initialization section from
Delphi, doing this:
void __initialization_TestLibrary_Intf();
#pragma startup __initialization_TestLibrary_Intf
void __initialization_TestLibrary_Intf()
{
RegisterSomeClass(__classid(TTestStruct));
}
And TTestStruct is declared inside the TestLibrary namespace as such:
// TestStruct
class DELPHICLASS TTestStruct;
class PASCALIMPLEMENTATION TTestStruct : public TObject
{
// Allow the use of "inherited" in our class as Delphi allows
typedef TObject inherited;
private:
AnsiString fNewField;
public:
virtual void __fastcall Assign(Classes::TPersistent* iSource);
__published:
__property AnsiString NewField = {read=fNewField, write=fNewField};
public:
#pragma option push -w-inl
inline __fastcall virtual TTestStruct(): TObject() {}
#pragma option pop
#pragma option push -w-inl
inline __fastcall virtual ~TTestStruct() {}
#pragma option pop
};
The code compiles just fine, that's great. However, the linker gives me
an error with this message (translated from French):
[Linker Error] Error: Unresolved external 'TestLibrary::TTestStruct::'
referenced by D:\SOURCES\TESTCPP\DEBUG_BUILD\TESTLIBRARY.OBJ
The TTestStruct is not used anywhere but for the __classid call, and I'm
guessing this makes the compiler or linker decide the class is of no
interest and can be stripped off the object file.
I would like to force the compiler or linker to put that class inside
the object file regardless, and tried to figure out how to do so. One
thing that came to mind is to use the "_export" keyword, but that is not
compatible with the PASCALIMPLEMENTATION keyword, which I MUST keep.
I also tried declaring an extern variable of type TTestStruct*, but that
did not help either.
Is there some sort of option I can pass to the compiler or linker to say
that a symbol should be kept regardless of it being used or not?
Thanks for your help
Olivier Sannier
 
 

Re:For the compiler/linker to keep a class.

OBones wrote:
Quote
And TTestStruct is declared inside the TestLibrary namespace as such:

// TestStruct
class DELPHICLASS TTestStruct;
class PASCALIMPLEMENTATION TTestStruct : public TObject
{
// Allow the use of "inherited" in our class as Delphi allows
typedef TObject inherited;

private:
AnsiString fNewField;
public:
virtual void __fastcall Assign(Classes::TPersistent* iSource);

__published:
__property AnsiString NewField = {read=fNewField, write=fNewField};
public:
#pragma option push -w-inl
inline __fastcall virtual TTestStruct(): TObject() {}
#pragma option pop
#pragma option push -w-inl
inline __fastcall virtual ~TTestStruct() {}
#pragma option pop
};
Do not implement constructor and destructor inline. Move their
implenmentation to separate cpp file. That would probably reslove your
problems.
BTW inline is not needed at all because any method implemented inside
class declaration is assumed inline.
 

Re:For the compiler/linker to keep a class.

Darko Miletic wrote:
Quote
OBones wrote:
>And TTestStruct is declared inside the TestLibrary namespace as such:
>
>// TestStruct
>class DELPHICLASS TTestStruct;
>class PASCALIMPLEMENTATION TTestStruct : public TObject
>{
>// Allow the use of "inherited" in our class as Delphi allows
>typedef TObject inherited;
>
>private:
>AnsiString fNewField;
>public:
>virtual void __fastcall Assign(Classes::TPersistent* iSource);
>
>__published:
>__property AnsiString NewField = {read=fNewField, write=fNewField};
>public:
>#pragma option push -w-inl
>inline __fastcall virtual TTestStruct(): TObject() {}
>#pragma option pop
>#pragma option push -w-inl
>inline __fastcall virtual ~TTestStruct() {}
>#pragma option pop
>};

Do not implement constructor and destructor inline. Move their
implenmentation to separate cpp file. That would probably reslove your
problems.
Does not seem to help.
I declared this
public:
__fastcall virtual TestStruct();
__fastcall virtual ~TestStruct();
and defined this
__fastcall TestStruct::TestStruct(): Urotypes::TROComplexType()
{
}
__fastcall TestStruct::~TestStruct()
{
}
but it did not change a thing.
Quote
BTW inline is not needed at all because any method implemented inside
class declaration is assumed inline.
Ah yes, good point.
 

{smallsort}

Re:For the compiler/linker to keep a class.

You do not need PASCALIMPLEMENTATION in class definition because that
means class is implemented in pascal.
You just need DELPHICLASS and you should implement all methods in
separate cpp. Than it should work fine.
 

Re:For the compiler/linker to keep a class.

Darko Miletic wrote:
Quote
You do not need PASCALIMPLEMENTATION in class definition because that
means class is implemented in pascal.

You just need DELPHICLASS and you should implement all methods in
separate cpp. Than it should work fine.
Yes, that's exactly it. Thanks a lot, it's ok now.