最近一个项目用lua5.1.4和c++联合开发。在开发的开始阶段遇到了一点问题,问题如下:
用命令行运行:lua test.lua
lua: error loading modules 'dllforlua' from file './dllforlua.dll':
找不到指定的程序。
因为是第一次使用,这个问题困扰了我好几天。经过多番测试,发现如下规律:
如果用require调用的时候,C动态库的文件名必须要与导出函数保持一致。
例如:动态库名字:testlua.dll,那么导出函数必须如下:
extern "C"
LUALIB_API int luaopen_testlua(lua_State *L) {
lua_register(L, "average", lua_average);
return 0;
}
另外一种解决方法,用package.loadlib(绝对路径.dll, "导出函数名")
然后调用这个导出函数
dllentry = package.loadlib("D:\\myprogram\\Lua\\firstC\\Release\\luatest.dll", "luaopen_suibian")
dllentry()
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)
说明:luatest.dll是动态库的名字,luaopen_suibian是导出函数名,2者可以不一致。
建议用第一种方式。 ......
在Notes 的C/S 编程中,经常需要对一个集合中的对象进行遍历处理,这时如果能显示一个进度条指示当前处理进程,就比较直观形象,客户界面也比较友好。
1、声明API函数
'声明API函数
Declare Function NEMProgressBegin Lib "nnotesws.dll"
( Byval wFlags As Integer ) As Long
Declare Sub NEMProgressEnd Lib "nnotesws.dll"
( Byval hwnd As Long )
Declare Sub NEMProgressSetBarPos Lib "nnotesws.dll"
( Byval hwnd As Long, Byval dwPos As Long)
Declare Sub NEMProgressSetBarRange Lib "nnotesws.dll"
(&nb ......
时常在cpp的代码之中看到这样的代码:
#ifdef __cplusplus
extern "C" {
#endif
//一段代码
#ifdef __cplusplus
}
#endif
这样的代码到底是什么意思呢?首先,__cplusplus是cpp中的自定义宏,那么定义了这个宏的话表示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入extern "C"{和}处理其中的代码。
要明白为何使用extern "C",还得从cpp中对函数的重载处理开始说起。在c++中,为了支持重载机制,在编译生成的汇编码中,要对函数的名字进行一些处理,加入比如函数的返回类型等等.而在C中,只是简单的函数名字而已,不会加入其他的信息.也就是说:C++和C对产生的函数名字的处理是不一样的.
比如下面的一段简单的函数,我们看看加入和不加入extern "C"产生的汇编代码都有哪些变化:
int f(void)
{
return 1;
}
在加入extern "C"的时候产生的汇编代码是:
.file "test.cxx"
.text
.align 2
.glob ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. fclose函数
fclose函数的功能是关闭一个流,其用法是:int fclose(FILE *stream); 程序例子如下:
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *fp;
char buf[11] = "0123456789";
/* create a file containing 10 bytes */
fp = fopen("DUMMY.FIL", "w");
fwrite(&buf, strlen(buf), 1, fp);
/* close the file */
fclose(fp);
return 0;
}
2. fcloseall函数
fcloseall函数的功能是关闭打开流,其用法是:int fcloseall(void); 程序例子如下:
#include <stdio.h>
int main(void)
{
int streams_closed;
/* open two streams */
fopen("DUMMY.ONE", "w");
fopen("DUMMY.TWO", "w");
......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. fclose函数
fclose函数的功能是关闭一个流,其用法是:int fclose(FILE *stream); 程序例子如下:
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *fp;
char buf[11] = "0123456789";
/* create a file containing 10 bytes */
fp = fopen("DUMMY.FIL", "w");
fwrite(&buf, strlen(buf), 1, fp);
/* close the file */
fclose(fp);
return 0;
}
2. fcloseall函数
fcloseall函数的功能是关闭打开流,其用法是:int fcloseall(void); 程序例子如下:
#include <stdio.h>
int main(void)
{
int streams_closed;
/* open two streams */
fopen("DUMMY.ONE", "w");
fopen("DUMMY.TWO", "w");
......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. fgetc函数
fgetc函数的功能是从流中读取字符,其用法是:int fgetc(FILE *stream); 程序例子如下:
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char ch;
/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");
/* write a string into the file */
fwrite(string, strlen(string), 1, stream);
/* seek to the beginning of the file */
fseek(stream, 0, SEEK_SET);
do
{
/* read a char from the file */
ch = fgetc(stream);
/* display the character */
&nb ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. fgetc函数
fgetc函数的功能是从流中读取字符,其用法是:int fgetc(FILE *stream); 程序例子如下:
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char ch;
/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");
/* write a string into the file */
fwrite(string, strlen(string), 1, stream);
/* seek to the beginning of the file */
fseek(stream, 0, SEEK_SET);
do
{
/* read a char from the file */
ch = fgetc(stream);
/* display the character */
&nb ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. gcvt函数
gcvt函数的功能是把浮点数转换成字符串,其用法是:char *gcvt(double value, int ndigit, char *buf);程序例子如下:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char str[25];
double num;
int sig = 5; /* significant digits */
/* a regular number */
num = 9.876;
gcvt(num, sig, str);
printf("string = %s\n", str);
/* a negative number */
num = -123.4567;
gcvt(num, sig, str);
printf("string = %s\n", str);
/* scientific notation */
num = 0.678e5;
gcvt(num, sig, str);
printf("string = %s\n", str);
return(0);
}
2. getcr函数
......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. gcvt函数
gcvt函数的功能是把浮点数转换成字符串,其用法是:char *gcvt(double value, int ndigit, char *buf);程序例子如下:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char str[25];
double num;
int sig = 5; /* significant digits */
/* a regular number */
num = 9.876;
gcvt(num, sig, str);
printf("string = %s\n", str);
/* a negative number */
num = -123.4567;
gcvt(num, sig, str);
printf("string = %s\n", str);
/* scientific notation */
num = 0.678e5;
gcvt(num, sig, str);
printf("string = %s\n", str);
return(0);
}
2. getcr函数
......