为C ++构建Lua [英] Building Lua for C++

查看:110
本文介绍了为C ++构建Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Lua Wiki,如果Lua 5.1或更高版本被编译为C ++,它将使用C ++异常。由于Lua是一个C库,它只能在makefile中引用CC。所以,我的想法是将CC重新定义为g ++,所以我不必修改makefile。

  make generic CC =g ++

我能够毫无问题地构建Lua。然而,现在当我将我的C ++应用程序链接到Lua(静态)库时,我收到了很多Lua函数(lua_checklstring,lua_pushinteger等)的未定义引用错误。



  make generic CC =gcc

我为C ++编译Lua不正确吗?我假设我需要使用g ++,因为Lua源代码包含预处理器检查cplusplus以确定是否应启用C ++异常。

正如在我对原始问题的评论中提到的,我在我的C ++应用程序中包含了lua.hpp。当Lua被编译为C库后,这种效果很好,因为lua.hpp undos命名为:

  lua.hpp:

externC{
#includelua.h
#includelualib.h
#includelauxlib.h
}

正如@EtanReisner指出的,当使用Lua作为C ++库时,我不需要撤消名称截断。因此,解决方案只包含实际的Lua头文件,而不是lua.hpp

  //使用Lua作为C库
#include< lua.hpp>

//将Lua用作C ++库
#include< lua.h>
#include< lualib.h>
#include< lauxlib.h>


According to the Lua Wiki, if Lua 5.1 or later is compiled as C++, it will use C++ exceptions. Since Lua is a C library it only references CC in the makefile. So, my idea was to redefine CC as "g++" so I don't have to modify the makefile.

make generic CC="g++"

I was able to build Lua without any problems. However, now when I link my C++ application to the Lua (static) library I receive undefined reference errors for many Lua functions (lua_checklstring, lua_pushinteger, etc).

When I build Lua using using gcc my application links successfully.

make generic CC="gcc"

Am I compiling Lua for C++ incorrectly? I assume I need to use g++ somehow since the Lua source code contains preprocessor checking for cplusplus to determine if C++ exceptions should be enabled.

解决方案

As mentioned in my comment to the original question, I was including lua.hpp in my C++ application. This worked well when Lua was compiled as a C library since lua.hpp undos name mangling:

lua.hpp:

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

As pointed out by @EtanReisner, when using Lua as a C++ library instead I do not need to undo the name mangling. So the solution was to simply include the actual Lua headers, rather than lua.hpp

// Using Lua as a C library
#include <lua.hpp>

// Using Lua as a C++ library
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

这篇关于为C ++构建Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆