Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-09

bridging an C++ shared library statically

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

Hi All,

I need to use a C++ so file in FreePascal. I have written a C wrapper for it, which successfully connected to Pascal.

Now my question is, can I use the $linklib directive to link the C-Wrapper STATICALLY, which in-turn use the shared so file? I tried to do this but it complained a lot of undefined references.

The key problem is, the {$linklib} instruction only tell FPC where to look for external functions in the C Wrapper, but how can I tell the linker to look for the ORIGINAL C++ so file to look for symbols required but not defined in the C-Wrapper?

My project is hosted at: https://github.com/xrfang/CTPas/

Thanks a lot.

Xiangrong

Reply by email

Michael Van Canneyt
to Xiangrong Fang
Xiangrong Fang wrote 6 lines
Hi All, I need to use a C++ so file in FreePascal.   I have written a C wrapper for it, which successfully connected to Pascal. Now my question is, can I use the $linklib directive to link the C-Wrapper STATICALLY, which in-turn use the shared so file?   I tried to do this but it complained a lot of undefined references.

This is normal.

The key problem is, the {$linklib} instruction only tell FPC where to look for external functions in the C Wrapper, but how can I tell the linker to look for the ORIGINAL C++ so file to look for symbols required but not defined in the C-Wrapper?
{$linklib XYZ}

can be used for any library. So you should add the C wrapper library, C++ library, and probably some others as well, such as the C++ STL.

Michael.

Reply by email

Xiangrong Fang
to Michael Van Canneyt

Ok, but when I added these:

implementation
{$linklib libthostintf.a}
{$linklib thostmduserapi.so}
end.

I got:

/usr/bin/ld: cannot find -lthostmduserapi

All the required .so and .a file are in CURRENT dir.

2013/9/21 Michael Van Canneyt

Show quoted text 36 lines
On Sat, 21 Sep 2013, Xiangrong Fang wrote: Hi All, > > I need to use a C++ so file in FreePascal. I have written a C wrapper > for it, which successfully connected to Pascal. > > Now my question is, can I use the $linklib directive to link the > C-Wrapper STATICALLY, which in-turn use the shared so file? I tried to do > this but it complained a lot of > undefined references. > This is normal. > The key problem is, the {$linklib} instruction only tell FPC where to > look for external functions in the C Wrapper, but how can I tell the linker > to look for the ORIGINAL C++ > so file to look for symbols required but not defined in the C-Wrapper? > {$linklib XYZ} can be used for any library. So you should add the C wrapper library, C++ library, and probably some others as well, such as the C++ STL. Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Michael Van Canneyt
to Xiangrong Fang
Xiangrong Fang wrote 13 lines
Ok, but when I added these: implementation {$linklib libthostintf.a} {$linklib thostmduserapi.so} end. I got: /usr/bin/ld: cannot find -lthostmduserapi All the required .so and .a file are in CURRENT dir.

1) Did you give the correct name ?
2) Is the current dir in the linker search path ? AFAIK the current dir is by default not in the linker search path.

Michael.

Reply by email

Xiangrong Fang
to Michael Van Canneyt
Show quoted text 6 lines
1) Did you give the correct name ? 2) Is the current dir in the linker search path ? AFAIK the current dir is by default not in the linker search path.

I am sure the name is correct. No matter I supply the linker path (-Fl.) or not, it does not work (i.e. ld reported cannot find -lthostmduserapi)

How ever, I managed to compile it by doing the following:

1) rename thostmduserapi.so to libthostmduserapi.so
2) add the following into implementation section:
{$linklib thostmduserapi.so}
{$linklib ThostMdIntf.a}
{$linklib stdc++}
{$linklib gcc_s}
{$linklib c}
3) Compile the project with LD_RUN_PATH=.

However, my last question is: How can I instruct FPC to look for thostmduserapi.so, NOT libthostmduserapi.so, i.e. WITHOUT the lib prefix?

I checked the ld man page, it said to put a colon prefix, to tell ld not to prepend "lib". I then did this:

{$linklib :thostmduserapi.so}

Unfortunately, ld still cannot find the library, however the error message changed:

/usr/bin/ld: cannot find thostmduserapi

while using {$linklib thostmduserapi.so}, the error was:

/usr/bin/ld: cannot find -lthostmduserapi

How to let ld look for library WITHOUT lib prefix?

Thanks!

Reply by email

Michael Schnell
to Xiangrong Fang: I need to use a C++ so file in FreePascal. I have written a C wrapper for it, which…
Xiangrong Fang wrote
Hi All, I need to use a C++ so file in FreePascal

AFAIK, you only can use C++ libraries in Pascal, if the functions for the mutual interface are "flat" C function (i. e. ANSI C functions not using any C++ classes). Of course, on top of this in both source codes the same ABI spec needs to be defined (e.g. "PASCAL" or "STDCALL").

-Michael

Reply by email

Sven Barth
to Michael Schnell
Michael Schnell wrote 9 lines
On 09/21/2013 04:04 PM, Xiangrong Fang wrote: > Hi All, > > I need to use a C++ so file in FreePascal AFAIK, you only can use C++ libraries in Pascal, if the functions for the mutual interface are "flat" C function (i. e. ANSI C functions not using any C++ classes). Of course, on top of this in both source codes the same ABI spec needs to be defined (e.g. "PASCAL" or "STDCALL").

You know that he wrote this the following as well (directly after your quote ends)?

I have written a C wrapper for it, which successfully connected to Pascal.

He only has problems LINKING the library and here it does not matter whether the library is written in C or C++.

Regards,
Sven

Reply by email