Page 1 of 1

userlib on mac and linux

Posted: Tue Sep 18, 2018 7:41 pm
by 5mware
hi guys,

i need a simple and clear description, maybe a small test-code, on how to create in c-language an user-lib for mac and linux.

how can i make this here to a userlib:

Code: Select all

int getInt()
{
    return 25;
}
int getAdd( int a, int b )
{
    return a + b;
}
I want to test in in PB like this simple way:

Code: Select all

    Debug getInt()
    Debug getAdd( 8, 12 )
please don't tell me from the SDK-Stuff in pb-directory. there isn't the specific files on mac or linux available. i am using pb 5.60 on both operating systems.

Re: userlib on mac and linux

Posted: Thu Sep 20, 2018 4:01 pm
by 5mware
hi guys,

did i asked something impossible?

)-:

Re: userlib on mac and linux

Posted: Thu Sep 20, 2018 4:14 pm
by NicTheQuick
Just tried it by myself the first time. It's really simple.
Create these two files:
library.h

Code: Select all

extern int getInt();

extern int getAdd(int a, int b);
library.c

Code: Select all

#include "library.h"

int getInt() {
    return 25;
}

int getAdd(int a, int b) {
    return a + b;
}
Compile them with gcc:

Code: Select all

gcc -c -Wall -Werror -fpic library.c
You will get a file named library.o.
You can use that file directly in Purebasic with its Import command:

Code: Select all

ImportC "library.o"
	getInt.l()
	getAdd.l(a.l, b.l)
EndImport

Debug getInt()
Debug getAdd(1, 2)
The result is:
25
3
But there is one thing I am unsure about. I don't know if you have to use Import or ImportC. In the simple example above it works both ways but I don't know if that's always the case.

Re: userlib on mac and linux

Posted: Fri Sep 21, 2018 3:08 pm
by NicTheQuick
Is it working?

Re: userlib on mac and linux

Posted: Fri Sep 21, 2018 5:05 pm
by 5mware
Hi NichTheQuick,

Thank you very much for your sample. Now it's working very well.

On Linux:
gcc -c -Wall -Werror -fpic SOURCECODE.c >> Standard Output or 64Bit
gcc -m32 -c -Wall -Werror -fpic SOURCECODE.c >> 32Bit

On Mac:
gcc -dynamiclib -o LIBNAME.dylib SOURCECODE.c >> Standard Output or 64Bit
gcc -m32 -dynamiclib -o LIBNAME.dylib SOURCECODE.c >> 32Bit

I also tried g++ with .cpp-file instead of gcc, and the result is the same :-)

now i am looking for a solution to receive string-return from a c-function or give a string as a parameter to a c function.

Re: userlib on mac and linux

Posted: Fri Sep 21, 2018 7:24 pm
by ccode
Hello 5mware,

library.c

Code: Select all

#include <stdlib.h>

//wchar_t = 2 Byte Input (Directly)
char _getChar(wchar_t Eingabe[]) 
{
	return Eingabe[1];
}

const char * _getString(const char Inp[])
{
	return Inp;
}
library.h

Code: Select all

extern char _getChar(wchar_t Inp[]);

extern const char * _getString(const char Inp[]);
PureBasic:

Code: Select all

ImportC "library.obj"
  _getInt.l()
  _getAdd.l(a.l, b.l)
  _getChar(*str)
  _getString(*str)
EndImport

Debug _getInt()
Debug _getAdd(1, 2)
Debug Chr(_getChar("Hello")) ;Return 2. Char
Debug PeekS(_getString(Ascii("Hello")),-1,#PB_Ascii) ; Ascii
Debug PeekS(_getString("Hello")) ;Unicode (2 Byte / Char ) = Standard