Verknüpfen von in Python eingebetteten C ++ - Objektdateien

Oct 03 2020

Ich habe eine C ++ Konstruktordatei ( formatting_SQ.cpp) einer Header-Datei, formatting_SQ.hdie ich mit anderen Konstruktor-Dateien von Header-Dateien ( neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h) verknüpfen möchte, um zu haben formatting_SQ.o.

Dann möchte ich meine main.cpp-Datei mit dieser formatting_SQ.oDatei verknüpfen . Das Problem ist: formatting_SQist in Python eingebettet, und nach meinem Verständnis benötigt C ++, eingebettet in Python, das Kompilierungsflag -lpython3.6munter Linux: Ein solches Flag erfordert einen Verweis auf eine main()Funktion, die ich nicht habe, formatting_SQ.cppweil es sich um eine Konstruktordatei handelt eine Objektdatei sein.

Also habe ich zuerst versucht, Objektdateien für jede Konstruktordatei zu erstellen und dann alles auf einmal miteinander zu verknüpfen :

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

Hier kommt meine erste Frage: Sind diese Befehle richtig oder fehlt irgendwann ein Kompilierungsflag? Dies gibt mir einen Segmentierungsfehler, wenn ich versuche auszuführen ./the_executable.

Dann habe ich versucht, formatting_SQ.cppunabhängig mit allen anderen Konstruktordateien zu kompilieren , aber wie erwartet funktioniert dies nicht, da es keinen Verweis auf main in gibt 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

Hier kommt meine zweite Frage: Wie kann ich eine eingebettete Python-Objektdatei erstellen, die formatting_SQ.cppmit allen anderen Konstruktordateien verknüpft ist, ohne dass dieser undefined reference to mainFehler auftritt?

formatatting_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){
      
....

Antworten

Joachim Oct 03 2020 at 12:19

Nach einigen langen Stunden der Recherche kann ich zu dem Schluss kommen, dass die Kompilierungsmethode korrekt ist, aber EXTREM SORGFÄLTIG ist, wenn Sie Ihre Importmodule aus Python deklarieren, da dies das Problem für mich war

#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 !!! 

Sie müssen Ihre Module lokal deklarieren, da sonst Konflikte im Namespace auftreten, da dasselbe Modul möglicherweise mehrmals importiert wird und dies den Segmentierungsfehler verursacht.