Last active
October 28, 2023 20:33
-
-
Save itdaniher/46fec3dd3b7eb603d7cbb5cd55fa5e1d to your computer and use it in GitHub Desktop.
compile python script to ELF on Linux via cython and gcc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import sys | |
import tempfile | |
from Cython.Compiler import Main, CmdLine, Options | |
in_file_name = sys.argv[1] | |
source = open(in_file_name).read() | |
out_file_name = in_file_name.replace('.py', '.out') | |
temp_py_file = tempfile.NamedTemporaryFile(suffix='.py', delete=False) | |
temp_py_file.write(source.encode()) | |
temp_py_file.flush() | |
Main.Options.embed = 'main' | |
res = Main.compile_single(temp_py_file.name, Main.CompilationOptions(), '') | |
gcc_cmd = 'gcc -fPIC -O2 %s -I/usr/include/python3.6 -L/usr/lib/python3.6 -lpython3.6m -o %s' % (res.c_file, out_file_name) | |
print(gcc_cmd) | |
assert 0 == subprocess.check_call(gcc_cmd.split(' ')) |
Nice Script, but i had to rename "lpython3.6m" to "lpython3.6"
Could you tell me what the function of "-lpython3.6m" is? "python3.6m" Is the name of a library? but I can't find it under /usr/lib/python3.6.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love your script. I've used it in https://edugit.org/jakobkir/telnet-chat/