Reputation: 336
I 'm a total beginner in C++ and getting crazy trying to embed Python in C++ using VS Code IDE and GCC compiler. I am stock and now I 'm keep facing this silly error that says:
python.h: No such file or directory gcc
I have followed steps explaned in "Using GCC with MinGW in VS Code" in order to configure C++ in VS Code but I failed to install MinGW (The bin folder was empty) so I add already installed CodeBlocks MinGW to my path and it seems to work.
I have python 3.8 installed and tried other solutions and already put Python.h and python library path in project include path.
"C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/include/" and "C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/libs/"
here is the code that I want to compile:
#include <stdio.h>
#include <conio.h>
#include <python.h>
int main()
{
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");
Py_Finalize();
printf("\nPress any key to exit...\n");
if(!_getch()) _getch();
return 0;
}
and this is my c_cpp_properties.json. (C++ configuration file):
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/include/**",
"C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/libs/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files (x86)/CodeBlocks/MinGW/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x86"
}
],
"version": 4
}
and this is tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:/Program Files (x86)/CodeBlocks/MinGW/bin/gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/Program Files (x86)/CodeBlocks/MinGW/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:/Program Files (x86)/CodeBlocks/MinGW/bin/gcc.exe\""
}
]
}
Upvotes: 1
Views: 5830
Reputation: 336
The problem was a combinations of simple mistakes and as a beginner I solved it by some digging in gcc -l -L option flags for library link example and gcc arguments docs.
There are several simple and important points that should be observed:
1. The order of options is really important. If its wrong, compiler wont work. It should be like -I(capital i),-g,-L,-l(lower case L),-o.
2. To embed python in C++ firstly the path to python include files have to be known to compiler by -I option. Then the path to python libraries by a -L option. Then name of the each required python .lib file contained in libs folder of python.
Here is an example of it:
"-IC:/Users/MPC/AppData/Local/Programs/Python/Python38-32/include",=>(path to python include files)
"-g",=>(debug option specified by ide)
"${file}",
"-LC:/Users/MPC/AppData/Local/Programs/Python/Python38-32/libs",=>(path to python libraries)
"-lpython38",=>(python lib contained in python libs)
"-lpython3",=>(python lib contained in python libs)
"-l_tkinter",=>(python lib contained in python libs)
"-o",=>(output argument specified by ide)
"${fileDirname}\\${fileBasenameNoExtension}.exe"
I hope that with this answer, the beginners like me, get to the conclusion faster. There is also a good youtube tutorial of Embedding python in C++ with CMake and Pybind11 and VS Code
Upvotes: 5