简体   繁体   English

在C ++中嵌入Lua:链接问题(liblua5.1.a)

[英]Embedding Lua in C++: linkage problems (liblua5.1.a)

I am embedding Lua in a C++ application and I am getting the following linkage errors: 我将Lua嵌入C ++应用程序中,并且出现以下链接错误:

g++     -o dist/Debug/GNU-Linux-x86/testluaembed build/Debug/GNU-Linux-x86/src/main.o build/Debug/GNU-Linux-x86/src/LuaBinding.o -L../../mainline/tanlib_core/dist/Debug/GNU-Linux-x86 -L../../mainline/tanlib++/dist/Debug/GNU-Linux-x86 -L/usr/lib ../../mainline/tanlib_core/dist/Debug/GNU-Linux-x86/libtanlib_core.so ../../mainline/tanlib++/dist/Debug/GNU-Linux-x86/libtanlibpp.so /usr/lib/liblua5.1.a /usr/lib/libtolua++5.1.a /usr/local/boost_1_45_0/stage/lib/libboost_filesystem.a /usr/local/boost_1_45_0/stage/lib/libboost_system.a 
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `ll_loadfunc':
/usr/lib/liblua5.1.a(loadlib.o): In function `gctm':
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/testluaembed] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

Anyone knows why these errors are occurring, and how to fix them? 有谁知道为什么会发生这些错误,以及如何解决这些错误?

In http://lua-users.org/wiki/BuildingLua http://lua-users.org/wiki/BuildingLua中

there is a note: 有一个注释:

Note on embedding Lua in C++ applications 在C ++应用程序中嵌入Lua的注意事项

Please note that Lua is a clean subset of ANSI C, and can be compiled as C or C++. 请注意,Lua是ANSI C的干净子集,可以编译为C或C ++。 Lua headers do not come with {#ifdef __cplusplus extern "C" {#endif ... Lua header ...#ifdef __cplusplus}#endif } in them so that lua can be compiled as C or C++ simply by changing the name of the files, without having to make any changes to the file contents. Lua头文件中不带有{#ifdef __cplusplus extern“ C” {#endif ... Lua头文件...#ifdef __cplusplus} #endif},因此可以通过更改lua的名称将其编译为C或C ++。文件,而无需对文件内容进行任何更改。

If lua was compiled as a C library, which is typical with pre-packaged binaries, in order to embed Lua in a C++ application (ie link C to C++) you will have to place extern "C" around the inclusion of the Lua headers in your C++ application, eg, 如果lua被编译为C库(通常是预打包的二进制文件),则要将Lua嵌入C ++应用程序(即,将C链接到C ++),则必须在包含Lua标头的地方放置extern“ C”在您的C ++应用程序中,例如

extern "C" {
#include "lua.h"
}

If you do not do this you may get link errors because of C++ name mangling. 如果不这样做,则可能由于C ++名称修改而导致链接错误。

Please do not complain about this on the mailing list. 请不要在邮件列表上对此抱怨。 :-) Take the time to search the mailing list as this has been covered many times before. :-)花点时间搜索邮件列表,因为以前已经讨论过很多次。

It could be argued that if you are distributing a pre-packaged binaries of the libraries, then you have compiled the lua core as either C (most likely) or as C++, and if you compiled lua as C, you should modify the lua headers to indicate this. 可能会说,如果要分发库的预打包二进制文件,那么您已经将lua核心编译为C(最有可能)或C ++,并且如果将lua编译为C,则应修改lua头文件表示这一点。 However, using prebuilt libraries for lua isn't recommended by the authors, they recommend directly incorporating the lua source into your application. 但是,作者不建议将预构建的库用于lua,他们建议直接将lua源合并到您的应用程序中。 See BuildingModules for a discussion (the end of the page). 有关讨论,请参见BuildingModules(页面末尾)。

By default if lua 5.1 or later is compiled as C++, it will use C++ exceptions to unwind the stack rather than longjmp/setjmp, though this is configurable (at compile time). 默认情况下,如果lua 5.1或更高版本被编译为C ++,它将使用C ++异常展开堆栈,而不是longjmp / setjmp,尽管这是可配置的(在编译时)。 See luaconf.h near LUAI_THROW/LUAI_TRY for a discussion of this. 有关此问题的讨论,请参见LUAI_THROW / LUAI_TRY附近的luaconf.h。

您只需在您的C ++源代码中包含lua.hpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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