Skip to content
Advertisement

C++ including Python.h compiling using makefile

I am trying to compile a C++ program which uses Python.h to execute some python scripts. Before adding the python, I had a makefile which works perfectly. I added the code to run the python script, which involves including the Python.h file.

I edited the makefile to include this file, without success.

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:

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>

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!

Advertisement

Answer

You are mixing C and 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. Similarly, they use CFLAGS to hold flags for the C compiler and CXXFLAGS to hold flags for the C++ compiler.

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. The default value for CXX is g++ (on your system) and the default value for CXXFLAGS is empty. That’s why when make shows you the compile command, none of your options appear there:

 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.

Also, it’s wrong to set -std=c99 if you’re trying to compile C++; that flag sets the C 1999 standard, not C++.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement