簡體   English   中英

為什么我的.so文件由boost.python和c ++頭文件編譯失敗?

[英]why my .so file which is complied by boost.python and c++ header file failed?

我基於我的C ++代碼成功構建了一些.so,以便讓python調用。 但是對於這一點,我無法以所有可以思考的方式進行構建是很奇怪的。 誰能幫我?

exchang_info.h

#ifndef EXCHANGE_INFO_H_
#define EXCHANGE_INFO_H_

#include <sys/time.h>
#include <fstream>
#include <stdio.h>
#include "define.h"
#include "info_type.h"
#include "order_side.h"
// #include "wrapstruct.h"

struct ExchangeInfo {
  InfoType::Enum type;
  char contract[MAX_CONTRACT_LENGTH];
  char order_ref[MAX_ORDERREF_SIZE];
  int trade_size;
  double trade_price;
  char reason[EXCHANGE_INFO_SIZE];
  OrderSide::Enum side;

  ExchangeInfo()
    : trade_size(0),
      trade_price(-1) {
  }

  void Show(std::ofstream &stream) const {
    stream.write((char*)this, sizeof(*this));
  }

  void ShowCsv(FILE* stream) const {
    /*  
    char time_s[32];
    snprintf(time_s, sizeof(time_s), "%ld.%ld", time.tv_sec, time.tv_usec);
    double time_sec = atof(time_s);
    */
    fprintf(stream, "%s,%s,%s,%d,%lf,%s,%s\n", InfoType::ToString(type),contract,order_ref,trade_size,trade_price,reason,OrderSide::ToString(side));
  }

  void Show(FILE* stream) const {
    timeval time;
    gettimeofday(&time, NULL);
    fprintf(stream, "%ld %06ld exchangeinfo %s |",
            time.tv_sec, time.tv_usec, order_ref);

    fprintf(stream, " %lf@%d %s %s %s\n", trade_price, trade_size, 
    InfoType::ToString(type), contract, OrderSide::ToString(side));
  }
};

#endif  //  EXCHANGE_INFO_H_

wrapstruct.h

#ifndef WRAPSTRUCT_H_
#define WRAPSTRUCT_H_

#include "exchange_info.h"
#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(exchangeinfo) {
  class_<ExchangeInfo>("ExchangeInfo", init<>())
    .def_readwrite("type", &ExchangeInfo::type)
    .def_readwrite("contract", &ExchangeInfo::contract)
    .def_readwrite("order_ref", &ExchangeInfo::order_ref)
    .def_readwrite("trade_size", &ExchangeInfo::trade_size)
    .def_readwrite("trade_price", &ExchangeInfo::trade_price)
    .def_readwrite("reason", &ExchangeInfo::reason)
    .def_readwrite("side", &ExchangeInfo::side);
    //.def("Show", &ExchangeInfo::ShowCsv);
  enum_<InfoType::Enum>("InfoType")
    .value("Uninited", InfoType::Uninited)
    .value("Acc", InfoType::Acc)
    .value("Rej", InfoType::Rej)
    .value("Cancelled", InfoType::Cancelled)
    .value("CancelRej", InfoType::CancelRej)
    .value("Filled", InfoType::Filled)
    .value("Pfilled", InfoType::Pfilled)
    .value("Position", InfoType::Position)
    .value("Unknown", InfoType::Unknown);
  enum_<OrderSide::Enum>("OrderSide")
    .value("Buy", OrderSide::Buy)
    .value("Sell", OrderSide::Sell);
};

#endif //  WRAPSTRUCT_H_

編譯命令:

g++ -std=c++11 -FPIC -shared wrapstruct.h -o exchangeinfo.so

它可以包含.so文件,但不能由Python導入,當我嘗試導入exchangeinfo ,錯誤顯示為:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /root/lib-hft/include/exchangeinfo.so: invalid ELF header

這些天真的很困擾我,有人可以幫助我嗎? 您是否有更好的工具可以用來包裝c ++代碼以供python調用? 謝謝

g++ -std=c++11 -FPIC -shared wrapstruct.h -o exchangeinfo.so
                             ^^^^^^^^^^^^

沒有。

您不能將頭文件編譯為目標代碼。 嘗試時, gcc創建預編譯的標頭 ,而不是任何類型的目標文件。

% file exchangeinfo.so
exchangeinfo.so: GCC precompiled header (version 014) for C++

其他編譯器可能有用也可能無效。

將文件重命名為wrapstruct.cpp ,或創建一個新文件,並用單行命名

#include "wrapstruct.h"

並編譯。 第一種方法是首選。 BOOST_PYTHON_MODULE宏定義了一個變量,最好不要將此類定義放在標頭中。


實際上,您可以強制gcc將具有任何擴展名的文件視為任何類型的文件。 您可以添加-x c++標志,並且gcc會將其編譯為C ++文件,而不管擴展名如何,但這是不推薦的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM