简体   繁体   English

C++ 包括 Python.h 使用 makefile 编译

[英]C++ including Python.h compiling using makefile

I am trying to compile a C++ program which uses Python.h to execute some python scripts.我正在尝试编译一个 C++ 程序,它使用Python.h来执行一些 python 脚本。 Before adding the python, I had a makefile which works perfectly.在添加 python 之前,我有一个完美运行的 makefile。 I added the code to run the python script, which involves including the Python.h file.我添加了运行 python 脚本的代码,其中包括包含Python.h文件。

I edited the makefile to include this file, without success.我编辑了 makefile 以包含此文件,但没有成功。

My makefile:我的生成文件:

CC          = g++
SHELL       = /bin/sh
EXECUTABLES = Software_V2
CFLAGS      += -O2 -g0 -Wall -pedantic -Wstrict-prototypes -std=c99 $(shell python3-config --cflags)
LIBS         = -lm -lrt -laes_access -lstdc++ -pthread

OBJS = $(EXECUTABLES).o 

.PHONEY: all
all:    $(EXECUTABLES)

.PHONEY: clean
clean:
    rm -f *.[bo]
    rm -f *.err
    rm -f $(EXECUTABLES)

$(EXECUTABLES): $(OBJS)
        $(CC) $(OBJS) $(CFLAGS) $(LIBS) -o $@

I already installed following libraries:我已经安装了以下库:

  • sudo apt-get install python-dev
  • sudo apt-get install python3-dev
  • sudo apt install libpython3.7-dev

The error after running make I'm receiving the following error:运行后的错误make我收到以下错误:

g++    -c -o Software_V2.o Software_V2.cpp
Software_V2.cpp:3:10: fatal error: Python.h: No such file or directory
 #include <Python.h>
          ^~~~~~~~~~
compilation terminated.
make: *** [<builtin>: Software_V2.o] Error 1

I also tried changing the include from #include <Python.h> to #include <python3.7m/Python.h>我还尝试将包含从#include <Python.h>更改为#include <python3.7m/Python.h>

None of my tries succeeded, I'm out of inspiration at this moment to try some new things, maybe you guys know how to solve my problem.我的尝试都没有成功,我现在没有灵感去尝试一些新的东西,也许你们知道如何解决我的问题。 Thank you!谢谢!

You are mixing C and C++.您正在混合 C 和 C++。 These are completely different languages: you shouldn't confuse them.这些是完全不同的语言:您不应该混淆它们。

In makefiles, the default rules use CC to hold the C compiler and CXX to hold the C++ compiler.在 makefile 中,默认规则使用CC来保存 C 编译器,使用CXX来保存 C++ 编译器。 Similarly, they use CFLAGS to hold flags for the C compiler and CXXFLAGS to hold flags for the C++ compiler.类似地,他们使用CFLAGS来保存 C 编译器的标志,使用CXXFLAGS来保存 C++ 编译器的标志。

In your makefile you've not set either the CXX or the CXXFLAGS variables, but you're trying to use the default rule to build your object file.在您的 makefile 中,您没有设置CXXCXXFLAGS变量,但您正在尝试使用默认规则来构建您的目标文件。 The default value for CXX is g++ (on your system) and the default value for CXXFLAGS is empty. CXX的默认值为g++ (在您的系统上),而CXXFLAGS的默认值为空。 That's why when make shows you the compile command, none of your options appear there:这就是为什么当 make 向您显示编译命令时,您的任何选项都没有出现在那里:

 g++    -c -o Software_V2.o Software_V2.cpp

If you're trying to compile C++ code (implied by the fact that your filename extension is .cpp not .c ) then you should set CXXFLAGS not CFLAGS .如果您正在尝试编译 C++ 代码(暗示您的文件扩展名是.cpp而不是.c ),那么您应该设置CXXFLAGS而不是CFLAGS

Also, it's wrong to set -std=c99 if you're trying to compile C++;此外,如果您尝试编译 C++,则设置-std=c99是错误的; that flag sets the C 1999 standard, not C++.该标志设置了 C 1999 标准,而不是 C++。

Try include "Python.h" - when you use <> the pre-compiler looks in a different set of directories.尝试include "Python.h" - 当您使用 <> 时,预编译器会在一组不同的目录中查找。

Also run python3-config --cflags on the command line and make sure it gives you the right include directories - the Python.h file should be in one of them.还要在命令行上运行python3-config --cflags并确保它为您提供正确的包含目录 - Python.h 文件应该在其中之一中。

Ultimately, the problem is that the file is not being found in the standard directories that the pre-complier checks, nor in the directories that you specified (with the -I option).最终,问题是在预编译器检查的标准目录中找不到该文件,也没有在您指定的目录中(使用 -I 选项)。

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

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