Article Index

Page 38

 hello.c

#include <stdio.h>

int main(int argc, char **argv)

{

    printf("Hello C World\n");

    return 0;

}

 tasks.json

{

    "tasks": [

        {

            "type": "cppbuild",

            "label": "C/C++: cl.exe build active file",

            "command": "cl.exe",

            "args": [

                "/Zi",

                "/EHsc",

                "/nologo",

                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",

                "${file}"

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$msCompile"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "detail": "Task generated by Debugger."

        }

    ],

    "version": "2.0.0"

}

Page 30

Change mike.james in the quoted paths to your own user name

arith.c

#define PY_SSIZE_T_CLEAN

#include <Python.h>

static PyObject *add(PyObject *self, PyObject *args)

{

    int x, y, sts;

    if (!PyArg_ParseTuple(args, "ii", &x, &y))

        return NULL;

    sts = x + y;

    return PyLong_FromLong(sts);

}

static PyMethodDef AddMethods[] = {

    {"add", add, METH_VARARGS, "add two numbers"},

    {NULL, NULL, 0, NULL} // sentinel

};

static struct PyModuleDef addmodule = {

    PyModuleDef_HEAD_INIT,

    "arith",

    "C library for sum",

    -1,

    AddMethods};

PyMODINIT_FUNC PyInit_arith(void)

{

    return PyModule_Create(&addmodule);

}

tasks.json

{

    "tasks": [

        {

            "type": "cppbuild",

            "label": "C/C++: cl.exe build active file",

            "command": "cl.exe",

            "args": [

                "/Zi",

                "/EHsc",

                "/nologo",            

                "/IC:/Users/mike.james/AppData/Local/Programs/Python/Python311/include",              

                "${file}",

                "/link /dll /OUT:arith.pyd /LIBPATH:C:/Users/mike.james/AppData/Local/Programs/Python/Python311/libs"

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$msCompile"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "detail": "Task generated by Debugger."

        }

    ],

    "version": "2.0.0"

}

 launch.json

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "(Windows) Attach",

            "type": "cppvsdbg",

            "request": "attach",

            "processId": "${command:pickProcess}",

            "symbolSearchPath": "C:/Users/mike.james/AppData/Local/Programs/Python/Python-3.11.4",

            "sourceFileMap":{"D:/a/1/s/":"C:/Users/mike.james/AppData/Local/Programs/Python/Python311"},

        },

        {

            "name": "Python: Current File",

            "type": "python",

            "request": "launch",

            "program": "${file}",

            "console": "integratedTerminal"

        }

    ]

}

c_cpp_properties.json

{

    "configurations": [

        {

            "name": "Win32",

            "includePath": [

                "${workspaceFolder}/**",

                "C:/Users/mike.james/AppData/Local/Programs/Python/Python311/include/"

            ],

            "defines": [

                "_DEBUG",

                "UNICODE",

                "_UNICODE"

            ],

            "windowsSdkVersion": "10.0.22000.0",

            "compilerPath": "cl.exe",

            "cStandard": "c17",

            "cppStandard": "c++17",

            "intelliSenseMode": "windows-msvc-x64"

        }

    ],

    "version": 4

}

test.py

import arith

print(arith.add(1,2))

print(dir(arith))

 

 Page 44

launch.json

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "(Windows) Attach",

            "type": "cppvsdbg",

            "request": "attach",

            "processId": "${command:pickProcess}",

        },

        {

            "name": "Python: Current File",

            "type": "python",

            "request": "launch",

            "program": "${file}",

            "console": "integratedTerminal"

        }

    ]

}