1

I am running Python within C, and I can't seem to get the program to compile. I have the following included in the program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include <Python.h>
#include <arrayobject.h>

I am working in VS Code and I have the following tasks.json file:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-I/usr/include/**",
                "-I~/anaconda3/envs/myenv/include/python3.9/**",
                "-I~/anaconda3/envs/myenv/lib/python3.9/site-packages/numpy/core/include/numpy/**",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

When I compile, I get an error:

Starting build...
/usr/bin/gcc -fdiagnostics-color=always -I/usr/include/** -I~/anaconda3/envs/myenv/include/python3.9/** -I~/anaconda3/envs/myenv/lib/python3.9/site-packages/numpy/core/include/numpy/** -g ~/Documents/code/test.c -o ~/Documents/code/test
~/Documents/code/test.c:6:10: fatal error: Python.h: No such file or directory
    6 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.

Build finished with error(s).

I thought that with the -I statements, I'd included everything necessary to run this. What is the best way to include the necessary files? By the way, I also get errors with #include <glib.h>, which should be included with -I/usr/include/**. I'm pretty new to C so I apologize if this is an obvious question.

10
  • 1
    What shell are you using? -I/usr/include/** should perhaps be -I /usr/include/** but you should have -I infront of all directories. Commented Jun 13, 2022 at 15:45
  • 2
    What do you get with find /usr/include/ |grep -i python.h ? Commented Jun 13, 2022 at 15:47
  • 1
    And find ~/anaconda3/envs/myenv/include/python3.9 -type f | grep -i python.h ? Commented Jun 13, 2022 at 15:52
  • 1
    the command find ~/anaconda3/envs/metagenomics/include/python3.9/ -type f | grep -i python.h yields a result: ~anaconda3/envs/myenv/include/python3.9/Python.h Commented Jun 13, 2022 at 15:54
  • 2
    That tells you where you need your -I to point to. Commented Jun 13, 2022 at 16:08

2 Answers 2

1

I would suggest using pkg-config

"args": [
  "pkg-config --cflags python3",
}
Sign up to request clarification or add additional context in comments.

6 Comments

How should I tie this to my anaconda virtual environment?
Does Anaconda set PKG_CONFIG_PATH?
I'm not familiar with Anaconda, but you can check where pkg-config searches for installed libraries pkg-config --variable pc_path pkg-config and if you want to set value export PKG_CONFIG_PATH=/my/path should do.
Can you use it just like that? Will it actually execute pkg-config when you list it in args? I would expect something like $(pkg-config --cflags --libs python3) to pass it on to the shell ... but, even that feels a bit fragile.
@TedLyngmo Good point. Probably adding backticks should do the job as well.
|
0

I am posting this as an answer, though credit goes to the several contributors here. The trick is to include each folder you need individually. So my new tasks.json file is:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [

                "-fdiagnostics-color=always",
                "-I/usr/include/glib-2.0",
                "-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
                "-I$HOME/anaconda3/envs/myenv/include/python3.9",
                "-I$HOME/anaconda3/envs/myenv/lib/python3.9/site-packages/numpy/core/include/numpy",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Comments

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.