我需要用Lua脚本编写一个C ++项目,该脚本从PostgreSQL读取信息,并将接收到的数据返回给C ++项目。 当使用Lua解释器从终端调用它时,它可以正常工作,但是当我尝试从C ++调用Lua模块时,脚本工作不正确并返回错误。
Home OS-Debian 9。 PostgreSQL版本-9.6。 编译器-g ++。 Lua版本-5.3.4。 我已经尝试在Lua文件中尝试更改其他* .so文件。它没有帮助。
编译器/链接器标志:
g ++ -c -m64 -pipe -g -std = gnu ++ 11 -Wall -W -fPIC -DQT_QML_DEBUG -I。 -I / usr / lib / x86_64-linux-gnu / qt5 / mkspecs / linux-g ++-64 -o main.o
g ++ -m64 -o luaTest main.o -llua -ldl
main.cpp
lua_State* _script = luaL_newstate();
if (_script == NULL) {
std::cout << "Failed to allocate memory!\n";
return -1;
}
int result = luaL_loadfile(_script, filename1);
if (result != LUA_OK) {
std::cout << "Failed to load file!\n";
return -1;
}
luaL_openlibs(_script);
result = lua_pcall(_script, 0, 0, 0);
if (result != LUA_OK) {
std::string luaError = lua_tostring(_script, -1);
std::cout << "Failed to run script!\n" << luaError << '\n';
return -1;
}
lua:
package.cpath = "/usr/lib/x86_64-linux-gnu/liblua5.3-sql-postgres.so"
function getDatabase()
driver = require "luasql.postgres"
env = assert(driver.postgres())
connect = assert(env:connect("luabase", "sorokin", "123456", "127.0.0.1", "5432"))
res = connect:execute("DROP SEQUENCE user_ids")
res = connect:execute("DROP TABLE users")
res = assert(connect:execute("CREATE TABLE users(\
id INTEGER PRIMARY KEY ,\
fname CHAR(15),\
lname CHAR(30),\
job CHAR(15)\
)"))
res = assert(connect:execute("INSERT INTO users(id, fname, lname, job) \
VALUES (1, 'Andrey', 'Sedin', 'Deputat')"))
res = assert(connect:execute("INSERT INTO users \
VALUES (2, 'Andrey', 'Dranga', 'Boxer')"))
res = assert(connect:execute("INSERT INTO users \
VALUES (3, 'Roberto', 'Ierusalimskiy', 'Programmer')"))
cursor = assert(connect:execute("SELECT * FROM users ORDER BY lname"))
print()
print(string.format("%1s %-16s %-31s %-16s",
"#", "| FNAME", "| LNAME", "| JOB"))
print(string.format("%1s %-16s %-31s %-16s",
"-", "| -----", "| -----", "| ---"))
row = cursor:fetch({}, "a")
while row do
print(string.format("%1s %1s %-15s%1s %-30s%1s %-15s",
row.id, "|", row.fname, "|", row.lname, "|", row.job))
row = cursor:fetch(row, "a")
end
print()
cursor:close()
connect:close()
env:close()
end
getDatabase()
来自Lua脚本的错误消息:
从文件中加载模块'luasql.postgres'时出错 '/usr/lib/x86_64-linux-gnu/liblua5.3-sql-postgres.so': /usr/lib/x86_64-linux-gnu/liblua5.3-sql-postgres.so:未定义 符号:lua_settop