Using C++ libraries in FreeBasic

General FreeBASIC programming questions.
angros47
Posts: 1632
Joined: Jun 21, 2005 19:04

Using C++ libraries in FreeBasic

Postby angros47 » Mar 13, 2010 19:50

I've created a library in C++ (Mingw), and exported some functions using extern "C". I then tried to call it from a FreeBasic program (linking it), and I got theese errors:


Code: Select all

.//liblib.a(lib.o):lib.cpp:(.text+0x1e): undefined reference to `std::cout'
.//liblib.a(lib.o):lib.cpp:(.text+0x23): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
.//liblib.a(lib.o):lib.cpp:(.text+0x2b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'

...and many others

I tried to copy the stdc++.a, but I got more errors.
I know that FreeBasic can link libraries in the same way as GCC: on C forums, I found no help (the only suggestion was to use G++ instead of GCC, to link all files, or to use a makefile, but this is not a good solution, since I need to use fbc)

Can anybody help me? How can I link a library compiled with C++? Can I "pre-link" it and the C++ run-time, to have a single .a file? If so, how?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Mar 13, 2010 20:25

Try using #include "crt.bi" (C runtime) at the top of your program.
Last edited by anonymous1337 on Mar 13, 2010 20:26, edited 1 time in total.
srvaldez
Posts: 2458
Joined: Sep 25, 2005 21:54

Postby srvaldez » Mar 13, 2010 20:25

I am no expert at all, just curious, did you compile your lib with g++?
angros47
Posts: 1632
Joined: Jun 21, 2005 19:04

Postby angros47 » Mar 13, 2010 23:57

@Printchard:

Tested it, it doesn't work. The problem is, I need some routines that are part of C++ standard library (it should be iostream), and aren't part of FreeBasic. Can they be linked in my library? Or there is another solution?

@srvaldez

Yes, I compiled the library with g++, and made it with ar cr
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Postby Merick » Mar 14, 2010 0:27

Were you trying to link stdc++.a into your lib or into the FB code? If the missing function are needed only by your lib's internal functions, then linking stdc++.a into the lib itself when compiling it with mingw should be enough.
angros47
Posts: 1632
Joined: Jun 21, 2005 19:04

Postby angros47 » Mar 14, 2010 0:43

Merick wrote: If the missing function are needed only by your lib's internal functions, then linking stdc++.a into the lib itself when compiling it with mingw should be enough.


That's exactly what I'd like to do; but how can I do it? the linker allows to create a .a library from many .o files, but not from another .a library.

Can you help me?
srvaldez
Posts: 2458
Joined: Sep 25, 2005 21:54

Postby srvaldez » Mar 14, 2010 2:25

perhaps this link will help http://www.trilithium.com/johan/2005/06/static-libstdc/ I would be interested if you solve it, had exact same problem with a library I was trying to compile and gave up.
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Postby duke4e » Mar 14, 2010 4:09

@Printchard
lol
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Mar 14, 2010 4:13

agros47 wrote:the linker allows to create a .a library from many .o files, but not from another .a library.


For what it's worth, ar is used to manipulate archives. Try something like

ar -rcs libmylib.a libstdc++.a

and see it's documentation for more details. Hope this helps.
4quiles
Posts: 56
Joined: Jul 19, 2009 3:19

Postby 4quiles » Mar 14, 2010 5:45

Sometimes I use C++ libs in Freebasic dll's. The C++ lib usually use some STL stuff, like list or maps. Try doing the following:

Create all the C++ functions with the extern "C" (and of course use only parameters that are recognized by FB).
Copy libstdc++.a from the mingw lib path into the Freebasic lib path
Finally declare the functions in some .bi file:

Code: Select all

#Ifndef __MYFILE_BI__
#define __MYFILE_BI__

#Inclib "stdc++"
#Inclib "MyCppLib"

Extern "Windows-MS"

Declare Function SomeCppFunction(...) as Integer
Declare Function ...

End Extern

#EndIf  ' __MYFILE_BI__


anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Mar 14, 2010 6:03

duke4e wrote:@Printchard
lol
Sorry :( I don't have FB installed so I (probably shouldn't have) took a guess.
angros47
Posts: 1632
Joined: Jun 21, 2005 19:04

Postby angros47 » Mar 14, 2010 9:47

4quiles wrote:Sometimes I use C++ libs in Freebasic dll's. The C++ lib usually use some STL stuff, like list or maps. Try doing the following:

Create all the C++ functions with the extern "C" (and of course use only parameters that are recognized by FB).
Copy libstdc++.a from the mingw lib path into the Freebasic lib path
Finally declare the functions in some .bi file:



Tried it, I got a lot of errors:

Code: Select all

.//libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x12): undefined reference to `__cmshared_unexpected'
.//libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x44): undefined reference to `__cmshared_terminate'
.//libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x68): undefined reference to `__cmshared_terminate'
.//libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x84): undefined reference to `__cmshared_unexpected'
.//libstdc++.a(eh_terminate.o):eh_terminate.cc:(.text+0x8b): undefined reference to `__cmshared_terminate'
.//libstdc++.a(eh_catch.o):eh_catch.cc:(.text+0x127): undefined reference to `_Unwind_Resume'
.//libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x6): undefined reference to `__cmsharedptr'
.//libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x70): undefined reference to`__cmsharedptr'
.//libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0xc5): undefined reference to`__cmsharedptr'
.//libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x111): undefined reference to `__cmsharedptr'
.//libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x12d): undefined reference to `__cmsharedptr'
.//libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x152): undefined reference to `_Unwind_Resume'
.//libstdc++.a(eh_globals.o):eh_globals.cc:(.text+0x16e): undefined reference to `__cmsharedptr'
4quiles
Posts: 56
Joined: Jul 19, 2009 3:19

Postby 4quiles » Mar 14, 2010 19:42

This test is a small library made in Dev C++ using some STL and then is used on Freebasic. The library does not do anything useful... just an example.
Maybe you can compare and see what is not working.

http://www.sendspace.com/file/1i4yp6

P.D: Someone knows an upload site without all that annoying publicity? :)
angros47
Posts: 1632
Joined: Jun 21, 2005 19:04

Postby angros47 » Mar 14, 2010 20:11

Tested it: maybe the problem is that I have Mingw 4.3.3, and the example is for version 3?

If I use the library included in the sample, it works, if I try to rebuild it, it doesn't work.

Edit: if I build the library with the given object file, it work: if I recompile c++ code into a new .o file, it doesn't
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Postby dani.user » Mar 14, 2010 20:33

Compile the library as a .dll and make sure the functions you export for fb don't return or ask for variabiles from stdc++

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests