c++转换成perl:如何使用c++11字符串STL函数

swig c++ to perl : how to use c++11 string STL functions

本文关键字:c++11 字符串 STL 函数 何使用 转换 perl c++      更新时间:2023-10-16

我想从使用Perl的网站调用c++函数。c++代码工作得很好,我在SWIG包装器中遇到了一些关于c++11中的新函数的麻烦。字符串> STL。在这种情况下stoi(string),而问题与其他函数相同,如string.pop_back(), stol…所有的功能都带到了与c++11版本。

如何使SWIG考虑到这些新函数,以便它可以编译?

这是我得到的错误信息错误:' stoi '不是' std '的成员

/* --- source fonctions.cpp --- */
bool checkRIB(std::string word){
    cout << "verification du rib : "<< word<<endl;
    if (word.size()!=23) return false;
    for (size_t i=0; i!=word.size(); i++) {
        if (!isdigit(word[i])){
            if ((word[i]>='A'&&word[i]<='I')){
                word[i]= (int)(word[i]-'A')%10+'1';
            }else if ((word[i]>='J'&&word[i]<='R')){
                word[i]= (int)(word[i]-'J')%10+'1';
            } else if ((word[i]>='S'&&word[i]<='Z')){
                word[i]= (int)(word[i]-'S')%10+'2';
            } else cout << "mauvais code"<<endl;
        }
    }   
    return ( (97-((89*std::stoi(word.substr(0,5)) + 15*std::stoi(word.substr(5,5))+3*std::stol(word.substr(10,11))) % 97)) == std::stoi(word.substr(21,2)) );
}
/* --- header fonctions.h--- */
#ifndef DEF_FONCTIONS
#define DEF_FONCTIONS
#include <fstream>      // pour lecture / ecriture de fichiers
#include <iostream>
#include <math.h>
bool checkRIB(std::string word);
#endif
/* --- interface code interface.i --- */
%module interface
%include"std_string.i"
%include "std_map.i"
%include "std_vector.i"
%{ 
/* Put header files here or function declarations like below */
    bool checkRIB(std::string word);
}
bool seRessemblent(const std::string &s1, const std::string & s2, float seuil=0.65);
%}
bool checkRIB(std::string word);

和以下编译步骤(在Ubuntu上):

1% swig -c++ -perl5 interface.i
2% g++ -c `perl -MConfig -e 'print join(" ", @Config{qw(ccflags optimize cccdlflags)}, "-I$Config{archlib}/CORE")'` fonctions.cpp interface_wrap.cxx
(here error : *error: ‘stoi’ is not a member of ‘std’*)
3% g++ `perl -MConfig -e 'print $Config{lddlflags}'` fonctions.o interface_wrap.o -o interface.so

其他功能运行正常(由于空间原因未显示)

谢谢你的帮助

亚历克西斯

我找到了一个解决问题的方法:我将编译过程从

1% swig -c++ -perl5 interface.i
2% g++ -c `perl -MConfig -e 'print join(" ", @Config{qw(ccflags optimize cccdlflags)}, "-I$Config{archlib}/CORE")'` fonctions.cpp interface_wrap.cxx
(here error : *error: ‘stoi’ is not a member of ‘std’*)
3% g++ `perl -MConfig -e 'print $Config{lddlflags}'` fonctions.o interface_wrap.o -o interface.so

1% swig -c++ -perl5 interface.i
2% g++ -c -fPIC fonctions.cpp -std=c++11
3% g++ -c `perl -MConfig -e 'print join(" ", @Config{qw(ccflags optimize cccdlflags)}, "-I$Config{archlib}/CORE")'` interface_wrap.cxx
4% g++ `perl -MConfig -e 'print $Config{lddlflags}'` fonctions.o interface_wrap.o -o interface.so

这样就可以根据需要在c++11中编译普通的c++代码。要使用-fPIC选项,以便库可以连接。

我无法找到一种方法来使用c++11与接口包装虽然