userlib on mac and linux

Everything else that doesn't fall into one of the other PB categories.
5mware
User
User
Posts: 30
Joined: Mon Dec 14, 2015 2:14 pm

userlib on mac and linux

Post 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.
5mware
User
User
Posts: 30
Joined: Mon Dec 14, 2015 2:14 pm

Re: userlib on mac and linux

Post by 5mware »

hi guys,

did i asked something impossible?

)-:
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: userlib on mac and linux

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: userlib on mac and linux

Post by NicTheQuick »

Is it working?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
5mware
User
User
Posts: 30
Joined: Mon Dec 14, 2015 2:14 pm

Re: userlib on mac and linux

Post 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.
ccode
User
User
Posts: 99
Joined: Sat Jun 23, 2018 5:21 pm

Re: userlib on mac and linux

Post 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
Post Reply