How to load .SO ? lazarus / 1-Linux ---------------------------------------------------------------------- levibyte · 2010-01-07 00:35 Hi everybody. I Have cpp code like Code: [Select]int sum(int a, int b)  {   int c;   c = a + b;   return c;  } I compile this code as a shared library (libmytest.so). How can I load this dynamic library from Lazarus and call "sum" function. Thank you. ---------------------------------------------------------------------- alter · 2010-01-07 01:08 Quote from: levibyte on January 07, 2010, 01:35:52 amHi everybody. I Have cpp code like Code: [Select]int sum(int a, int b)  {   int c;   c = a + b;   return c;  } I compile this code as a shared library (libmytest.so). How can I load this dynamic library from Lazarus and call "sum" function. Thank you. function Sum(a, b: integer): integer; cdecl; external SO_NAME; ---------------------------------------------------------------------- levibyte · 2010-01-07 01:32 Thank you for your reply. Actually that doesn't work :[ I declare external function and it is ok. (compiled normaly). But when I try to use this function in code ( sum(10,20); ) I get error ( such as "undefined sum" ). What is the problem? ---------------------------------------------------------------------- alter · 2010-01-07 03:37 Quote from: levibyte on January 07, 2010, 02:32:31 amThank you for your reply. Actually that doesn't work :[ I declare external function and it is ok. (compiled normaly). But when I try to use this function in code ( sum(10,20); ) I get error ( such as "undefined sum" ). What is the problem? You mean it doesn't compile? In that case declaration above is not visible from procedure where you are calling sum. If you are putting this declaration in external unit then be sure that you have put the declaration between interface and implementation section. Also be sure to put unit with declaration in uses section (either the one in interface or in implementation) of unit where you are calling function from the library. ---------------------------------------------------------------------- gtom · 2010-01-07 07:12 Well, SUM is IN your Library... Lazarus knows nothing about SUM... if you want to use SUM in your application you have to declare {Link sum.so}  // tell the compiler to load the library function sum(a,b: integer): integer; cdecl; (optional: name, lib) external; // tell the compiler where this func is declared Then lazarus needs to know from WHERE the library is located... -Fl your-path-to-the-library If not lazarus compiles but cannont link the file... ---------------------------------------------------------------------- levibyte · 2010-01-07 09:09 after> Well , I mean it stops on Linking... Code: [Select]Linking loadlib.pas loadlib.o(.text+0xd): in function 'main' :undefined reference to 'sum' ---------------------------------------------------------------------- levibyte · 2010-01-07 09:14 my pascal code Code: [Select]{$link /usr/lib/libtest.so} function sum(a:integer ; b:integer):integer; cdecl; external 'libtest.so'; begin  writeln(sum(10,20)); end. ---------------------------------------------------------------------- theo · 2010-01-07 09:21 Please read the docs: http://www.freepascal.org/docs-html/prog/progsu43.html#x50-480001.1.43 ---------------------------------------------------------------------- marcov · 2010-01-07 20:21 cpp code, so while the code looks C, it is C++. Solution: declare function with extern C { } ---------------------------------------------------------------------- levibyte · 2010-01-08 17:44 Theo> Thank you. That works for libc , I used strcpy from this lib in pacal. But when I am trying to use functions from my own library linker stops.  The way I create shared library Code: [Select]g++ mytest.cpp -o libtest.so -shared help me please :[ ---------------------------------------------------------------------- marcov · 2010-01-09 20:24 Quote from: levibyte on January 08, 2010, 06:44:10 pmTheo> Thank you. That works for libc , I used strcpy from this lib in pacal. But when I am trying to use functions from my own library linker stops.  The way I create shared library Code: [Select]g++ mytest.cpp -o libtest.so -shared help me please :[ See above quote. C++ mangling is compiler and version specific, so this will not work. You need to declare your functions C compatible on the C/C++ side (extern C { } ) ---------------------------------------------------------------------- levibyte · 2010-02-21 22:59 cooooool , that works =] ( with extern "C" {} ) Thank you.