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.2.1.tar.gz (8.1 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.2.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for l0n0lc-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6ebf7d561562e8f6fd452a315033f41bbff3aae946ba11a0b670c1b7032db28c
MD5 ca300a540776df06b7781d34661c292e
BLAKE2b-256 e07454035e211c8b1eefe73b8f0c80cec651b1adf10b4b02add9668f847b95a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: l0n0lc-0.2.1-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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 47af083b68cfd002ff56c4a1dce1542df666c117450239328e74519631ffdd79
MD5 8d5c6bd7702374452688421e7f413aff
BLAKE2b-256 c03f763329821200e633c9ff778d0febdda35e5a85b50630f3f4c56cff174781

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