Skip to main content

用python写c

Project description

用python写c语言

import l0n0lc as lc
import os
import ctypes

if not os.path.exists('./l0n0lcoutput'):
    os.mkdir('l0n0lcoutput')


fp = open('l0n0lcoutput/helloworld.cpp', 'w')
lc.配置输出函数(fp.write)
lc.c_include('stdio.h')
lc.c_include('stdlib.h')

with lc.如果没有定义('hahaha'):
    lc.c_define('hahaha', '2')


@lc.c_函数(前缀=['extern "C"'])
def test_add(a: lc.c_i32, b: lc.c_i32) -> lc.c_i32:
    with lc.如果(a.大于(100)) as f:
        a(10)
    with lc.否则如果(a.等于(10)):
        a(100)
    with lc.否则():
        a(1000)
    return a + b # type: ignore

@lc.c_函数()
def main(argc: lc.c_i32, argv: lc.c_char_p_p) -> lc.c_i32:
    c =lc.c_i32(test_add(1, 2))
    lc.输出(f'printf("Hello World! c = %d\\n", {c});\n')
    lc.输出('printf("Hello World!\\n");\n')
    with lc.如果定义了('hahaha'):
        lc.输出('printf("哈哈哈!\\n");\n')
    with lc.宏如果('hahaha == 1') as c:
        lc.输出('printf("111!\\n");\n')
        c.否则如果('hahaha == 2')
        lc.输出('printf("否则2!\\n");\n')
        c.否则()
        lc.输出('printf("否则!\\n");\n')
    i = lc.c_i32(0)
    with lc.循环(i.小于(argc)):
        lc.输出(f'printf("i = %d, v = %s\\n", {i}, {argv[i]});\n')
        i += 1
    return 0  # type: ignore


fp.close()

cm = lc.编译()
cm.编译文件('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/helloworld')
os.system('./l0n0lcoutput/helloworld 1 2')

cm.编译为动态库('l0n0lcoutput/helloworld.cpp', 'l0n0lcoutput/libhelloworld.so')
path = f'{os.getcwd()}/l0n0lcoutput/libhelloworld.so'
print(path)
libhelloworld = ctypes.CDLL(path)
libhelloworld.test_add.argtypes=[ctypes.c_int32, ctypes.c_int32]
libhelloworld.test_add.restype = ctypes.c_int32
a = libhelloworld.test_add(300, 6)
print(a)
a = libhelloworld.test_add(10, 6)
print(a)
a = libhelloworld.test_add(1, 6)
print(a)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

l0n0lc-0.3.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

l0n0lc-0.3.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file l0n0lc-0.3.0.tar.gz.

File metadata

  • Download URL: l0n0lc-0.3.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for l0n0lc-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9040cc15c7caf3808593b0039d30035a9cffa0674cb8818d8b3d0c535202cd1b
MD5 cdd114f01ed1415c3d6bef8d52492c63
BLAKE2b-256 9909a11629c14bd386a06762cdc54a5aa740182d3a0a951b057c7d6d42ebb05f

See more details on using hashes here.

File details

Details for the file l0n0lc-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: l0n0lc-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for l0n0lc-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4ee08c3992d88ca93c9e2915dfa1a987d9dca70401858bdf4c9e339c6a1268c
MD5 a1b20e1164308de624ee06fe980cc477
BLAKE2b-256 cf6c1adef20ced5fc9af9f15246c9f4e2e1f23f808e21d829338cd252fc3aa36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page