链接用Python嵌入的C ++对象文件 [英] Linking C++ object files embedded with Python

查看:111
本文介绍了链接用Python嵌入的C ++对象文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个头文件formatting_SQ.h的C ++构造器文件(formatting_SQ.cpp),我想链接到头文件(neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h)的其他构造器文件,以便具有formatting_SQ.o.

I have a C++ constructor file (formatting_SQ.cpp) of a header file formatting_SQ.h which I want to link to other constructor files of header files (neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h) in order to have formatting_SQ.o.

然后,我想将我的main.cpp文件与此formatting_SQ.o文件链接.问题是:formatting_SQ是用python嵌入的,据我所知,用Python嵌入的C ++在Linux上需要编译标志-lpython3.6m:这样的标志需要引用main()函数,我不这样做. formatting_SQ.cpp中没有,因为它是一个构造文件,意为目标文件.

Then, I want to link my main.cpp file with this formatting_SQ.o file. The problem is: formatting_SQ is embedded with python, and as far as my understanding goes, C++ embedded with Python needs the compiling flag -lpython3.6m on Linux: such flag requires a reference to a main() function, which I don't have in formatting_SQ.cpp because it's a constructor file meant to be an object file.

因此,我首先尝试为每个构造函数文件创建目标文件,然后一次将所有内容链接在一起 :

So I first tried to create object files for each constructor file and then link everything together at once:

g++ -c -O3 -Wall -fPIC -fopenmp -std=c++14 -lstdc++ `python3 -m pybind11 --includes` *.cpp 
g++ -o the_executable neat.o nnode.o link.o trait.o gene.o network.o innovation.o organism.o species.o genome.o population.o formatting_SQ.o main.o -fopenmp -O3 -Wall -fPIC `python3 -m pybind11 --includes` -lpython3.6m

这是我的第一个问题:这些命令正确还是最终缺少编译标志?尝试执行./the_executable时,这给我带来了细分错误.

Here comes my first question: Are these command right or is there eventually a compilation flag missing ? This gives me a segmentation fault as I try to execute ./the_executable.

然后,我尝试与所有其他构造函数文件一起独立编译formatting_SQ.cpp,但是按预期,这是行不通的,因为formatting_SQ.cpp中没有对main的引用.

Then, I tried to compile formatting_SQ.cpp independently with all other constructor files, but as expected, this doesn't work because there is no reference to main in formatting_SQ.cpp.

g++ -o temp_formatting neat.o nnode.o link.o trait.o gene.o network.o innovation.o organism.o species.o genome.o population.o formatting_SQ.o -fopenmp -O3 -Wall -fPIC `python3 -m pybind11 --includes` -lpython3.6m

这是我的第二个问题:我如何创建一个将formatting_SQ.cpp与所有其他构造函数文件 链接的python嵌入式目标文件,而不会出现undefined reference to main错误?

So here comes my second question: how could I create a python embedded object file linking formatting_SQ.cpp with all other constructor files without having this undefined reference to main error ?

formatting_SQ.cpp

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include <omp.h>
#include "formatting_SQ.h"
#include "neat.h"
#include "network.h"
#include "link.h"
#include "nnode.h"
#include "trait.h"
#include "gene.h"
#include "genome.h"
#include "innovation.h"
#include "organism.h"
#include "species.h"
#include "population.h"

namespace py = pybind11;
py::module compile_data = py::module::import("initialize");

main.cpp

#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include "formatting_SQ.h"
#include <omp.h>
    
namespace py = pybind11;
        
int main(int argc, char** argv){
      
....

推荐答案

因此,经过长时间的研究,我可以得出结论,编译方法是正确的,但是请务必谨慎地声明从python导入模块的位置,因为这是我的问题

So after some long hours of research I can conclude that the compilation method is correct but BE EXTREMELY CAREFULL with where you declare your import modules from python, because this was the problem for me

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
#include <omp.h>
#include "formatting_SQ.h"
#include "neat.h"
#include "network.h"
#include "link.h"
#include "nnode.h"
#include "trait.h"
#include "gene.h"
#include "genome.h"
#include "innovation.h"
#include "organism.h"
#include "species.h"
#include "population.h"

namespace py = pybind11;
py::module compile_data = py::module::import("initialize"); DON'T DO THIS its wrong !!! 

必须在本地声明模块,否则名称空间中会发生冲突,因为同一模块可能会被多次导入,这会导致分段错误.

You must declare your modules locally otherwise there be some conflicts in the namespace as the same module may be imported more than once and this causes the segmentation fault.

这篇关于链接用Python嵌入的C ++对象文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆