# problems with LibraryFunctionLoad

**URL:** <https://community.wolfram.com/t/problems-with-libraryfunctionload/2881>\
**Category:** Questions\
**Tags:** mathematica, external-interfaces\
**Created:** [August 7, 2014, 2:04pm UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881 "2014-08-07T14:04:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![egger](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@egger](https://community.wolfram.com/u/egger)\
**Post date:** [August 7, 2014, 2:04pm UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/1 "2014-08-07T14:04:24Z")

</div>

Hi,  
Im trying out Mathematica to connect our shared library to Mathematika but I have problems during running functions in the shared Library. So I started with a test shared library.  
Some parts work fine so far. At first what worked:

```
1.	AppendTo[$LibraryPath, ToFileName["/slowfs/lithop4-nb-2/egger/Mathematica/Tests"]];

2.	LibraryLoad["libtestlib"]

3.	INTTEST2 = LibraryFunctionLoad["libtestlib", "Inttest2", {Integer, Integer}, Integer]

```

That worked so far. But running

```
INTTEST2[10, 101]

```

returned an error message:

```
LibraryFunction::rterr: An error with return code -1429266942 was encountered evaluating the function Inttest2. >>

```

I used Red Hat Enterprise Linux Client release 5.7 (Tikanga) and gcc version 4.1.2 20080704 (Red Hat 4.1.2-51).  
Building the test shared library was done by

gcc -c -g -fPIC testlib.cpp -I.

gcc -shared -o libtestlib.so testlib.o

The console output from my shared library was:

Inttest2 parameter 1 is -1429266944 and parameter 2 is 2

Inttest parameter is -1429249280

I added two attachments testlib\_header.txt that contains the header file testlib.h and testlib\_source.txt in unix text format.

The interesting lines of the header are:

void Inttest1(int iTest);

int Inttest2(int iTest, int iTest2);

and of the cpp file are:

void Inttest1(int iTest)  
{  
std::cerr \<\< "Inttest parameter is " \<\< iTest \<\< std::endl;  
}

long Inttest2(long iTest, long iTest2)  
{  
std::cerr \<\< "Inttest2 new parameter 1 is " \<\< iTest \<\< " and parameter 2 is " \<\< iTest2 \<\< std::endl;  
long iSum = iTest + iTest2;  
return iSum;  
}

I hope you can help me with that problem.  
Kind regards,  
Roland

---

<div class="post-metadata">

**Author:** ![egger](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@egger](https://community.wolfram.com/u/egger)\
**Post date:** [August 8, 2014, 2:12pm UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/2 "2014-08-08T14:12:46Z")

</div>

Hello,

has anybody any experience with LibraryFunctionLoad?

Kind regards,

Roland

---

<div class="post-metadata">

**Author:** ![nickl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/nickl/32/21430_2.png) [@nickl](https://community.wolfram.com/u/nickl)\
**Post date:** [August 8, 2014, 7:14pm UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/3 "2014-08-08T19:14:01Z")

</div>

From looking at the source you posted, you haven’t included the WolframLibrary.h header or the DLLEXPORT type; have you gone through the LibraryLink [“User Guide”](http://reference.wolfram.com/language/LibraryLink/tutorial/LibraryStructure.htm)?

In general, to call a function like this, you’ll need some sort of interface function like:

```
DLLEXPORT int demo1_I_I( WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {
	mint I0;
	mint I1;
	I0 = MArgument_getInteger(Args[0]);
	I1 = I0 + 1;
	MArgument_setInteger(Res, I1);
	return LIBRARY_NO_ERROR;
}

```

(this gets an integer from M\_, add 1 to it, and then returns it to M\_)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [August 8, 2014, 8:24pm UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/4 "2014-08-08T20:24:30Z")

</div>

It looks like there is a misunderstanding here. `LibraryFunctionLoad` is not meant for loading and calling arbitrary C functions. You will need to write C functions that follow specific rules before they can be loaded using `LibraryFunctionLoad`. Please see the LibraryLink user guide on how to do this.

---

<div class="post-metadata">

**Author:** ![egger](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@egger](https://community.wolfram.com/u/egger)\
**Post date:** [August 11, 2014, 8:42am UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/5 "2014-08-11T08:42:57Z")

</div>

Hi Nick,  
thank you for your answer. That is an interesting information. I tried to look into your link  
[http://reference.wolfram.com/language/LibraryLink/tutorial/LibraryStructure.htm](http://reference.wolfram.com/language/LibraryLink/tutorial/LibraryStructure.htm)  
but I got only “Document Not Found”. [\Freaked smiley]  
Oops, the page you are looking for cannot be found.

I found and read:

> **[External Operations—Wolfram Documentation](https://reference.wolfram.com/language/guide/ExternalOperations.html)**
>
> Tightly integrated into the Wolfram Language is a rich set of primitives for interacting with external environments. The Wolfram Language's symbolic architecture makes possible powerful symbolic representations for external constructs and...

Ah, now I think I found it:

> **[Wolfram LibraryLink User Guide—Wolfram Documentation](https://reference.wolfram.com/language/LibraryLink/tutorial/Overview.html)**
>
> Introduction Library Structure and Life Cycle Interaction with the Wolfram Language

Let’s see if it helps me. If you have another link for me please send it.

Kind regards,

Roland

---

<div class="post-metadata">

**Author:** ![egger](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@egger](https://community.wolfram.com/u/egger)\
**Post date:** [August 11, 2014, 8:45am UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/6 "2014-08-11T08:45:35Z")

</div>

Hello Szabolcs,

thank you, too. I found a tutorial link above.

> **[Wolfram LibraryLink User Guide—Wolfram Documentation](https://reference.wolfram.com/language/LibraryLink/tutorial/Overview.html)**
>
> Introduction Library Structure and Life Cycle Interaction with the Wolfram Language

If you know another interesting link for me please send it.  
Best regards,  
Roland

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [August 11, 2014, 1:39pm UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/7 "2014-08-11T13:39:56Z")

</div>

Yes, that’s the one.

You can also take a look here:

- [http://mathematica.stackexchange.com/questions/8438/minimal-effort-method-for-integrating-c-functions-into-mathematica](http://mathematica.stackexchange.com/questions/8438/minimal-effort-method-for-integrating-c-functions-into-mathematica)

---

<div class="post-metadata">

**Author:** ![egger](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@egger](https://community.wolfram.com/u/egger)\
**Post date:** [September 26, 2014, 10:26am UTC](https://community.wolfram.com/t/problems-with-libraryfunctionload/2881/8 "2014-09-26T10:26:50Z")

</div>

Hi,  
Im trying out Mathematica to connect our shared library to Mathematika but I have problems during running functions in the shared Library. So I started with a test shared library.  
Some parts work fine so far. At first what worked:

```
1.	AppendTo[$LibraryPath, ToFileName["/slowfs/lithop4-nb-2/egger/Mathematica/Tests"]];

2.	LibraryLoad["libtestlib"]

3.	INTTEST2 = LibraryFunctionLoad["libtestlib", "Inttest2", {Integer, Integer}, Integer]

```

That worked so far. But running

```
INTTEST2[10, 101]

```

returned an error message:

```
LibraryFunction::rterr: An error with return code -1429266942 was encountered evaluating the function Inttest2. >>

```

I used Red Hat Enterprise Linux Client release 5.7 (Tikanga) and gcc version 4.1.2 20080704 (Red Hat 4.1.2-51).  
Building the test shared library was done by

gcc -c -g -fPIC testlib.cpp -I.

gcc -shared -o libtestlib.so testlib.o

The console output from my shared library was:

Inttest2 parameter 1 is -1429266944 and parameter 2 is 2

Inttest parameter is -1429249280

I added two attachments testlib\_header.txt that contains the header file testlib.h and testlib\_source.txt in unix text format.

The interesting lines of the header are:

void Inttest1(int iTest);

int Inttest2(int iTest, int iTest2);

and of the cpp file are:

void Inttest1(int iTest)  
{  
std::cerr \<\< "Inttest parameter is " \<\< iTest \<\< std::endl;  
}

long Inttest2(long iTest, long iTest2)  
{  
std::cerr \<\< "Inttest2 new parameter 1 is " \<\< iTest \<\< " and parameter 2 is " \<\< iTest2 \<\< std::endl;  
long iSum = iTest + iTest2;  
return iSum;  
}

I hope you can help me with that problem.  
Kind regards,  
Roland
