Calling libc

Objective: Call getpid from libc. Print PID returned by the function.

Python

from ctypes import cdll
libc = cdll.LoadLibrary("libc.so.6") 
print(libc.getpid())

Similar approach can be used for accessing other shared libraries.

Rust

Rust compiler comes with pre-prepared bindings for libc. As for now, this is only available in unstable versions of the compiler. Compile as:

rustc --edition 2024 -Z unstable-options calllibc.rs
#![feature(rustc_private)]
extern crate libc;
use libc::pid_t;

#[link(name = "c")]
extern {
    fn getpid() -> pid_t;
}

fn main() {
    let pid = unsafe { getpid() };
    println!("{}", pid);
}

Crystal

lib Libc
    alias PidT = Int32
    fun getpid : PidT
end

pid = Libc.getpid
puts(pid)

Similar approach can be used for accessing other shared libraries.