简体   繁体   English

链接嵌入 Python 的 C++ 对象文件

[英]Linking C++ object files embedded with Python

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 .我有一个头文件formatting_SQ.h的C++构造函数文件( formatting_SQ.cpp ),我想将它链接到头文件的其他构造函数文件( neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h ),以便有formatting_SQ.o

Then, I want to link my main.cpp file with this formatting_SQ.o file.然后,我想我的main.cpp文件,此链接formatting_SQ.o文件。 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.问题是: formatting_SQ是用 python 嵌入的,据我所知,嵌入 Python 的 C++ 在 Linux 上需要编译标志-lpython3.6m :这样的标志需要对main()函数的引用,而我不需要在formatting_SQ.cpp有,因为它是一个构造函数文件,旨在成为一个对象文件。

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

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 .当我尝试执行./the_executable这给了我一个分段错误。

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 .于是,我试图编译formatting_SQ.cpp与所有其他构造文件独立,但如预期,这不起作用,因为在主没有提及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

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 ?所以我的第二个问题来了:如何创建一个 python 嵌入的对象文件,将formatting_SQ.cpp与所有其他构造函数文件链接起来,undefined reference to main error 的undefined reference to main

formatting_SQ.cpp格式化_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){
      
....

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因此,经过长时间的研究,我可以得出结论,编译方法是正确的,但要非常小心从 python 中声明导入模块的位置,因为这对我来说是问题

#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.您必须在本地声明您的模块,否则命名空间中会出现一些冲突,因为同一个模块可能会被多次导入,这会导致分段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM