1

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\""
    }
]

}

16
  • 1
    How are you building the code? c_cpp_properties.json only configures the intellisense Commented Dec 13, 2021 at 7:22
  • 1
    You need to modify tasks.json to tell the compiler itself how to build, and what flags, options and arguments should be used for the compiler. Commented Dec 13, 2021 at 7:23
  • @AlanBirtles I am following build steps from link Commented Dec 13, 2021 at 7:24
  • 1
    I suggest you find some documentation or tutorial about the -I, -L and -l options. You don't use them correctly. Commented Dec 13, 2021 at 7:53
  • 1
    Not to be discouraging, but "total beginner in C++" and "trying to embed Python" is not an ideal combination. Embedding Python is more at the "intermediate to advanced" level. Commented Dec 13, 2021 at 8:52

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

For future readers if your code is compiling fine, however you are still getting underline errors on vscode for windows using g++/mingw or similar you may also need to update the c_cpp_properties.json file by adding the Include path to Intellisense. do the below "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/msys64/mingw64/include/python3.10"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.