lunarmodules / luasql

LuaSQL is a simple interface from Lua to a DBMS.
http://lunarmodules.github.io/luasql
545 stars 191 forks source link

Undefined symbol in mysql.so while calling from C++ #114

Closed DarwinLang closed 5 years ago

DarwinLang commented 5 years ago

I have two program which using C++ to call Lua connecting to Mysql. It ran successful while it was running isolation. But when I linked them all , the error occurred.

Lua version:5.3

Lua code : local sql = require ("luasql.mysql")

C++ code:


#include <iostream>

extern "C" {
#include <stdlib.h>
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

using namespace std;

int main (void) {

    lua_State* L = luaL_newstate();
    luaL_openlibs(L);   
    if( luaL_loadfile(L,"test_sql.lua") ) cout << "load failed";
    if( lua_pcall(L,0,0,0)) fprintf(stderr,"%s",lua_tostring(L,-1));
    return 0;
}

The error was: Capture

tomasguisasola commented 5 years ago

Hi yandacampro

This is not a LuaSQL problem since the undefined symbol is a Lua function which is called by the driver. It seems you forgot to link the Lua library or if it is linked, it is not exporting the symbols to other shared objects... How did you build your ./temp executable?

Regards, Tomás

Em ter, 25 de jun de 2019 às 23:57, yandacampro notifications@github.com escreveu:

I have two program which using C++ to call Lua connecting to Mysql. It ran successful while it was running isolation. But when I linked them all , the error occurred.

Lua version:5.3

Lua code : local sql = require ("luasql.mysql")

C++ code:

include

extern "C" {

include

include

include

include

include

}

using namespace std;

int main (void) {

lua_State* L = luaL_newstate(); luaL_openlibs(L);
if( luaL_loadfile(L,"test_sql.lua") ) cout << "load failed"; if( lua_pcall(L,0,0,0)) fprintf(stderr,"%s",lua_tostring(L,-1)); return 0; }

The error was: [image: Capture] https://user-images.githubusercontent.com/46775721/60147647-dad00a80-9800-11e9-9622-ff234edf9c0e.PNG

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/keplerproject/luasql/issues/114?email_source=notifications&email_token=AABAB3OKXA6UJLI6SWMFFWLP4LLJJA5CNFSM4H3N7XHKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G3WIG7Q, or mute the thread https://github.com/notifications/unsubscribe-auth/AABAB3MYVOFS7M3W3TL66R3P4LLJJANCNFSM4H3N7XHA .

DarwinLang commented 5 years ago

Thanks for replying so quickly.

I built the ./temp by using g++ -o temp temp.cpp -ldl -llua

I have checked my ldconfig ,but it seems didn't work. image

I followed these steps to link shared library and it works fine on my previous project (https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html) Mayby I need to change "mysql.so" to "libmysql.so"? Because if I used ldconfig -p it can't show the info of mysql.so

Regards, Yanda

tomasguisasola commented 5 years ago

Hi Yanda

I built the ./temp by using g++ -o temp temp.cpp -ldl -llua And you lualibrary is named liblua.a or liblua.so?

I have checked my ldconfig ,but it seems didn't work.

You checked that mysql.so have the name lua_settop, but the problem is the inverse: the Lua library have to export this name to other objects.

I followed these steps to link shared library and it works fine on my previous project (https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html) Mayby I need to change "mysql.so" to "libmysql.so"? Because if I used ldconfig -p it can't show the info of mysql.so That seems to be useless, I suppose.

You could check how you built Lua library and check also how the command line interpreter is build and follow the same pattern...

Regards, Tomás

DarwinLang commented 5 years ago

Thanks for Tomás 's advise and I solved the problem . It is not the issue from luasql , it's the issue from lua. When I make install lua , it only created the lualib.a and it cannot be used from C++ ( Or it can?) so I edited the Makefile of lua to create liblua.so and linked it by ldconfig. And it works fine.

Here is the code 1.edited the Makefile from root of lua folder TO_LIB= liblua.a liblua.so 2.edited the Makefile from lua/src ( the code should be placed correctly)

LUA_SO=liblua.so

ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) **$(LUA_SO)**

$(LUA_SO): $(CORE_O) $(LIB_O)
$(CC) -o $@ -shared $? -ldl -lm

Best Regards, Yanda