simple static library in Linux and OSX using gcc

Just starting out? Need help? Post your questions and find answers here.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

simple static library in Linux and OSX using gcc

Post by Keya »

See also:
* Create and use masm/fasm lib: http://www.purebasic.fr/english/viewtop ... 4&p=489136
* Create and use Visual Studio lib: http://www.purebasic.fr/english/viewtop ... 9&p=486947

im using 5.42LTS-x64 and very recent Linux Mint 64

1. Create mylib.c

Code: Select all

double addnums(double num1, double num2) {
      return num1 + num2;
}
2. Compile the C source to object (mylib.c -> mylib.o):

Code: Select all

gcc -c -o mylib.o mylib.c -m32/-m64

3. Compile the object to library (mylib.o -> mylib.a) - not really needed unless you want to bundle multiple .o's into a .a:

Code: Select all

ar rcs mylib.a mylib.o
4. Call the mylib.a library from Purebasic:

Code: Select all

ImportC "mylib.a"  ;ImportC not Import
     addnums.d (value1.d, value2.d) As "addnums"
EndImport

MessageRequester("Result", StrD(addnums(5,3)) )
-----

NOTE: The rest of this thread can essentially be disregarded, as it's about a problem i initially had due when i first posted using reserved word "add" for my procedure name which i renamed to "addnums" to fix - thanks to jack for isolating that issue :)
Last edited by Keya on Fri Feb 10, 2017 1:12 am, edited 15 times in total.
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: simple lib in Linux!?

Post by jack »

have you successfully compiled pb programs in mint?
did you run the check install.sh script to see if you are missing anything?
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: simple lib in Linux!?

Post by Keya »

yes i can compile and execute for example Messagebox("Test","hello world") and others fine, and also theyre all local/nonnetworked files, and checkinstall.sh reports "Everything seems correctly setup for PureBasic !"
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: simple lib in Linux!?

Post by jack »

I just tried your example and have the same problem, tried by placing the lib in the pb libraries/linux folder and also in /usr/local/lib but no go.
as for os x, I have no problems
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: simple lib in Linux!?

Post by Keya »

thankyou very much for trying :) hopefully someone else can chime in with some insight about Linux!
so that exact above code works fine on OSX? nice!! :)
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: simple lib in Linux!?

Post by jack »

Keya
try without "As add"

Code: Select all

ImportC "mylib.a"
     add.d (value1.d, value2.d)
EndImport
can't test myself because am getting "compiler isn't loaded yet..." no matter how many times I close and open the IDE
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: simple lib in Linux!?

Post by Keya »

with As "blah" i get "undefined reference to 'blah'", and "Linker error!"
with As "add" i dont get any linker errors but just "Assembler error!"
and with no As statement i get the same Assembler error as if id used As "add"
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: simple lib in Linux!?

Post by jack »

keya, the problem is the keyword add
purebasic.asm [11]:
extrn add
error: reserved word used as symbol.
Error: Assembler
rename the function add to something else.
I tried

Code: Select all

ImportC "/usr/local/lib/mylib.a"
     add1.d (value1.d, value2.d) As "add" ;<- note as add1
EndImport

MessageRequester("Result", StrD(add1(5,2)) )
.
[edited, stupid mistake due to lack of sleep]
for some reason when compiling in linux you can't use names that are assembler keywords.
Last edited by jack on Mon May 16, 2016 6:20 pm, edited 1 time in total.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: simple lib in Linux!?

Post by Keya »

ahhh good catch! :) it was probably bad form of me to use 'add' as the function name when i already knew it was an asm instruction, lol - only a few weeks ago someone else had a similar issue with 'crc32'!
Thankyou very much jack! Now i know how to make and call simple libs on Windows, Linux and OSX, which i think is a really cool and programmer-empowering feature of Purebasic! Thankyou Fred!!! another door opens after another good days learning :)
Post Reply