C [Solved] how to make a shared library on freebsd? i'm getting errors.

Hi. I'm using the "normal commands" and getting message "i cannot relocate object of this function in the .o, you need to enable -fPIC or use -Wl,-x,notext" when you make the object.

#1 when I make the object using cc -c, cc uses -d, so i "assume" it should be: "cc a.c -o a.o -Wl,-f,PIC" is that right?

#1.1 I AM NOT PORTING LINUX I AM MAKING A freeBSD native app (I do not want libtool advice) I know since 4.3BSD it's been possible to make .so on freeBSD (up to the switch from gcc to clang, and it was not the same as for linux, but worked).

#2 i've tried every combination of "old rule of thumb" commands all of the result in the same message

#3 I cannot find any documentation for shared lib making for freebsd by google. but the scripts are "pretty hairy" i am looking for a few magic commands (the easy ones not some jargon of selecting readonly reloc per symbol or calculating depends)

#4 i understand (barely) that one has an option of (including and using) /use/shared/mk/bsd.lib.mk or something and took a look at it. i'm not interested in a ubuntu-style robot refusing to work for me base on "not naming my lib properly". right now i just want "foo.so", period. i understand freebsd isn't linux and i may have to learn more, or maybe not. I just want the linker options free and clear of macro jargon and macro "you can't do that on my system" refusals.
 
How do I make a .so from a .o? what are the options for CC cc -c and for LINK cc -o literally?

Hi. I'm using the "normal commands to make a .so" (so i think). I'm getting message "I cannot relocate object of this function in the .o, you need to enable -fPIC or use -Wl,-x,notext" when you make the .o so i can read it into the shared library". This is for a freeBSD native app that does not intend to be portable. i can't google any freebsd instructions on foo.so lib making. I of course tried every thinkable permuation of the advice: but get the same message. Also - I'm not getting any message about symbols from other .so: the symbols in question are all defined in the .o. I'd rather not use mk/lib.bsd.mk - i just want "simple as possible", just the literal clang cc/ld/vv options.
 
Should be exactly the same as on Linux:

$ cc -fPIC -ofoo.so -shared foo.c

-fPIC to enable position independent code
-o to specify the output file name
-shared to specify creating a shared library

This is using the compiler to compile and link it. You can do similar with the linker (ld) directly and previously compiled object files.
 
Use the source Luke.

/usr/src/share/examples/libifconfig/Makefile

Code:
    $(CC) -Wall -fPIC -lifconfig -g -o example_setdescription setdescription.c
 
Thank you all,

I did solve it. LLVM ld.lld does not like the "two step" (make .o, link) of past gcc make scripts (specifically, the X11 build imake scripts). It does likes a single step (cc foo.c -o foo -shared -fPIC ....) and works easily that way.

I cannot cut and paste the full working command right now, not that you need it.
 
Thank you all,

I did solve it. LLVM ld.lld does not like the "two step" (make .o, link) of past gcc make scripts (specifically, the X11 build imake scripts). It does likes a single step (cc foo.c -o foo -shared -fPIC ....) and works easily that way.

I cannot cut and paste the full working command right now, not that you need it.
I don't think this is true. Recalling from my past of using CodeBlocks on Windows with both GCC and Clang, as CodeBlocks' project templates use exactly the same settings for both GCC and Clang with exactly this kind of two step linking and it never failed me a single time. Only with Clang 10 and later I have to edit the Clang project template to remove no longer supported flag (GCC still supports, though) to make my DLL built. And I think it's nothing different on other platforms.
 
Back
Top