Creating PB Userlibraries in C++
Posted: Fri Nov 25, 2005 2:30 pm
This subject is so old, but still active. Despite my many attempts, I just fail finding a good and clear tutorial on how to extend PureBasic with C++ code. Of course I first used the forum search, but found only one topic that could provide actual help:
viewtopic.php?t=15150
Unfortunately, I still have problems I can't overcome. I'm using VC++, and try to make a simple userlib to see if it's actually possible. The code is based upon an example found at the link above.
So I naturally started an empty (simple) dynamic DLL-project, and write this:
PureTest.cpp
Nothing unusual, I successfully create a dll. Also, I double-checked that this DLL works in PB with the OpenLibrary()-command.
Then I open the LibraryMaker.exe, and provide it a Desc-file as follows:
PureTest.Desc
It goes through, no error messages. I restart PB compiler, and SampleCFunction is now recognized as PB-command. Then I try to execute this code:
PureTest.pb
If debugger enabled, I get:
PureBasic - Linker Error
POLINK: warning: Multiple '.data' sections found with different flags (0xc0000040 and 0x60000040).
POLINK: error: Unresolved external symbol '_PB_SampleCFunction_DEBUG'.
POLINK: error: Unresolved external symbol '_PB_SampleCFunction'.
POLINK: fatal error: 2 unresolved external(s).
If debugger disabled, I get:
PureBasic - Linker Error
POLINK: warning: Multiple '.data' sections found with different flags (0xc0000040 and 0x60000040).
POLINK: error: Unresolved external symbol '_PB_SampleCFunction'.
POLINK: fatal error:1 unresolved external(s).
I've tried to solve this for several hours now, and I'm falling into despair. Since there are userlib-writers here any help would be appriciated.
viewtopic.php?t=15150
Unfortunately, I still have problems I can't overcome. I'm using VC++, and try to make a simple userlib to see if it's actually possible. The code is based upon an example found at the link above.
So I naturally started an empty (simple) dynamic DLL-project, and write this:
PureTest.cpp
Code: Select all
// PureTest.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
#include <windows.h>
//////////////////////////////////////////////////////////////////////////////
// Step (1) Include PB's debugger stuff.
// With SendDebuggerError() the debugger can be stopped with some
// warning message. Useful in "_DEBUG" functions.
//////////////////////////////////////////////////////////////////////////////
char *PB_DEBUGGER_ErrorDescription;
HWND PB_DEBUGGER_Window;
int PB_DEBUGGER_Control;
void SendDebuggerError(char*);
void SendDebuggerError(char *text)
{
if (PB_DEBUGGER_ErrorDescription == 0)
{
PB_DEBUGGER_ErrorDescription = text;
PB_DEBUGGER_Control = 2; // Halt the PureBasic program flow...
SendMessage(PB_DEBUGGER_Window, WM_COMMAND, 6, 1);
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Step (2) Write the C functions you want to export to PureBasic.
// These functions must be prefixed with "PB_" for PureBasic to
// recognize them.
//
// Note: Functions have to use STDCALL calling convention.
//////////////////////////////////////////////////////////////////////////////
#define PBCOMMAND extern
PBCOMMAND int _stdcall PB_SampleCFunction();
PBCOMMAND int _stdcall PB_SampleCFunction()
{
return 1234;
}
//////////////////////////////////////////////////////////////////////////////
// Step (3) Write the C debug functions. They must be suffixed with "_DEBUG".
// This function for example will raise an error in PB's debugger,
// if one of the three arguments are NULL (0).
//
// Note: Functions have to use CDECL calling convention and must use void
// as return value.
//////////////////////////////////////////////////////////////////////////////
#define PBDEBUG extern void
PBDEBUG PB_SampleCFunction_DEBUG();
PBDEBUG PB_SampleCFunction_DEBUG()
{
}Then I open the LibraryMaker.exe, and provide it a Desc-file as follows:
PureTest.Desc
Code: Select all
;
; Langage used to code the library: ASM or C
C
;
; Number of windows DLLs our library needs.
; This is used to tell the PureBasic compiler /
; linker which libraries are needed to link with.
1
MSVCRT
;
; Library type (Can be OBJ or LIB).
OBJ
;
; Number of PureBasic libraries needed by your library.
; (additional PureBasic libraries that are needed for linking).
0
;
; Help directory name.
SampleLib
;
; Library functions (FunctionName, Arg1, Arg2, ...);
; First the function name. Its the name of the C function
; that should be exported to PB, without the "PB_" Prefix
; used in C. After function name a comma and a list of
; arguments seperated by comma aswell. in parentheses
; a argument description that is showed by the PureBasic IDE
; as quick help. After the parentheses a general description
; can follow.
;
; Translation table for some arguments:
; ANSI C | PureBasic
; --------------------------
; int * | Long
; byte * | Long
; int (32 bits) | Long
; char * | Long / String
; long (32 bits) | Long
; float | Float
; double | - (must be casted to float in C for now)
; char | Byte
; void | None
; short(16 bits) | Word
;
SampleCFunction () - My Sample Function.
;
; Return value:
; Type of return value and flags for:
; 'InitFunction' (a function which is called automatically
; at the start of a PureBasic program),
; 'EndFunction' (called at the end, for cleaning)
; and 'DebuggerCheck' (a debugger handler function for this command.)
Long | DebuggerCheckPureTest.pb
Code: Select all
MessageRequester("",Str(SampleCFunction()))PureBasic - Linker Error
POLINK: warning: Multiple '.data' sections found with different flags (0xc0000040 and 0x60000040).
POLINK: error: Unresolved external symbol '_PB_SampleCFunction_DEBUG'.
POLINK: error: Unresolved external symbol '_PB_SampleCFunction'.
POLINK: fatal error: 2 unresolved external(s).
If debugger disabled, I get:
PureBasic - Linker Error
POLINK: warning: Multiple '.data' sections found with different flags (0xc0000040 and 0x60000040).
POLINK: error: Unresolved external symbol '_PB_SampleCFunction'.
POLINK: fatal error:1 unresolved external(s).
I've tried to solve this for several hours now, and I'm falling into despair. Since there are userlib-writers here any help would be appriciated.