python - 为什么我的由 boost.python 和 c++ 头文件编译的 .so 文件失败了?
问题描述
我根据我的 c++ 代码成功构建了一些 .so,让 python 调用。但是对于这个,很奇怪,我不能以我能想到的所有方式来构建。谁能帮我?
交换信息.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_
包装结构.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++ 文件,无论扩展名如何,但极不推荐这样做。
推荐阅读
- javascript - 为什么 Phaser 3 中的图像没有变化?
- flutter - 如何在谷歌颤动地图上显示多个用户
- javascript - 如何通过打字稿中的setinterval增加数字
- javascript - 如何按数字键值对对象进行排序
- html - 是否可以在 div 或 span 元素上创建背景视频?
- mysql - Hybris 增加小数位数
- c++ - 如何在 C 中获取像素而不产生任何垃圾
- android - 如果删除文件AndroidManifest.xml,有办法恢复文件吗?
- asp.net-core - 使用 Gijgo 网格和 ASP.NET core 3.1 无法正确呈现网格内容
- c# - C# SQL 数据库访问