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.1.2.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.1.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: l0n0lc-0.1.2.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.1.2.tar.gz
Algorithm Hash digest
SHA256 f75591baa5bdacff35454b551d9de8965a623b802da336c8c904078c15a9dc1e
MD5 3df9e8e50be7e8d7e89993666f119da9
BLAKE2b-256 bfc277184e253b1ce02812aa2a13df5e5574c6483bc052aaa06d918276b0e351

See more details on using hashes here.

File details

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

File metadata

  • Download URL: l0n0lc-0.1.2-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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2a94bb0eb1d23b7d64cae1b1dab073ff98234290cd3e50af605f7990b11c504b
MD5 e74a4ad43e34f7ebf512f2ea0a26e2de
BLAKE2b-256 4a872d407e7ce06e17efdab0c50969c05cc4d306d53586e36f743802f91e8044

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