Import C++ Static Library

Everything else that doesn't fall into one of the other PB categories.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Import C++ Static Library

Post by Shield »

Good Morning :)


I've been writing a static library with C++ (GCC) which I wanted to import in PB.
Works fine in general, but as soon as I use "new" (or more specific, as soon as I want to create an object inside the library),
PB's linker tells me this:

Code: Select all

POLINK: error: Unresolved external symbol '__ZdlPv'.
POLINK: error: Unresolved external symbol '__Znwj'.
Does anybody have any idea how to solve this problem?

I already tried using VC++ since PB links those libraries,
but I couldn't get it to work at all with VC++. The PB compiler got stuck
while linking, with the polink.exe process allocating more and more memory
up to inifinity... -_-


I'd appreaciate any help :) Thanks.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Import C++ Static Library

Post by Danilo »

Shield wrote:Works fine in general, but as soon as I use "new" (or more specific, as soon as I want to create an object inside the library),
PB's linker tells me this:

Code: Select all

POLINK: error: Unresolved external symbol '__ZdlPv'.
POLINK: error: Unresolved external symbol '__Znwj'.
Does anybody have any idea how to solve this problem?
Try with your own new and delete operators.

Code: Select all

void * operator new   (size_t size);
void   operator delete(void *rawMemory, size_t size);

void * operator new(size_t size) {
    if (size == 0) size = 1;
    return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
}

void   operator delete(void *rawMemory,size_t size) {
    if (rawMemory) HeapFree(GetProcessHeap(),0,rawMemory);
}
Try with this. If it works, you have to make it more nice (throw exception if alloc fails etc..).

You can set new/delete globally like above. In classes they need to be static.

Code: Select all

class dcsFont : public DCS::dcsObject {
      public:
         // [...]
         static void * operator new   (size_t size);
         static void   operator delete(void *rawMemory, size_t size);
      private:
         // [...]
};

void * DCS::dcsFont::operator new(size_t size) {
    if (size == 0) size = 1;
    return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
}

void   DCS::dcsFont::operator delete(void *rawMemory,size_t size) {
    if (rawMemory) HeapFree(GetProcessHeap(),0,rawMemory);
}
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Import C++ Static Library

Post by Shield »

Tried that before and didn't work...yours does though, no idea what I did wrong. Thanks. :)
This fixed the __Znwj error, but the __ZdlPv still remains and pops up as soon as I create STL
container objects. Doesn't matter if I create them with 'new' or on the stack.
Danilo wrote:If it works, you have to make it more nice (throw exception if alloc fails etc..).
Doesn't this also lead to link errors? Had to disable exception handling in the compiler settings to get rid of them.

What I also tried is using malloc / HeapAlloc to allocate space for the object and then create it
using Foo* bar = new(pointer) std::somecontainer();

That raised the same error message, though. :|
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Import C++ Static Library

Post by Danilo »

Unmangle __ZdlPv to see what it is. I don't know that GCC stuff.

When using C++ with PB there are more problems, for example
initializing the runtime with WinMainCRTStartup()/wWinMainCRTStartup()
or mainCRTStartup()/wmainCRTStartup().
If you don't init you could get "C Run-Time Error R6030" (says MSDN).
We made some tests with this in the german forum but did not work well.

You could try with the c runtime init function for DLLs, see MSDN.
Maybe that does not change the program flow like the functions above.

Also you have to link to msvcrt.lib (DLL import) or libcmt.lib (static version)
for MS VisualC++. Maybe msvcprt.lib too for C++ stuff.

Don't know, sorry. I just don't mix that 2 anymore. :)
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Import C++ Static Library

Post by Shield »

Danilo wrote:Unmangle __ZdlPv to see what it is. I don't know that GCC stuff.
Me neither. Wanted to use VC++ but as I said...polink just gets stuck with a memory leak.
Can't even use the IDE's abort button to abort compilation, instead have to manually kill it with the task manager.

Just wanted to create some memory handling functions I need every once in a while for quick and dirty tests
which I usually do with PB but can't be arsed to write those functions in PB because of missing features in pointer arithmetics.

Anyway, thanks for your help. I guess I'll just create a DLL and use that one. :wink:
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Post Reply