我正在尝试包装一个创建3D矢量的C,以便我可以从
Python调用它并可视化数据.我正在尝试用SWIG包装,但是当我这样做时,我收到错误消息
‘vector’ was not declared in this scope
并且将“vector”包含在我能想到的每个文件中我不知道我要做什么来包含它.我已经创建了一组非常基本的测试函数来尝试查看问题所在,它与我正在尝试运行的实际代码大致相似.
TEST.CPP
#include <vector>
#include <iostream>
using namespace std;
vector<int> testfunction(vector <int>& value){
cout <<"Running\n";
return value;
}
test.h
#ifndef TEST_H_ // To make sure you don't declare the function more than once by including the header multiple times.
#define TEST_H_
#include <vector>
#include <iostream>
vector<int> testfunction(vector <int>& value);
test.i
/*test.i*/
%module test
%{
#include "test.h"
#include <vector>
%}
vector<double> testfunction(vector<double> value);
编译我正在使用以下内容
g++ -g -ggdb -c -std=c++0x -I /include/python2.7 -o run test.cpp
swig -c++ -python test.i
gcc -fpic -c test.cpp test_wrap.cxx -I /include/python2.7
gcc -shared test.o test_wrap.o -i _test.so
谁能告诉我哪里出错了?
最佳答案 我找到了这个问题的答案,我将在这里发布更新的代码.问题有两个:
> SWIG的example.i文件错误,需要包含“std_vector.i”
>编译命令需要更多标志.
以下应编译并运行.
example.h文件
#ifndef TEST_H_
#define TEST_H_
#include <vector>
#include <iostream>
std::vector<int> testfunction(std::vector <int>& value);
#endif
example.cpp
#include <vector>
#include <iostream>
std::vector<int> testfunction(std::vector <int>& value){
std::cout <<"Running\n";
return value;
}
example.i
%module example
%{
#include "example.h"
%}
%include "std_vector.i"
// Instantiate templates used by example
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
}
// Include the header file with above prototypes
%include "example.h"
生成文件
所有:
g++ -g -ggdb -std=c++0x -I include/python2.7 -o run example.cpp example_run.cpp
swig -c++ -python example.i
gcc -fpic -c example.cpp example_wrap.cxx -I include/python2.7
gcc -Wl,--gc-sections -fPIC -shared -lstdc++ example.o example_wrap.o -o _example.so
Python调用:
>>> import example as ex
>>> iv=ex.IntVector(1)
>>> ex.testfunction(iv)
快乐矢量!