I’m trying to compile c code using the python API.
main.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int
main(int argc, char *argv[])
{
PyStatus status;
PyConfig config;
PyConfig_InitPythonConfig(&config);
status = PyConfig_SetBytesString(&config, &config.program_name, argv[0]);
if (PyStatus_Exception(status)) {
goto exception;
}
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
goto exception;
}
PyConfig_Clear(&config);
PyRun_SimpleString(
"import sys\n"
"from time import time,ctime\n"
"print('Today is', ctime(time()))\n"
);
if (Py_FinalizeEx() < 0) {
exit(120);
}
return 0;
exception:
PyConfig_Clear(&config);
Py_ExitStatusException(status);
}
I decided to use gcc for this along with flake
flake.nix
{
description = "A very basic python c api";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
in {
packages.${system}.default =
pkgs.stdenv.mkDerivation {
name = "hello_python";
src = self;
nativeBuildInputs= with pkgs; [
python313
gcc
];
buildPhase = ''
gcc main.c -o hello_python
'';
installPhase = ''
mkdir -p $out/bin;
install -t $out/bin hello_python
'';
NIX_CFLAGS_COMPILE = [
"-I${pkgs.python313.outPath}/include/${pkgs.python313.executable}"
];
NIX_LDFLAGS = [
"-L${pkgs.python313.outPath}/lib"
];
};
};
}
$ tree
.
├── flake.lock
├── flake.nix
└── main.c
But when I tried to run it, I got a liker error
$ nix run .
error: Cannot build '/nix/store/q0l1ad8a2r0li5w2prhz8kxxvkdhj77q-hello_python.drv'.
Reason: builder failed with exit code 1.
Output paths:
/nix/store/9vbjswb7q3n8csyf428fzdr6yzsk4vxw-hello_python
Last 20 log lines:
> Running phase: unpackPhase
> unpacking source archive /nix/store/navqf7pq5f08byh7kmbq0f84lpm7l7sd-source
> source root is source
> Running phase: patchPhase
> Running phase: updateAutotoolsGnuConfigScriptsPhase
> Running phase: configurePhase
> no configure script, doing nothing
> Running phase: buildPhase
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: /build/ccimUTjq.o: in function `main':
> main.c:(.text.startup+0x32): undefined reference to `PyConfig_InitPythonConfig'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0x48): undefined reference to `PyConfig_SetBytesString'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0x6a): undefined reference to `PyStatus_Exception'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0x7d): undefined reference to `Py_InitializeFromConfig'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0x9f): undefined reference to `PyStatus_Exception'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0xaf): undefined reference to `PyConfig_Clear'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0xbd): undefined reference to `PyRun_SimpleStringFlags'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0xc2): undefined reference to `Py_FinalizeEx'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0xea): undefined reference to `PyConfig_Clear'
> /nix/store/dc9vaz50jg7mibk9xvqw5dqv89cxzla3-binutils-2.44/bin/ld: main.c:(.text.startup+0x10c): undefined reference to `Py_ExitStatusException'
> collect2: error: ld returned 1 exit status
For full logs, run:
nix log /nix/store/q0l1ad8a2r0li5w2prhz8kxxvkdhj77q-hello_python.drv
We are looking for an answer in Python - Official NixOS Wiki , C - NixOS Wiki and Cannot link against Python but I didn’t find anything.
Can someone help?