Python에 포함 된 C ++ 개체 파일 연결

Oct 03 2020

formatting_SQ.cpp헤더 파일의 formatting_SQ.h다른 생성자 파일 ( neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h)에 연결하려는 헤더 파일 의 C ++ 생성자 파일 ( )이 있습니다 formatting_SQ.o.

그런 다음 main.cpp 파일을이 formatting_SQ.o파일 과 연결하고 싶습니다 . 문제가있다 : formatting_SQ파이썬 내장되며, 지금까지 내 이해가가는대로, C ++에서 파이썬 내장이 컴파일 플래그를 필요로 -lpython3.6m리눅스를 : 이러한 플래그는에 대한 참조가 필요 main()나는에없는 기능 formatting_SQ.cpp은 의미 생성자 파일이기 때문에이 개체 파일이됩니다.

그래서 먼저 각 생성자 파일에 대한 개체 파일을 만든 다음 모든 것을 한 번에 연결하려고했습니다 .

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.

그런 다음 formatting_SQ.cpp다른 모든 생성자 파일과 독립적 으로 컴파일을 시도 했지만 예상대로 .NET에 main에 대한 참조가 없기 때문에 작동하지 않습니다 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 ?

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

답변

Joachim Oct 03 2020 at 12:19

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.