问题
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
It generated the module StatHandler.pm
I generated the .so file using the following commands.
g++ -c statHandler.h statHandler.cpp -fPIC
g++ -O2 -shared -o StatHandler.so statHandler.o
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 is set properly. I tried to go through the SWIG documentation and I didn't get much from it.
What should I specify in StatHandler.i file to generate the package "Vinod::StatHandler". Do I need to provide any other options for swig?
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();
};
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
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;
}
回答1:
Actually, you forgot to compile the wrapper that gets generated by swig
.
As stated in the SWIG Doc, you need to :
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
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.
来源:https://stackoverflow.com/questions/16146606/generate-a-perl-module-with-a-separate-namespace-using-swig