使用Visual Studio的Lua模块

时间:2014-02-18 22:34:29

标签: lua visual-studio-2013

我正在尝试使用Visual Studio 2013创建一个Lua模块,主要的ccp是here和module.hpp,另一个是here

我更改了主ccp以删除不需要的包含文件和两个不需要的函数,所以我得到了这个:

#include <lua.hpp>
#include <GeoIP.h>
#include "module.hpp"

static GeoIP * geoip = NULL;

static int load_geoip_database(lua_State * L)
{
    const char * filename = luaL_checkstring(L, 1);
    if (geoip) GeoIP_delete(geoip);
    geoip = GeoIP_open(filename, GEOIP_MEMORY_CACHE);
    lua_pushboolean(L, geoip != NULL);
    return 1;
}

static int ip_to_country(lua_State * L)
{
    if (!geoip) return luaL_error(L, "missing GeoIP database");
    const char * ipaddr = luaL_checkstring(L, 1);
    const char * country = GeoIP_country_name_by_addr(geoip, ipaddr);
    lua_pushstring(L, (country ? country : ""));
    return 1;
}

static int ip_to_country_code(lua_State * L)
{
    if (!geoip) return luaL_error(L, "missing GeoIP database");
    const char * ipaddr = luaL_checkstring(L, 1);
    const char * code = GeoIP_country_code_by_addr(geoip, ipaddr);
    lua_pushstring(L, (code ? code : ""));
    return 1;
}

static int shutdown_geoip(lua_State * L)
{
    GeoIP_delete(geoip);
    geoip = NULL;
    return 0;
}

namespace lua{
    namespace module{

        void open_geoip(lua_State * L)
        {
            static luaL_Reg functions[] = {
                { "load_geoip_database", load_geoip_database },
                { "ip_to_country", ip_to_country },
                { "ip_to_country_code", ip_to_country_code },
                { NULL, NULL }
            };

            luaL_register(L, "geoip", functions);
            lua_pop(L, 1);

            lua::on_shutdown(L, shutdown_geoip);
        }

} //namespace module
} //namespace lua

Visual Studio引发了以下错误:

1>------ Build started: Project: GeoIP, Configuration: Release Win32 ------
1>  Main.cpp
1>Main.obj : error LNK2001: unresolved external symbol _GeoIP_delete
1>Main.obj : error LNK2001: unresolved external symbol _luaL_checklstring
1>Main.obj : error LNK2001: unresolved external symbol _luaL_register
1>Main.obj : error LNK2001: unresolved external symbol _lua_pushstring
1>Main.obj : error LNK2001: unresolved external symbol _GeoIP_country_name_by_addr
1>Main.obj : error LNK2001: unresolved external symbol _lua_settop
1>Main.obj : error LNK2001: unresolved external symbol "void __cdecl lua::on_shutdown(struct lua_State *,int (__cdecl*)(struct lua_State *))" (?on_shutdown@lua@@YAXPAUlua_State@@P6AH0@Z@Z)
1>Main.obj : error LNK2001: unresolved external symbol _GeoIP_country_code_by_addr
1>Main.obj : error LNK2001: unresolved external symbol _GeoIP_open
1>Main.obj : error LNK2001: unresolved external symbol _luaL_error
1>Main.obj : error LNK2001: unresolved external symbol _lua_pushboolean
1>C:\Users\User\Documents\Visual Studio 2013\Projects\Win32Project1\Release\GeoIP_win32.dll : fatal error LNK1120: 11 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我不知道错误是什么意思,该模块之前已由其他人构建,因此代码应该没问题。

1 个答案:

答案 0 :(得分:3)

您似乎忘记配置构建,因此链接器知道要链接到模块的库。看起来你应该链接到GeoIP DLL,当然还有Lua DLL。在项目的“属性”对话框中,查找Linker | General | Additional Library Directories,它必须指明Lua DLL所在的文件夹以及GeoIP DLL所在的文件夹。然后查看Linker | Input | Additional Dependencies,它必须指明Lua DLL的Lua导出库的名称(例如lua51.lib),以及GeoIP(类似libgeoip.lib)的名称。