尝试使用文档中的std::vector
运行此STL示例,但我得到链接器错误。
example.h
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
double average(std::vector<int> v) {
return std::accumulate(v.begin(),v.end(),0.0)/v.size();
}
std::vector<double> half(const std::vector<double>& v) {
std::vector<double> w(v);
for (unsigned int i=0; i<w.size(); i++)
w[i] /= 2.0;
return w;
}
void halve_in_place(std::vector<double>& v) {
std::transform(v.begin(),v.end(),v.begin(),
std::bind2nd(std::divides<double>(),2.0));
}
的例子
%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"
因为我使用swig作为PHP扩展,所以我运行swig -php -c++ example.i
来生成适当的包装器。编译源代码得到
Warning 6 warning C4005: 'inline' : macro redefinition C:'Program Files (x86)'Microsoft Visual Studio 12.0'VC'include'xkeycheck.h 203 1 swig_std_vector
Error 7 error C1189: #error : The C++ Standard Library forbids macroizing keywords. Enable warning C4005 to find the forbidden macro. C:'Program Files (x86)'Microsoft Visual Studio
12.0'VC'include'xkeycheck.h 250 1 swig_std_vector
将_XKEYCHECK_H
添加到预处理器指令中会导致更多错误:
Error 18 error LNK2001: unresolved external symbol "int __cdecl std::_Atomic_compare_exchange_weak_4(unsigned long volatile *,unsigned long *,unsigned long,enum std::memory_order,enum std::memory_order)" (?_Atomic_compare_exchange_weak_4@std@@YAHPECKPEAKKW4memory_order@1@2@Z) C:'Users'Allan'Documents'Visual Studio 2013'Projects'swig_std_vector'swig_std_vector'example_wrap.obj swig_std_vector
Error 19 error LNK2001: unresolved external symbol "unsigned long __cdecl std::_Atomic_fetch_sub_4(unsigned long volatile *,unsigned long,enum std::memory_order)" (?_Atomic_fetch_sub_4@std@@YAKPECKKW4memory_order@1@@Z) C:'Users'Allan'Documents'Visual Studio 2013'Projects'swig_std_vector'swig_std_vector'example_wrap.obj swig_std_vector
Error 20 error LNK2001: unresolved external symbol "unsigned long __cdecl std::_Atomic_load_4(unsigned long volatile *,enum std::memory_order)" (?_Atomic_load_4@std@@YAKPECKW4memory_order@1@@Z) C:'Users'Allan'Documents'Visual Studio 2013'Projects'swig_std_vector'swig_std_vector'example_wrap.obj swig_std_vector
Error 21 error LNK2001: unresolved external symbol "unsigned long __cdecl std::_Atomic_fetch_add_4(unsigned long volatile *,unsigned long,enum std::memory_order)" (?_Atomic_fetch_add_4@std@@YAKPECKKW4memory_order@1@@Z) C:'Users'Allan'Documents'Visual Studio 2013'Projects'swig_std_vector'swig_std_vector'example_wrap.obj swig_std_vector
我可以使用SWIG制作更简单的PHP扩展,但它们不使用源文件中的STL。
需要定义ZEND_WIN32_FORCE_INLINE