V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yhhy168
V2EX  ›  Lua

lua 调用 c 的例子 编译时出错

  •  
  •   yhhy168 · 2013-05-29 22:41:22 +08:00 · 4588 次点击
    这是一个创建于 3956 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近在学 lua,今天在网上找了个 lua 调用 C 的例子学习,例子的代码如下:


    #include <math.h>
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"


    static int l_sin (lua_State *L)
    {
    double d = luaL_checknumber(L, 1);
    lua_pushnumber(L, sin(d));
    return 1;
    }

    static const struct luaL_reg mylib [] = {
    {"lsin", l_sin},
    {NULL, NULL}
    };

    int luaopen_mylib(lua_State *L)
    {
    luaL_openlib(L, "mylib1", mylib, 0);
    return 1;
    }


    编译时报“mylib.c:14: 错误:数组元素的类型不完全”的错,提示“static const struct luaL_reg mylib [] = {” 这一行的代码出错,网上找了下说 GCC 4.0 以上版本,结构要先定义再用。不过本人 C 盲,想问下这个错该怎样改呢,先多谢了。

    测试环境:
    Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio
    gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
    3 条回复    1970-01-01 08:00:00 +08:00
    xatest
        1
    xatest  
       2013-05-29 22:53:00 +08:00
    这是C调用Lua,不是Lua调用C吧〜
    static const struct luaL_reg mylib [] = {
    改成
    static const luaL_reg mylib [] = {
    因为luaL_reg本来就是struct。
    yhhy168
        2
    yhhy168  
    OP
       2013-05-29 23:50:46 +08:00
    @xatest
    改了也没编译通过,报另外一个错了:


    mylib.c:14: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mylib’
    mylib.c: 在函数‘luaopen_mylib’中:
    mylib.c:21: 错误:‘mylib’未声明(在此函数内第一次使用)
    mylib.c:21: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
    mylib.c:21: 错误:所在的函数内也只报告一次。)


    我是这样编译的:
    gcc mylib.c -fPIC -shared -o libmylib.so
    yhhy168
        3
    yhhy168  
    OP
       2013-05-30 15:19:22 +08:00
    自己顶下~
    没有人遇到这种情况吗?
    不懂 C 是要学得坎坷些了:(
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2823 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 13:37 · PVG 21:37 · LAX 06:37 · JFK 09:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.