Trying to use an FB static library in a C program

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Trying to use an FB static library in a C program

Post by xlucas »

I've been able to run functions in a C library from an FB program just fine (static), but when I try the other way around, there's something I'm doing wrong.

My FB libary has the following code:

Code: Select all

Function Duplicate(v As Short) As Short
  Return 2 * v
End Function
So I do this:

Code: Select all

fbc -lib dupli.bas
and I botain a libdupli.a file.

Now I make a C program called a.c like this:

Code: Select all

extern short DUPLICATE(short v);

#include "stdio.h"

void main() {
  printf("%d", DUPLICATE(5));
}
I've already tried with "Duplicate", but since I know FreeBasic turns everything to uppercase, I've also tried it this way, yet, when I want to compile and link everything together like this:

Code: Select all

gcc -L. -l:libdupli.a a.c
I get:

Code: Select all

/usr/bin/ld: /tmp/ccpSo8AN.o: in function `main':
a.c:(.text+0xe): undefined reference to `DUPLICATE'
collect2: error: ld returned 1 exit status
What am I doing wrong? Thanks!
oyster
Posts: 274
Joined: Oct 11, 2005 10:46

Re: Trying to use an FB static library in a C program

Post by oyster »

Code: Select all

gcc a.c -L. -l:libdupli.a 
btw, you can try any of the following code to get function "Duplicate"

Code: Select all

extern "C"
    Function Duplicate(v As Short) As Short export
        Return 2 * v
    End Function
end extern


'~ Function Duplicate alias "Duplicate"(v As Short) As Short export
    '~ Return 2 * v
'~ End Function


caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Trying to use an FB static library in a C program

Post by caseih »

Right. Short answer is name mangling. So you either put your function in an extern "c" block, or you use the CDECL keyword in your function definition and define an alias that will be used by the C code:
https://www.freebasic.net/wiki/KeyPgCdecl
https://www.freebasic.net/wiki/KeyPgFunction
oyster
Posts: 274
Joined: Oct 11, 2005 10:46

Re: Trying to use an FB static library in a C program

Post by oyster »

no, the solution has nothing to do with "name mangling", but the sequence how gcc treats source code and library

What I write on "name mangling" is for further convenient usage, but not for solving xlucas's problem
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Trying to use an FB static library in a C program

Post by caseih »

True, although FB converting everything to upper case is a form of name mangling, which alias can help with. There's also the issue calling convention, which extern "C" or CDECL address. Although I believe FB's default is CDECL. I ran xlucas' test code on my LInux machine and it linked and ran without errors as he had it (minus the extern line), so I'm not sure why he had problems there.

Just a note on the C code: declaring the function as "extern" is unnecessary in this case. All you need is a prototype. extern is used when accessing variables or objects defined in the global scope in another .o file. Normally in GCC all subs and functions in a .o file are accessible to the linker from any other .o file.

EDIT: corrected to "unnecessary."
Last edited by caseih on Jan 06, 2024 3:47, edited 1 time in total.
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: Trying to use an FB static library in a C program

Post by xlucas »

I didn't expect the solution to be so simple! And thanks for the details about the name. I had a big confusion with that.
I see that, if my function calls graphics functions, I get a lot of undefined references when I try to call it from C. Is FBGFX not compiled into the library by default? Is there a way to force it to do it?
Post Reply