独创函数YC_cppCompile()
该函数将c/c++源代码编译转换为执行代码。
int YC_cppCompile(
char **pExebuf=NULL, //执行代码缓冲区地址指针
const char *srcbuf=NULL, //源代码文件名或源代码缓冲区
int srclen=0, //源代码长度(用于源代码位于缓冲区的情形)
const char *cppfile=NULL //源码文件名(用于源代码位于缓冲区的情形)
);
参数:
pExebuf
当pExebuf不等于NULL时,函数将申请内存,内存地址放入*pExebuf,生成的执行代码存入该内存。如果源代码有错误,则将以0结尾的语法错误文本放入该内存。当pExebuf等于NULL时,只打印行版权信息。应用程序必须用free()函数来释放该内存。
srcbuf, srclen
当srclen等于0时,srcbuf是源代码的文件名,当srclen大于0时,srcbuf是源代码缓冲区,srclen则是源代码的长度。
cppfile
该参数只有当srclen>0时使用,表示源代码文件名,该参数可设为NULL。
返回值:
如果函数运行成功,返回值大于0,表示执行代码长度,pExebuf中存放执行代码。
如果函数运行失败,返回值为0,pExebuf中存放以\0结尾的源代码语法错误文本。
注意:
无论函数运行成功还是失败,应用程序都必须用语句free(*pExebuf)释放函数内部
申请的内存(*pExebuf)。
1. 用YC_cppCompile()编译缓冲区C/C++代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "yc.h"
void main()
{
char *srcbuf = "void main(int argc,char **argv[]) \
{ \
printf(`编译缓冲区源代码.\n\n`); \
printf(`argc = %d\n`,argc); \
for(int ii=0; ii<argc; ii++) printf(`%10s\n`,argv[ii]); \
}";
char *exebuf;
int glen = YC_cppCompile(&exebuf,srcbuf,strlen(srcbuf),__FILE__); //编译源码
if(glen) //缓冲区源代码srcbuf被编译成功
{
YC_writefile("p00.exe",exebuf,glen); //将编译结果存入文件 p00.exe
printf("p00.exe 已创建.\n");
}
else //编译失败
{
remove("p00.exe"); //源代码srcbuf有错误,删除p00.exe
printf("%s\n",exebuf);
}
free(exebuf); //必须由应用程序释放编译结果所占内存
char *argv[] = { "横看成岭侧成峰","远近高低各不同",
"不识庐山真面目","只缘身在此山中",}; //设置形参之值
int argc = sizeof argv/sizeof argv[0]; //设置形参个数
//执行p00.exe。其参数srclen=-1: 用system()执行,0:用WinExec()执行。
YC_cppRun("p00.exe 沙艳娜",-1,argc,argv);
}
代码文件book.cpp
编译:用YC命令:ycc book.cpp 或 VC++命令:cl book.cpp 生成 book.exe。
运行:在cmd界面执行book.exe后,输出下列文字:
book.cpp
book.cpp - compiled OK.
p00.exe 已创建.
编译缓冲区源代码.
argc = 6
p00.exe
王安石
横看成岭侧成峰
远近高低各不同
不识庐山真面目
只缘身在此山中
执行p00 one two后,输出下列文字:
编译缓冲区源代码.
argc = 3
p00
one
two
2. 用YC_cppCompile()编译C/C++文件
void main(int argc,char *argv[])
{
for(int ii=0; ii<argc; ii++) printf("argv[%d] = %s\n",ii,argv[ii]);
}
c/c++文件: ran.cpp
#include <stdio.h>
#include <stdlib.h>
#include "ycapi.h"
void main()
{
char *exebuf;
int glen = YC_cppCompile(&exebuf,"ran.cpp");
if(glen) //ran.cpp被编译成功
{
YC_writefile("p01.exe",exebuf,glen); //将编译结果存入文件 p01.exe
printf("p01.exe has been created.\n\n");
}
else //编译失败
{
remove("p01.exe");
printf("%s\n",exebuf); //打印错误信息
}
free(exebuf); //必须由应用程序释放编译结果所占内存
char *argv[] = {"好雨知时节,","当春乃发生。", //设置形参之值
"随风潜入夜,","润物细无声。" };
int argc = sizeof(argv) / sizeof(argv[0]); //设置形参个数
YC_cppRun("p01.exe",-1,argc,argv); //-1: DOS方式运行exe文件
}
c/c++文件: story.cpp
该程序将ran.cpp文件被编译生成的执行代码存入p01.exe。
编译:用YC命令:ycc story.cpp 或 VC++命令:cl story.cpp 生成 story.exe。
运行:在cmd界面执行story.exe后,输出下列文字:
ran.cpp
ran.cpp - compiled OK.
p01.exe has been created.
argv[0] = p01.exe
argv[1] = 好雨知时节,
argv[2] = 当春乃发生。
argv[3] = 随风潜入夜,
argv[4] = 润物细无声。
运行:在cmd界面执行p01 one two 后,输出下列文字:
argv[0] = p01
argv[1] = one
argv[2] = two
3. 使用YC_cppCompile()函数的文件
ycmk.cpp
int exelen = YC_cppCompile(&exebuf,cppfile);
ycc.cpp
int exelen = YC_cppCompile(&exebuf,srcbuf,glen,cppfile);
yced.cpp
int exe_len = YC_cppCompile(&exe_buf,…);
ycjs.cpp
exe_len = YC_cppCompile(&exe_ptr,fileName);
ycos.cpp
int boot_len = YC_cppCompile(&boot_buf,“ycboot.cpp”);
int head_len = YC_cppCompile(&head_buf,“ychead.cpp”);
int ker_len = YC_cppCompile(&ker_buf,“ycker.cpp”);
int fs_len = YC_cppCompile(&fs_buf,“ycfs.cpp”);
int mm_len = YC_cppCompile(&mm_buf,“ycmm.cpp”);