简体   繁体   中英

Error of linking a static lib embedded with Lua on Linux

Update

after using -ldl and link liblua.a in Project 2(console) the compile is okay, but when it runs it crashed with error Segment fault (core dumped) as soon as it runs a Lua function.


Problem Background:

  • OS: Linux Ubuntu

  • IDE: CodeBlock

  • Launguage: C++

  • 2 project:

    • Project 1: static lib using Lua;

    • Project 2: console application, using the lib generated by Project 1

Problem description

Project 1 (static lib) is built successfully.

The problem is that when building project 2, it says that those Lua functions in the lib from project 1 are undefined, here is part of the error messages:

g++  -o bin/Release/BattleConsoleCB obj/Release/main.o  -s
../BattleConsole/libBattleCore.a
../BattleConsole/libBattleCore.a(DataLoader.o): 
In function `boolDataLoader::GetNumber<double>(char const*, double&) [clone .isra.5]':
DataLoader.cpp:(.text+0x13): undefined reference to `lua_settop'
DataLoader.cpp:(.text+0x1e): undefined reference to `lua_getglobal'
DataLoader.cpp:(.text+0x2b): undefined reference to `lua_isnumber'
DataLoader.cpp:(.text+0x3e): undefined reference to `lua_tonumberx'
DataLoader.cpp:(.text+0x51): undefined reference to `lua_settop'

Note that "DataLoader.cpp" is from project 1, which should have been built in the static lib "libBattleCore.a" which should have been embedded with Lua.

How to solve the problem? Thanks for your help.


Additional information:

  • Project 2 should include: "libBattleCore.h", "main.cpp", libBattleCore.a

  • Project 1 : CodeBlockbuilding options have included "Lua/install/include" in Compile search directory and "Lua/install/lib" in Link search directory

  • The same code is successfully built and run on Win with VS2012

  • If anything else is needed, please inform, I will add it.

  • I am a green hand on linux and g++. :-(

Thank you

I can't comment without 50 rep. There is no support for anonymous replies. So posting here is all I can do.

It's been a few years, but with g++, I thought it was .o files that you listed on the command line and with libraries you used -L to specify the directory to search and then -lname to include the library. (Where name lacked the leading lib and trailing .a .) Eg -L../BattleConsole -lBattleCore

Oh, and the ordering of -lfoo -lbar vs -lbar -lfoo is critical, determining which library can use code from the other.

It's not clear that a static library will necessary include code from other libraries. You may need to link (in the right order) against the libraries from project1.

Try "nm" . As in nm --demangle libBattleCore.a | grep ... This will let you see what precisely is included in each library. man nm or http://linux.die.net/man/1/nm can help you figure out what the letters stand for. For example, U is Undefined (used by not provided). T is defined in the text (code) segment. W and V are weakly-defined. Etc.

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