How to make correct .so library?

Started by Tentaclius, August 16, 2006, 03:21:36 AM

Previous topic - Next topic

Tentaclius

Please tell, how should i compile/link library for use it within newlisp (in Linux)?

Or what is C++ keyword for sharing function?



With GNU Assembler it works this way:

test.s:

.section .text
.global myfun

myfun:
...

than after gcc -c test.s -o test.o; ld test.o -shared

test.lsp:

(import "./test.so" "myfun")
...


all works fine but how to do the same with C++-function?

Lutz

#1
I think for linking C++ objects you can use the the same -shared keyword as for C programs. See the documentation here: http://newlisp.org/DesignPatterns.html">http://newlisp.org/DesignPatterns.html in chapter 19.



C++ preprocesses the function names used in programs, so they are not the same in the object code, there is a new name for each C++ call pattern of a function. You would have to use other tools to find out, i.e. the assembly output of your compile.



Lutz

pjot

#2
With C++ you will have a problem with the so-called 'name-mangling'. In C-classes, you can have multiple methods with the same name. This technique is called 'overloading'. However, the resulting binary must have unique addresses to jump to, and therefore the C++ compiler will create unique names during compiletime for these duplicate methods. These names can be different each time you recompile.



Therefore, the 'dlopen' function to import C functions from a Shared Object or DLL cannot be used when these objects are created with C++.



There is an interesting HOWTO which explains it all:



http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html">http://www.isotton.com/howtos/C++-dlope ... HOWTO.html">http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html





As you can see, the sourcecode of C++ programs must be adjusted to get things working.



For the GTK-server I also get many requests to port the thing to Qt or FLTK, but as these toolkits are written in C++, I never can reach them, unless I start changing the sourcecode of these toolkits :-(



Peter