Col_Blimp
Col_Blimp

Reputation: 789

Lua module with Visual Studio

I'm trying to make a Lua module with Visual Studio 2013, the main ccp is here along with the module.hpp and the other include is here.

I changed the main ccp to remove a unneeded include file and two unneeded functions so I get this:

#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 throws me the following errors:

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 ==========

and I dont know what the errors are meaning, the module has been built before by others so the code should be OK.

Upvotes: 1

Views: 1636

Answers (1)

Oliver
Oliver

Reputation: 29581

Looks like you forgot to configure your build so the linker knows what libraries to link to your module. Looks like you should link to GeoIP DLL and, of course, the Lua DLL. In the project's Properties dialog, look for Linker | General | Additional Library Directories, it must indicate the folder where your Lua DLL is located, and the folder where the GeoIP DLL is located. Then look at Linker | Input | Additional Dependencies, it must indicate the name of the Lua export lib for the Lua DLL (such as lua51.lib), and same for GeoIP (something like libgeoip.lib).

Upvotes: 3

Related Questions