Page 1 of 1

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

Posted: Tue Apr 11, 2017 5:15 pm
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;
}

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

Posted: Tue Apr 11, 2017 6:43 pm
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').

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

Posted: Tue Apr 11, 2017 7:11 pm
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

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

Posted: Tue Apr 11, 2017 7:28 pm
by freak
You are using a C++ compiler. Either switch to a C compiler or declare your function as extern "C".

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

Posted: Tue Apr 11, 2017 7:39 pm
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;
}

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

Posted: Tue Apr 11, 2017 9:46 pm
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;
}