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

Uploaded Python 3

File details

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

File metadata

  • Download URL: l0n0lc-0.1.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.1.1.tar.gz
Algorithm Hash digest
SHA256 cb1baf95e82cd9e40958adf01618af025b38cf2a884055f24a7e738dece9f306
MD5 b0c17c8cc45b92bed818d06a5aaf340a
BLAKE2b-256 4a61c8b04b25fcd6d50d4319a492117883f84180847a194e1e808d45d84f864c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: l0n0lc-0.1.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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef3d7452472aca6eabfddbe1b3e47449f642d096758fc6344c6e5a95ad9c53fe
MD5 4b080592af7e94251f30690bf2e4210f
BLAKE2b-256 392ec981a018ba0ea50c138e3f0c89afc46fb4aa11013656413a553d997cea7d

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