gpt4 book ai didi

python - 为什么我的 .so 文件由 boost.python 和 c++ 头文件编译失败?

转载 作者:行者123 更新时间:2023-11-30 03:14:21 34 4
gpt4 key购买 nike

我成功地基于我的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 会创建一个预编译 header ,而不是任何类型的目标文件。

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

其他编译器可能会或可能不会做任何有用的事情。

要么将您的文件重命名为 wrapstruct.cpp,要么创建一个用一行命名的新文件

#include "wrapstruct.h"

然后编译。第一种方式更受欢迎; BOOST_PYTHON_MODULE宏定义了一个变量,这样的定义最好不要放在headers中。


事实上,您可以强制 gcc 将具有任何扩展名的文件视为任何类型的文件;您可以添加 -x c++ 标志,gcc 会将其编译为 C++ 文件,而不管扩展名如何,但极度不推荐这样做。

关于python - 为什么我的 .so 文件由 boost.python 和 c++ 头文件编译失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57899727/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com