Home > php教程 > php手册 > body text

记第一次lua和C互相调用的例子

WBOY
Release: 2016-06-13 08:43:08
Original
1047 people have browsed it

记第一次lua和C互相调用的例子

(以下的内容参考别人的例子,特此声明)
在写程序之前,需要安装lua的环境
在终端输入命令:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>sudo apt-get install lua5.1<br /> </li><li>sudo apt-get install lua5.1-0-dev(注意:安装这个东西是为了lua的一些头文件和库文件路径使用) </li></ol>
Copy after login
安装好之后,输入lua -v,查看lua版本:

建议:输入ln -s lua5.1 lua,做一个软链接,以后运行lua的时候,就会运行lua5.1
第一部分:C语言调用lua中的函数(ubuntu)
1.先写个add.c文件

2.再写一个我们将要调用的lua函数(add.lua)

3.最后,使用命令:gcc -o add add.c -I /usr/include/lua5.1 -llua5.1,生成add可执行文件,如图:

第二部分:lua调用C语言的函数(ubuntu)
1.写一个hello.lua文件

2.再写一个power_lua.c文件

3.运行命令:gcc-Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 power_lua.c
在当前目录中会看到power.so文件

4.最后,激动人心的时刻到了!
运行:lua hello.lua,结果如下:

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!