【问题标题】:How to pass const string parameter from perl to c++ using swig如何使用 swig 将 const 字符串参数从 perl 传递到 c++
【发布时间】:2020-05-22 14:47:46
【问题描述】:

我是 Swig 的新手,在调试此错误时需要帮助。

我的动机是使用 Swig、C++、Perl 创建一个耗时更少的日志记录系统。但是我通过 swig 将字符串参数从 Perl 传递给 C++ 时出错。

我已将我的整个代码和错误放在下面。 请帮我解决这个问题。以后谢谢。

Logger.cpp

#include <stdexcept>

#include <string>
#include <ctime>
#include "Logger.h"

using namespace std;
Logger* Logger::pInstance = nullptr;




Logger& Logger::instance()
{
    lock_guard<mutex> guard(sMutex);
    printf("instance");
    if (pInstance == nullptr)
        pInstance = new Logger();
    return *pInstance;
}

void Logger::Cleanup()
{
    lock_guard<mutex> guard(Logger::sMutex);
    delete Logger::pInstance;
    Logger::pInstance = nullptr;
}


Logger::Logger()
{
    printf("Constructor\n");
    mOutputStream.open("log.out", ios_base::app);
    if (!mOutputStream.good()) {
        throw runtime_error("Unable to initialize the Logger!");
    } 

}
void Logger::log(const string& inMessage, const string& inLogLevel)
{
    lock_guard<mutex> guard(sMutex);
    logHelper(inMessage, inLogLevel);
}

void Logger::logHelper(const std::string& inMessage, const std::string& inLogLevel)
{
    mOutputStream << "[ " << current_time() << " ]" << inLogLevel << ": " << inMessage << endl;
}

string Logger::current_time()
{
    time_t rawtime;
  struct tm * timeinfo;
  char buffer[80];

  time (&rawtime);
  timeinfo = localtime(&rawtime);

  strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
  std::string str(buffer);
    return str;
}



Logger::~Logger(){
    printf("deconstructor\n");
    mOutputStream.close();
}  

Logger.h

#ifndef LOGGER_H_
#define LOGGER_H_
#include <stdio.h>

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <mutex>

class Logger
{
    public:
        Logger();
        ~Logger();
        Logger& instance();
        void log(const std::string& inMessage, 
        const std::string& inLogLevel);
    protected:
        static Logger* pInstance;
        std::ofstream mOutputStream;
        void Cleanup();
        void logHelper(const std::string& inMessage, 
        const std::string& inLogLevel);
    std::string current_time();
    private:


        std::mutex sMutex;
};
#endif

Logger.i

%module Logger 
%{
    #include "Logger.h"

%}

%include "Logger.h"

ch.pl

use strict;
use warnings;

use Cwd qw( abs_path );
use File::Basename qw( dirname );
use lib dirname(abs_path($0));

use Logger;  

my $p=new Logger::Logger();
$p->instance();
$p->log("test message");

错误信息*

TypeError in method 'Logger_log', argument 2 of type 'std::string' at ch1.perl line 19.

【问题讨论】:

    标签: c++ perl swig


    【解决方案1】:

    您需要在接口文件中包含 std::string 的类型映射,以便 SWIG 知道如何处理该类型,请参阅 chapter 9.4 in the manual

    9.4.1 std::string std_string.i 库提供类型映射,用于将 C++ std::string 对象与目标中的字符串相互转换 脚本语言。

    %module Logger 
    %include "std_string.i"
    %{
        #include "Logger.h"
    %} 
    %include "Logger.h"
    

    如果我使用它并像这样运行ch.pl

    use strict;
    use warnings;
    use lib '.';
    use Logger;      
    my $p = Logger::Logger->new();
    $p->instance();
    $p->log("test message", "DEBUG");
    

    我得到输出:

    Constructor
    instanceConstructor
    deconstructor
    

    log.out文件的内容是:

    [ 22-05-2020 17:35:12 ]DEBUG: test message
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      相关资源
      最近更新 更多