什么是 cppsp?
发布: (2025年12月30日 GMT+8 16:38)
3 min read
原文: Dev.to
Source: Dev.to
要求
- 已安装 C++ 编译器,并将其文件夹加入系统 PATH(环境变量)。
- 需要 64 位 C++ 编译器(用于生成可执行文件)。
- (可选) 将生成的
exe/elf/mach‑o所在文件夹加入 PATH。 - 删除
_mac/_linux目录,以确保cppsp_compiler正常工作。 - (可选) 将
cppsp_compiler.exe(或cppsp_compiler)重命名为任意名称,以更改编译指令(例如cppsp、abcdef,……)。
安装
Windows
curl -L -o cppsp_compiler.exe https://github.com/user19870/cppsp/raw/refs/heads/First/cppsp_compiler.exe
Linux
curl -L -o cppsp_compiler https://github.com/user19870/cppsp/raw/refs/heads/First/cppsp_compiler_linux.delete_linux
macOS
curl -L -o cppsp_compiler https://github.com/user19870/cppsp/raw/refs/heads/First/cppsp_compiler_mac.delete_mac
使用方法
在命令行编译 .cppsp 脚本:
# 如果编译器已在 PATH 中
cppsp_compiler script.cppsp
# 如果不在 PATH 中,使用相对或绝对路径
.\cppsp_compiler.exe script.cppsp
# 或
c:\path\to\cppsp_compiler.exe script.cppsp
设置 C++ 包含 / 库文件夹
在编译器所在目录下创建两个 .ini 文件:
-
include.ini – 以逗号分隔的包含目录列表
C:\path\to\include1,C:\path\to\include2 -
lib.ini – 以逗号分隔的库目录列表
C:\path\to\lib1,C:\path\to\lib2
功能特性
- 能编译仅包含
print("hello world")的脚本。 - 通过
import几乎可以导入任意 C++ 头文件。 - 使用
@inject与@function嵌入原始 C++ 代码。 - 支持缩进和多行语句(自 1.3 版起可用)。
关键字
| 关键字 | 描述 |
|---|---|
#useclang / #usegcc | 选择 clang++ 或 g++ 作为底层编译器。 |
@command("…") | 向编译指令追加自定义参数(例如 -Os、-m64)。 |
#overwrite | 替换默认编译指令。例如:@command("g++ -Os -m64 -nostdlib -shared -o dll.dll dll.cpp")。 |
import | 导入 C++ 头文件,例如 import iostream,cstdio,vector。 |
@function> | 在 int main() 之前注入任意 C++ 代码(函数、using 语句、#define 等)。 |
@inject(…) | 将代码直接插入 int main(){…} 中。 |
print() | 向控制台输出值,例如 print("12\n", " ", 1, " ", 2.1, true, false)。 |
input() | 从控制台读取值到变量(变量必须通过 @inject 声明)。 |
// | 单行注释。 |
警告 ⚠️
- v1.2 之前:关键字前不允许出现空格或空白字符。
- v1.3 之前:不支持多行语句。
@command()本身不能跨多行书写;如有需要请使用多个@command()调用。
示例
@command("-mtune=native -fomit-frame-pointer -static-libgcc -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--as-needed -s -Wl,--strip-all -Os -m64")
import iostream,vector
@function>
print("12\n"," ",1," ",2.1,true,false," ")
print("abc")
print(1,"\n") // comment
//print(1.1)
@inject(int x=1; int y=2; int z=3;
auto is_bool = [](const std::string& s){ return s == "true" || s == "false"; };)
input(x,y,z)
@function cars = {"Volvo","BMW","Ford","Mazda"}; };>>
print(x+y+z)
简单 DLL 示例
#overwrite
@command("g++ -Os -m64 -nostdlib -shared -o dll.dll dll.cpp")
@function>