问题描述
我有一个头文件formatting_SQ.cpp
的C ++构造文件(formatting_SQ.h
),我想链接到头文件(neat.cpp nnode.cpp link.cpp etc...-> neat.h nnode.h link.h
)的其他构造文件以使{{ 1}}。
然后,我想将我的main.cpp文件与此formatting_SQ.o
文件链接。问题是:formatting_SQ.o
已嵌入python,据我所知,嵌入Python的C ++在Linux上需要编译标志formatting_SQ
:此类标志需要引用-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
中没有对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
与所有其他构造函数文件链接在一起的python嵌入式对象文件 ,而不会出现此formatting_SQ.cpp
错误?
formatting_SQ.cpp
undefined reference to main
main.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");
解决方法
因此,经过长时间的研究,我可以得出结论,编译方法是正确的,但请务必谨慎声明从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 !!!
您必须在本地声明模块,否则名称空间中会发生冲突,因为同一模块可能被多次导入,这会导致分段错误。