在C ++中包含带有Makefile的Python.h

问题描述 投票:0回答:1

我有以下Makefile,我需要将Python.h添加到我的main.cpp文件中。无论我尝试或搜索什么,我似乎无法弄清楚如何使用Makefile正确导入Python.h。我已经正确安装了python,并且我的文件命名正确。 Python 2.7按预期存在于以下文件夹中:/usr/include/python2.7

main.cpp唯一重要的部分是我包含Python的地方:

#include <Python.h>

我的Makefile目前看起来像这样:

CC=g++
CFLAGS=-c -Wall
LIBS=-lwiringPi

all: my_code_file

single_chan_pkt_fwd: secondary.o main.o
    $(CC) main.o secondary.o $(LIBS) -o my_code_file

main.o: main.cpp
    $(CC) $(CFLAGS) main.cpp

secondary.o: secondary.c
    $(CC) $(CFLAGS) secondary.c

clean:
    rm *.o my_code_file

当我在命令行上运行命令make时,我收到以下错误:

main.cpp:26:20: fatal error: Python.h: No such file or directory
 #include <Python.h>

有人可以帮我解决我的案例中需要的Makefile添加吗?我在Raspberry Pi上运行所有这些,如果这有任何帮助的话。

根据Stack Overflow上的类似问题,我已经尝试过这个(运行正常,但没有帮助):

sudo apt-get install python-dev
python c++ makefile g++
1个回答
0
投票

您可以使用python-config(或python3-config)实用程序获取CFLAGS和LDFLAGS。

%> python3-config --cflags
-I/path/to/Python3.6.5/include/python3.6m -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
%> python3-config --ldflags
-L/path/to/Python3.6.5/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/path/to/Python-3.6.5/lib -lpython3.6m -lpthread -ldl -lutil -lrt -lm -Xlinker -export-dynamic

在你的Makefile中,也许像这样设置它们:

PY_CFLAGS  := $(shell python3-config --cflags)
PY_LDFLAGS := $(shell python3-config --ldflags)
© www.soinside.com 2019 - 2024. All rights reserved.