简体   繁体   中英

lua loads my c++ shared library but not its dependent shared libraries

I have a c++ (legacy) application which calls some lua scripts for some functionality.

Now i am writing a new c++ library which should be called from that lua script.

#include <lua.hpp>

extern "C" {

static int isquare(lua_State *L){              /* Internal name of func */
        return 1;                              /* One return value */
}
static int icube(lua_State *L){                /* Internal name of func */
        return 1;                              /* One return value */
}


/* Register this file's functions with the
 * luaopen_libraryname() function, where libraryname
 * is the name of the compiled .so output. In other words
 * it's the filename (but not extension) after the -o
 * in the cc command.
 *
 * So for instance, if your cc command has -o power.so then
 * this function would be called luaopen_power().
 *
 * This function should contain lua_register() commands for
 * each function you want available from Lua.
 *
*/
int luaopen_power(lua_State *L){
        printf("before power open");
        lua_register(
                        L,               /* Lua state variable */
                        "square",        /* func name as known in Lua */
                        isquare          /* func name in this file */
                        );

        lua_register(L,"cube",icube);
        printf("after power register");
        return 0;
}
}

g++ -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1  hellofunc.cpp -lstdc++

I did not mention any lua5.1 so file for linking.

but this power.so needs lua-5.1.so at runtime.

Now, i have a C++ legacy application which has lua52 compiled within it.

And it calls alert.lua for some work.

package.cpath = package.cpath .. ";/usr/lib64/power.so"
package.cpath = package.cpath .. ";/usr/lib64/liblua-5.1.so"
require("power")

Note: the lua which loads the power.so runs on lua5.2

Power.so is compiled and depends on lua5.1

And i get an error

 undefined symbol: lua_setfield'

Does these versions have to be same ?

Can someone shed some light on this issue?

EDIT: If i compile power.so with lua52.so then the lua script and the C++ application aborts unusually .

If if dont mention -llua52 while building power.so then at runtime there is an error saying undefined symbol.

EDIT: More Explanation:

There is a C++ application .exe. (samplecpp) There is also a .dll/.sh which is built along with the lua 5.2 library hence having lua as well as other functionality. (luaplugin.so)

This luaplugin.so can call any lua script which is configured. It calls and executes functions in lua script.

Now i have a lua script which i want to conenct to a different c++ module.

The c++ module(build to .so having dependency on lua52.so) i am writing in turn uses lua functions for registration etc. As it has to be loaded from the lua script.

But at runtime when samplecpp executes the lua script and when luascript requires the c++ .so, im getting unresolved error on the lua functions that are used in the c++ .so.

How can i make it refer the lua functions that are available in the samplecpp itself ?

Yes, you need to use C/C++ libraries compiled for the same version of Lua.

There is no ABI compatibility between different versions of Lua.
See http://www.lua.org/versions.html#numbering

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM