[SOLVED] C UserLib isn't working; as static lib it's works

Just starting out? Need help? Post your questions and find answers here.
User avatar
IceSoft
Addict
Addict
Posts: 1699
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

[SOLVED] C UserLib isn't working; as static lib it's works

Post by IceSoft »

What I want is using my C UserLib but I got a weird error message:
Using the C UserLib alone I get this error:
Interesting is the _ in front of PB_...
POLINK: error: Unresolved external symbol '_PB_FloatReturn'.

Code: Select all

Debug FloatReturn()
So I create a Static lib for checking the mistake and I get another weird error message:
Using ImportC and the C UserLib together I get this error:
Line 3: Invalid name: same as a command (from library FloatReturn').

Code: Select all

ImportC ".\FloatReturn.lib"
  FloatReturn.f() As "?PB_FloatReturn@@YAMXZ"; (float __cdecl PB_FloatReturn(void))
EndImport
Debug FloatReturn()
Using ImportC and the C UserLib together but renamed FloatReturn to FloatReturn1 => StaticLib part is working:

Code: Select all

ImportC ".\FloatReturn.lib"
  FloatReturn1.f() As "?PB_FloatReturn@@YAMXZ"; (float __cdecl PB_FloatReturn(void))
EndImport
Debug FloatReturn1()
Debug Output: 15.21000003814697
@Fred,
What is wrong on the C UserLib stuff?
Weird is: PB is searching a _PB_FloatReturn (_ in front of PB) but using as static lib too I got the error the name is the same as a PB command
My assumption: Maybe the LibraryMaker.exe 5.30 is not updated to new stuff on PB5.60?

Here are some more Info from the C UserLib:
Desc:

Code: Select all

; Language used to code the library: ASM or C
C
; Number of windows DLL than the library need
0
; Library type (Can be OBJ or LIB)
LIB
; Number of PureBasic library needed by the library
0
; Help directory name
FloatReturn
;
FloatReturn, () - float  PB_FloatReturn()
Float
Here the C Source:

Code: Select all

float PB_FloatReturn()
{
	return 15.21;
}
Last edited by IceSoft on Wed Apr 12, 2017 10:32 am, edited 2 times in total.
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
IceSoft
Addict
Addict
Posts: 1699
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: @Fred: C UserLib is not working but as static lib it's w

Post by IceSoft »

Here an easier example (no static lib is used):
Add the C userLib to: C:\Program Files (x86)\PureBasic\PureLibraries\UserLibraries

Code: Select all

Procedure.f FloatReturn1()
  ProcedureReturn 15.21
EndProcedure
Debug FloatReturn()
Raised PureBasic Linker error:
POLINK: error: Unresolved external symbol '_PB_FloatReturn'.

Code: Select all

Procedure.f FloatReturn()
  ProcedureReturn 15.21
EndProcedure
Debug FloatReturn()
Raised PureBasic error:
Line 2: Invalid name: same as a command (from library 'FloatReturn').
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: @Fred: C UserLib is not working but as static lib it's w

Post by infratec »

Not a big help, but ...

if you want to create a lib with PB you need ProcedureDLL or ProcedureCDLL instead of Procedure.

Bernd
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: @Fred: C UserLib is not working but as static lib it's w

Post by freak »

You are using a C++ compiler. Either switch to a C compiler or declare your function as extern "C".
quidquid Latine dictum sit altum videtur
GJ-68
User
User
Posts: 32
Joined: Sun Jun 23, 2013 1:00 pm
Location: France (68)

Re: @Fred: C UserLib is not working but as static lib it's w

Post by GJ-68 »

The problem with your UserLib is the calling convention.

_PB_FloatReturn is __cdecl calling convention
PB_FloatReturn@0 is __stdcall calling convention

I'm using this:

Code: Select all

#ifdef _M_IX86
	#define FNCALL __stdcall
	#pragma message("Compiling for x86")
#else // X64 doesn't have several calling convetion
	#define FNCALL
	#pragma message("Compiling for x64")
#endif

float FNCALL PB_FloatReturn()
{
	return 15.21;
}
User avatar
IceSoft
Addict
Addict
Posts: 1699
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: @Fred: C UserLib is not working but as static lib it's w

Post by IceSoft »

freak wrote:You are using a C++ compiler. Either switch to a C compiler or declare your function as extern "C".
@Fred,
Thanks! Your hint was the solution!

Some more hint:
Also the __stdcall stuff has also be part of the C source. If you don't add this on your source you get maybe an stack overflow (I get it on my performance testers)

Here the right way now (hope that's helping somebody):

Code: Select all

extern "C" float __stdcall PB_FloatReturn()
{
	return 15.21;
}
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Post Reply