C 朱莉娅:在库中找不到函数
我正在尝试使用ccall调用我创建的共享库中的函数。 当前,当我尝试运行ccall时,我得到一个错误:C 朱莉娅:在库中找不到函数,c,julia,shared-libraries,C,Julia,Shared Libraries,我正在尝试使用ccall调用我创建的共享库中的函数。 当前,当我尝试运行ccall时,我得到一个错误: 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 _sta
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
但是,当我查看libbar.so的内容时,函数如下所示:
。。。
0000000000000 57C T添加(浮动,浮动)
以下是我的设置:
// 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;
}
下面是我如何编译它的:
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
这是我的剧本:
println("Running Test Function")
shouldBeThree = ccall( (:add, "libbar"), Float32, (Float32, Float32), 1.0, 2.0)
println("Should be Three: ", shouldBeThree)
您正在使用
g++
进行编译。因此,定义了\uuu cplusplus
,并且预处理器排除了您的外部“C”
。您正在使用g++
进行编译。因此,定义了\uu cplusplus
,并且预处理器排除了外部“C”
。问题在于在头文件中使用\ifndef
而不是\ifdef
// bar.hpp
#ifdef __cplusplus
extern "C" {
#endif
float add(float a, float b);
#ifdef __cplusplus
}
#endif
<> P>此更改文件应同时使用C和C++编译器编译。 < P>问题是,在头文件中使用<代码>“αIFNDEF < /C>”而不是<代码> >“IFDEF < /C>”。
// bar.hpp
#ifdef __cplusplus
extern "C" {
#endif
float add(float a, float b);
#ifdef __cplusplus
}
#endif
<> P>用C和C++编译器编译文件。我认为NM列出带参数的函数是奇怪的。这是否表明名称已损坏,并且外部“C”不知何故未触发?嗯,如果我查看bar.o,函数确实出现损坏。这意味着我必须修正我如何编译它?然后你必须给朱莉娅一个被破坏的名字,或者弄清楚发生了什么。您是否需要将
extern
关键字与函数原型放在同一行?我不是大师,但我以前从未见过一个函数以这种方式声明为extern。C++正在修改函数名。我更改了bar.hpp,删除了#ifndef stuff//bar.hpp extern“C”float add(float a,float b);现在它运行正确了,我认为nm列出了带参数的函数是很奇怪的。这是否表明名称已损坏,并且外部“C”不知何故未触发?嗯,如果我查看bar.o,函数确实出现损坏。这意味着我必须修正我如何编译它?然后你必须给朱莉娅一个被破坏的名字,或者弄清楚发生了什么。您是否需要将extern
关键字与函数原型放在同一行?我不是大师,但我以前从未见过一个函数以这种方式声明为extern。C++正在修改函数名。我更改了bar.hpp,删除了#ifndef stuff//bar.hpp extern“C”float add(float a,float b);现在它运行正常了