使用 SWIG 生成具有单独命名空间的 perl 模块 [英] Generate a perl module with a separate namespace using SWIG

查看:66
本文介绍了使用 SWIG 生成具有单独命名空间的 perl 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成一个 perl 模块 Vinod::StatHandler.我有 2 个与 C++ 代码相关的文件.(statHandler.h,statHandler.cpp)

I need to generate a perl module Vinod::StatHandler. I have 2 files related to c++ code.(statHandler.h,statHandler.cpp)

目前我正在使用以下命令来生成模块.

Currently I am using the following command to generate the module.

swig -perl -noproxy -c++ -o StatHandler_wrap.cxx StatHandler.i

它生成了模块 StatHandler.pm

It generated the module StatHandler.pm

我使用以下命令生成了 .so 文件.

I generated the .so file using the following commands.

g++ -c statHandler.h statHandler.cpp -fPIC
g++ -O2 -shared -o StatHandler.so statHandler.o

仅包含use StatHandler;"就出现以下错误在测试 Perl 脚本中.

I got the following error by just including "use StatHandler;" in a test Perl script.

Can't find 'boot_StatHandler' symbol in /home/vinod/cpp/swig//StatHandler.so 

LD_LIBRARY_PATH 设置正确.我试图通读 SWIG 文档,但并没有从中得到多少.

LD_LIBRARY_PATH is set properly. I tried to go through the SWIG documentation and I didn't get much from it.

我应该在 StatHandler.i 文件中指定什么来生成包Vinod::StatHandler".我需要为 swig 提供任何其他选项吗?

What should I specify in StatHandler.i file to generate the package "Vinod::StatHandler". Do I need to provide any other options for swig?

StatHandler.i 的内容:

Contents of StatHandler.i:

%module "Vinod::StatHandler"
%{
#include "statHandler.h"
%}
#ifdef STATIC
%include perlmain.i
#endif

class StatHandler {
 StatHandler(string _dirName, string _statsFile):_statsDir(_dirName),_finalStatsFile(_statsFile){};
  void printStats();
 };

statHandler.h 的内容:

Contents of statHandler.h:

#ifndef STATHANDLER_H_
#define STATHANDLER_H_

#include<iostream>
#include<string.h>
#include<map>
#include<vector>

using namespace std;

class StatHandler {
 private:
  string _statsDir;
  string _finalStatsFile;
  vector<string> _statFiles;
  map <string, int> _dailyStats;
 public:
 StatHandler(string _dirName, string _statsFile):_statsDir(_dirName),_finalStatsFile(_statsFile){};
  bool getAllStatFiles();
  void extractStats();
  void printStats();
};

#endif

StatHandlers.cpp 的内容:

contents of StatHandlers.cpp:

#include <iostream>
#include "statHandler.h"
#include <sys/types.h>
#include <dirent.h>
#include <fstream>
#include <cstdlib>
using namespace std;


bool StatHandler::getAllStatFiles() {
  DIR* dirHandler;
  struct dirent* statsFile;
  if(dirHandler=opendir(_statsDir.c_str())){
    while(statsFile = readdir(dirHandler)){
      size_t size=strlen(statsFile->d_name);
      if(size>3) {
    char extension[4];
    memcpy(extension, &(statsFile->d_name[size-3]), 3);
    extension[3]='\0';
    if(strcmp(extension,".st") == 0) {
      _statFiles.push_back(_statsDir+statsFile->d_name);
    }
      }
    }
  } else {
    return false;
  }
  return true;
}


void StatHandler::extractStats() {
  getAllStatFiles();

  for(vector<string>::iterator fileIter=_statFiles.begin();fileIter!=_statFiles.end();++fileIter) {
    cout<<*(fileIter)<<"\n";
    ifstream in;
    in.open(fileIter->c_str(), ios::in);
    string term;
    while( in.is_open() && !getline(in, term, '\n').eof() ){
      size_t pos=0;
      if( (pos=term.find(" ")) != string::npos) {
    string keyword = term.substr(0,pos);
    string countStr=term.substr(pos+1);
    int count = atoi(countStr.c_str());
    if(_dailyStats.find(keyword) != _dailyStats.end()) {
      _dailyStats[keyword]+=count;
    } else {
       _dailyStats[keyword]=count;
    }
      }
      term.clear();
    }
    in.close();
  }  
}


void StatHandler::printStats() {
  ofstream out;
  out.open(_finalStatsFile.c_str(), ios::out);
  if(out) {
    for(map<string, int>::iterator statIter=_dailyStats.begin(); statIter!=_dailyStats.end();++statIter) {
      out<<statIter->first<<"\t"<<statIter->second<<"\n";
    }
    out.close();
  }

}


int main(int argc, char** argv) {
  StatHandler sh("/home/vinod/cpp/swig/","/home/vinod/cpp/swig/finalStats");
  sh.extractStats();
  sh.printStats();
  return 0;
}

推荐答案

实际上,您忘记编译由swig包装器>.

Actually, you forgot to compile the wrapper that gets generated by swig.

SWIG Doc 所述,您需要:

    swig -perl -noproxy -c++ -o StatHandler_wrap.cxx StatHandler.i
    g++ -c StatHandler.cpp -fPIC
    g++ -c StatHandler_wrap.cxx -I /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/CORE -fPIC
    g++ -O2 -shared -o StatHandler.so StatHandler.o StatHandler_wrap.o

然后,如果您将整个内容放在 Vinod/ 子目录中,LD_LIBRARY_PATH="Vinod" perl -MVinod::StatHandler 将按预期工作.

Then, provided you put the whole thing in the Vinod/ subdir, LD_LIBRARY_PATH="Vinod" perl -MVinod::StatHandler will works as expected.

注意:我在您的各种文件中编辑了大小写和复数,因为它们不一致.

NB: I edited the case and plural in your various files, as they were inconsistent.

这篇关于使用 SWIG 生成具有单独命名空间的 perl 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆