Using C++ libraries in FreeBasic

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

Using C++ libraries in FreeBasic

Post by angros47 »

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

Post by anonymous1337 »

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: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

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

Post by angros47 »

@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

Post by Merick »

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: 2324
Joined: Jun 21, 2005 19:04

Post by angros47 »

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: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

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:

Post by duke4e »

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

Post by stylin »

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

Post by 4quiles »

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

Post by anonymous1337 »

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

Post by angros47 »

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

Post by 4quiles »

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: 2324
Joined: Jun 21, 2005 19:04

Post by angros47 »

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

Post by dani.user »

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