Reputation: 4901
I am trying to use ccall to call functions from a shared library I created. Currently, when I try to run ccall, I get an error:
ERROR: ccall: could not find function add in library libbar
in anonymous at no file
in include at boot.jl:244
in include_from_node1 at loading.jl:128
in process_options at client.jl:282
in _start at client.jl:351
while loading /somedir/juliatest.jl, in expression starting on line 2
However, when I view the contents of libbar.so, the function is listed:
... 000000000000057c T add(float, float) ...
Here is my setup:
// bar.hpp
#ifndef __cplusplus
extern "C" {
#endif
extern float add(float a, float b);
#ifndef __cplusplus
}
#endif
// bar.cpp
#include "bar.hpp"
float add(float a, float b)
{
return a+b;
}
Here is how I compile it:
g++ -Wall -fPIC -c bar.cpp
gcc -shared -o libbar.so -Wl,-soname,libbar.so.1 -o libbar.so.1.0 bar.o
sudo mv libbar.so.1.0 /opt/lib
sudo ln -sf /opt/lib/libbar.so.1.0 /opt/lib/libbar.so.1
sudo ln -sf /opt/lib/libbar.so.1.0 /opt/lib/libbar.so
Here is my julia script:
println("Running Test Function")
shouldBeThree = ccall( (:add, "libbar"), Float32, (Float32, Float32), 1.0, 2.0)
println("Should be Three: ", shouldBeThree)
Upvotes: 2
Views: 1706
Reputation: 3783
The problem is that you use #ifndef
instead of #ifdef
in the header file.
// bar.hpp
#ifdef __cplusplus
extern "C" {
#endif
float add(float a, float b);
#ifdef __cplusplus
}
#endif
With that change the file should compile with both C and C++ compilers.
Upvotes: 1
Reputation: 4356
You are compiling with g++
. Therefore __cplusplus
is defined, and your extern "C"
is excluded by the preprocessor.
Upvotes: 3