一般编写的C模块都要开发出接口,提供给其他模块使用。
自己写了一个mcm的程序,轻松一下。
mcm.c内容如下:
// import interface.
#include "mcm.h"
static char g_cbuf_InterfaceFile[] = "#ifndef %s\r\n"
"#define %s\r\n\r\n"
"// import interface.\r\n\r\n\r\n"
"// export interface.\r\n"
"#ifdef __cplusplus\r\n"
"extern \"C\"\r\n{\r\n"
"#endif\r\n\r\n"
"\r\n"
"#ifdef __cplusplus\r\n"
"}\r\n"
"#endif\r\n\r\n"
"#endif\r\n";
static char g_cbuf_SourceFile[] = "// import interface.\r\n"
"#include \"%s\"";
#define buf_size 512
int main(int i_argc, char* pcbuf_argv[])
{
if (i_argc != 2)
{
printf("\"make c module\" or \"mcm\" create a simple c module frame source file,\r\n"
"and it's very simple! author: 2524322562@qq.com.\r\n\r\n");
printf("usage: %s module name\r\n", pcbuf_argv[0]);
return 0;
}
{
FILE* InterfaceFile = NULL; // c header file.
FILE* SourceFile = NULL; // c source file.
char cbuf_check1[buf_size], cbuf_check2[buf_size];
sprintf(cbuf_check1, "%s.h", pcbuf_argv[1]);
sprintf(cbuf_check2, "%s.c", pcbuf_argv[1]);
if (NULL == (InterfaceFile = fopen(cbuf_check1, "r")) && NULL == (SourceFile = fopen(cbuf_check2, "r")))
{
// file don't exists, create new file.
char cbuf_tmp[buf_size];
InterfaceFile = fopen(cbuf_check1, "wb");
SourceFile = fopen(cbuf_check2, "wb");
{
char cbuf_module[buf_size];
sprintf(cbuf_module, "%s_h", pcbuf_argv[1]);
sprintf(cbuf_tmp, g_cbuf_InterfaceFile, cbuf_module, cbuf_module);
fwrite(cbuf_tmp, strlen(cbuf_tmp), 1, InterfaceFile);
sprintf(cbuf_module, "%s.h", pcbuf_argv[1]);
sprintf(cbuf_tmp, g_cbuf_SourceFile, cbuf_module);
fwrite(cbuf_tmp, strlen(cbuf_tmp), 1, SourceFile);
fclose(InterfaceFile);
fclose(SourceFile);
}
}
else
{
printf("c module \"%s\" file already exists.\r\n", pcbuf_argv[1]);
if (InterfaceFile != NULL)
{
fclose(InterfaceFile);
}
if (SourceFile != NULL)
{
fclose(SourceFile);
}
return -1;
}
}
return 0;
}
mcm.h文件内容如下:
#ifndef mcm_h
#define mcm_h
// import interface.
#include <stdio.h>
#include <assert.h>
// export interface.
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
#endif
编译脚本bd.cmd如下:
@echo off
echo + batch script for windows sdk(c version).
echo + usage : type "bd" or "bd NDEBUG" or "bd clean".
echo + "bd" : build debug version.
echo + "bd NDEBUG": build release version.
echo + "bd clean" : delete result files.
if "%1" equ "clean" goto clean
if "%1" neq "" (if "%1" equ "NDEBUG" (echo + & echo + build release version. & echo + & goto release) else (echo + & echo + invalidate parameter. & echo + & goto end)) else (echo + & echo + build debug version. & echo + & goto debug)
:clean
echo + & echo + delete result files. & echo + & del *.obj & del *.exe & del *.res & del *.exp & del *.lib & del *.dll & del *.manifest
goto end
:release
cl /MD /D%1 mcm.c
goto end
:debug
cl /MDd mcm.c
goto end
:end
if exist mcm.exe.manifest mt -manifest mcm.exe.manifest -outputresource:mcm.exe
del *.manifest
D:\bd\inst\bs\mcm>dir
驱动器 D 中的卷是 program
卷的序列号是 0433-59C8
D:\bd\inst\bs\mcm 的目录
2011-11-24 16:39 <DIR> .
2011-11-24 16:39 <DIR> ..
2011-11-10 09:24 859 bd.cmd
2011-11-24 16:35 2,365 mcm.c
2011-11-24 16:09 207 mcm.h
3 个文件 3,431 字节
2 个目录 60,737,548,288 可用字节
D:\bd\inst\bs\mcm>bd NDEBUG
+ batch script for windows sdk(c version).
+ usage : type "bd" or "bd NDEBUG" or "bd clean".
+ "bd" : build debug version.
+ "bd NDEBUG": build release version.
+ "bd clean" : delete result files.
+
+ build release version.
+
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
mcm.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
/out:mcm.exe
mcm.obj
Microsoft (R) Manifest Tool version 5.2.3790.2075
Copyright (c) Microsoft Corporation 2005.
All rights reserved.
D:\bd\inst\bs\mcm>mcm
"make c module" or "mcm" create a simple c module frame source file,
and it's very simple! author: 2524322562@qq.com.
usage: mcm module name
D:\bd\inst\bs\mcm>mcm test
D:\bd\inst\bs\mcm>more test.h
#ifndef test_h
#define test_h
// import interface.
// export interface.
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
#endif
D:\bd\inst\bs\mcm>more test.c
// import interface.
#include "test.h"
D:\bd\inst\bs\mcm>
源码打包在CSDN资源频道:
http://download.csdn.net/detail/qs_coding/3837607