参考官方文档的Example 8: Using C++ with shared libraries,由于例子是linux,我想改为Windows的dll形式。
https://github.com/JuliaInterop/Cxx.jl
头文件JuliaDll.h
#ifndef ARRAYMAKER_H
#define ARRAYMAKER_Hclass __declspec(dllexport) ArrayMaker
{
private:
int iNumber;
float fNumber;
float* fArr;
public:
extern “C” __declspec(dllexport) ArrayMaker(int, float);
float* fillArr();
};extern “C” __declspec(dllexport) double GetSum(double x, double y);
#endif
cpp文件JuliaDll.cpp
#include “JuliaDll.h”
#include <iostream>using namespace std;
ArrayMaker::ArrayMaker(int iNum, float fNum) {
cout << "Got arguments: " << iNum << ", and " << fNum << endl;
iNumber = iNum;
fNumber = fNum;
fArr = new float[iNumber];
}float* ArrayMaker::fillArr() {
cout << “Filling the array” << endl;
for (int i=0; i < iNumber; i++) {
fArr[i] = fNumber;
fNumber *= 2;
}
return fArr;
}double GetSum(double x, double y)
{
return x + y;
}
参考例子导入库
julia> const path_to_lib = pwd()
addHeaderDir(path_to_lib, kind=C_System)
Libdl.dlopen(joinpath(path_to_lib, “JuliaDll.dll”), Libdl.RTLD_GLOBAL)
cxxinclude(joinpath(path_to_lib, “JuliaDll.h”))
调用这个导出为C接口的函数可行
julia> a = @cxx GetSum(1,2)
3.0
参照例子报错
julia> maker = @cxxnew ArrayMaker(5, 2.0)
LLVM ERROR: Program used external function ‘_ZN10ArrayMakerC1Eif’ which could not be resolved!
想请教一下是哪里不对