【问题标题】:swig c++ to perl : how to use c++11 string STL functionsswig c++ to perl : 如何使用 c++11 字符串 STL 函数
【发布时间】:2014-06-12 16:13:48
【问题描述】:

我想从一个使用 Perl 的网站调用 c++ 函数。 c++ 代码工作正常,我从 SWIG 包装器中遇到了关于 STL 中 c++11 中的一些新函数的问题。在本例中为 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

该界面在其他功能上运行良好(由于空间原因未显示)

感谢您的帮助

亚历克西斯

【问题讨论】:

  • 您需要将-std=c++11 传递给gcc。 swig 必须有一种传递额外编译器标志的方法。

标签: c++ perl c++11 stl swig


【解决方案1】:

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

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++代码就可以根据需要在c++11中编译了。 将使用 -fPIC 选项,以便可以加入库。

但我找不到将 c++11 与接口包装器一起使用的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-18
    • 2011-02-06
    • 2018-11-06
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2012-06-17
    相关资源
    最近更新 更多