# Linking a 3rd party library

**URL:** <https://community.freefem.org/t/linking-a-3rd-party-library/1313>\
**Category:** General Discussion\
**Created:** [November 8, 2021, 8:47am UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313 "2021-11-08T08:47:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)\
**Post date:** [November 8, 2021, 8:47am UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/1 "2021-11-08T08:47:12Z")

</div>

Hello everyone,

I am trying to link a 3rd party library in FreeFem++ called cwipi, the library in coded in C++. I changed the pref file to link the .so and the .h files but it did not work (I can load the library but I can’t use any function). I was wondering if I should transform the c++ functions then compile with ff-c++, but that’s a tedious task… Is there any way to directly link 3rd party libraries?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [November 8, 2021, 11:49am UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/2 "2021-11-08T11:49:01Z")

</div>

> I was wondering if I should transform the c++ functions then compile with ff-c++

That is exactly what you need to do. FreeFEM can’t guess what functions you need from the library, so you need to interface them yourself. There are many examples in `plugin/seq/` and `plugin/mpi/`.

---

<div class="post-metadata">

**Author:** ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)\
**Post date:** [November 8, 2021, 12:32pm UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/3 "2021-11-08T12:32:41Z")

</div>

Thank you for your reply, I will check them out.

---

<div class="post-metadata">

**Author:** ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)\
**Post date:** [November 8, 2021, 2:53pm UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/4 "2021-11-08T14:53:33Z")

</div>

I tried an example that worked, the function is called LinkTest in this file:  
[linktocwipi.log](https://community.freefem.org/uploads/short-url/8MNbIpYXxn3zCDeoLz3dvuC1Ghb.log) (764 Bytes)

However when I try to convert a function using the same method I get the following errors:

invalid conversion from ‘**void (_)(const char_, int)**’ to '_OneOperator2\_\<void, const char, int\>_\*  
invalid use of void expression {return **SetAny( f( GetAny((\*a0)(s)) , GetAny((\*a1)(s)) ) )**;}

Do you have any idea on how to fix it? I am passing the correct arguments for the function. Thank you in advance.

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [November 8, 2021, 2:57pm UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/5 "2021-11-08T14:57:29Z")

</div>

There are a couple of issues:

- cannot return `void`, return a `long` instead (`return 0L;`)
- cannot use `int`, need `long` instead
- cannot use `const char *`, need `std::string*` (look at `plugin/seq/addNewType.cpp`)

---

<div class="post-metadata">

**Author:** ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)\
**Post date:** [November 8, 2021, 4:45pm UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/6 "2021-11-08T16:45:24Z")

</div>

I see, I tried correcting with the above modifications: [linktocwipi.log](https://community.freefem.org/uploads/short-url/hg6BAVbhmzJO1sIHvfOV68BYfEP.log) (815 Bytes)

I’m still getting the following error: invalid conversion from ‘**long int (_)(std::\_\_cxx11::string_, long int)**’ {aka ‘**long int (\*)(std::\_** **\_cxx11::basic\_string\*, long int)**’} to ‘ **OneOperator2\_\<long int, std::\_\_cxx11::basic\_string\*, long int\>::func** ’ {aka ‘ **l** **ong int (_)(std::\_\_cxx11::basic\_string_ const&, const long int&)**’} [**-fpermissive**]

Is it also related to the string declaration?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [November 8, 2021, 5:21pm UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/7 "2021-11-08T17:21:54Z")

</div>

Please look at some more examples and see what your compiler is telling you. You cannot use values, but need to pass const references instead, see, e.g., `plugin/seq/BinaryIO.cpp:Global.Add("LoadFlag", "(", new OneOperator2_< double, long int *, string * >(LoadFlag));`

---

<div class="post-metadata">

**Author:** ![Floran](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@Floran](https://community.freefem.org/u/Floran)\
**Post date:** [May 16, 2024, 10:39am UTC](https://community.freefem.org/t/linking-a-3rd-party-library/1313/8 "2024-05-16T10:39:47Z")

</div>

In case some of you would like to employ the openmp parallelization from your library, you would need to add the flag -fopenmp in the ff-c++ file, line 8

> FFCXXFLAGS='-g -fopenmp …
