Using Lua when built as C++

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Lua when built as C++

Gisle Vanem
I did an experiment to build Lua 5.3 as C++.
It worked fine, but calling any 'lua_x()' causes
link errors.

The comment in ldo.c indicates it's possible:
   ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
   ** default, Lua handles errors with exceptions when compiling as
   ** C++ code, ...

Since LUA_API is either empty or defined as
'__declspec(dllimport)', I patched luaconf.h to suite
a C++ ABI:

#if !defined(LUA_API)
   /* around the original stuff.
#endif

and a '#define LUA_API extern "C++"' in an
external 'FI./config.h' file. How else should be do it?

Or is it only 'ldo.c' that could be compiled as C++?

--
--gv

v
Reply | Threaded
Open this post in threaded view
|

Re: Using Lua when built as C++

v
On Thu, 2020-01-30 at 15:25 +0100, Gisle Vanem wrote:
> I did an experiment to build Lua 5.3 as C++.
> It worked fine, but calling any 'lua_x()' causes
> link errors.
>
> Or is it only 'ldo.c' that could be compiled as C++?

I'm fairly sure that you need to build and link whole Lua in C++ mode,
not only some files. Are you linking statically or dynamically? With
clang/gcc and CMake all I had to do is to set language and linkage
language to C++, then Lua compiles and works with no extra tweaks as
static library.

What are exact linkage errors? You must not place Lua includes into
`extern "C"` block if you build Lua in C++ mode.


--
v <[hidden email]>


Reply | Threaded
Open this post in threaded view
|

Re: Using Lua when built as C++

Viacheslav Usov
In reply to this post by Gisle Vanem
On Thu, Jan 30, 2020 at 3:25 PM Gisle Vanem <[hidden email]> wrote:

> How else should be do it?

This is the entire patch I use for statically linked Lua:

diff --git a/src/luaconf.h b/src/luaconf.h
index fd447cc..49e127c 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -251,7 +251,11 @@
 
 #else /* }{ */
 
+#ifdef __cplusplus
+#define LUA_API extern "C"
+#else
 #define LUA_API extern
+#endif
 
 #endif /* } */
 
It is similar to your approach. In the past, I used some other ways, but eventually I settled on this. Repeat, statically linked Lua.

Cheers,
V.

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua when built as C++

Matthias Kluwe
In reply to this post by Gisle Vanem
Hi!

Am Do., 30. Jan. 2020 um 15:25 Uhr schrieb Gisle Vanem <[hidden email]>:

>
> I did an experiment to build Lua 5.3 as C++.
> It worked fine, but calling any 'lua_x()' causes
> link errors.
>
> The comment in ldo.c indicates it's possible:
>    ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
>    ** default, Lua handles errors with exceptions when compiling as
>    ** C++ code, ...
>
> Since LUA_API is either empty or defined as
> '__declspec(dllimport)', I patched luaconf.h to suite
> a C++ ABI:
>
> [...]

As you're looking into `__declspec(dllimport)`, you're probably using
windows. If you're using Visual Studio: The compiler guesses the
source type by the extension. I've been successfull using the compiler
flag /Tp (see https://docs.microsoft.com/en-us/cpp/build/reference/tc-tp-tc-tp-specify-source-file-type?view=vs-2019)
when building Lua as a C++ project.

Regards,
Matthias

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua when built as C++

Gisle Vanem
In reply to this post by Viacheslav Usov
Viacheslav Usov wrote:

> +#ifdef __cplusplus
> +#define LUA_API extern "C"
> +#else
>   #define LUA_API extern
> +#endif
>

It was a typo in my first mail.
I intended it to be as yours, but a
   #define LUA_API extern "C++"

and compiling all LUA-src with '-TP -EHsc' works too!
(I'm on MSVC-2019 and clang-cl v9).

I'm just experimenting with this since some .lua-code
in an Italian spagetti-program called Ntop-NG crashes
inside ldo.c An unknown reason causes 'CallInfo *ci' to become
NULL during a 'luaD_precall()'.

Hence the reason I'd like to use 'throw()' instead.

> It is similar to your approach. In the past, I used some other ways, but eventually I settled on this. Repeat,
> statically linked Lua.

Yes. Static.


--
--gv

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua when built as C++

Viacheslav Usov
On Thu, Jan 30, 2020 at 4:26 PM Gisle Vanem <[hidden email]> wrote:

>    #define LUA_API extern "C++"

The reason why I have settled on extern "C" is that I also use libraries built in C, which would otherwise be impossible to link with.

Cheers,
V.
Reply | Threaded
Open this post in threaded view
|

Re: Using Lua when built as C++

Gé Weijers

On Thu, Jan 30, 2020 at 7:41 AM Viacheslav Usov <[hidden email]> wrote:
On Thu, Jan 30, 2020 at 4:26 PM Gisle Vanem <[hidden email]> wrote:

>    #define LUA_API extern "C++"

The reason why I have settled on extern "C" is that I also use libraries built in C, which would otherwise be impossible to link with.


If you're mixing C and C++ interfaces throwing a Lua error becomes a problem:

- if you use 'longjmp' you don't perform proper stack unwinding and destruction in C++ code.
- if you use 'throw' the result is system dependent, the result of unwinding a stack with mixed C and C++ stack frames is probably not properly defined.

It may work on some platforms (like Windows with its structured exception handling), but it's not portable. C++ ABIs are also not the most stable, what works today may break tomorrow.

--

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua when built as C++

Viacheslav Usov
On Thu, Jan 30, 2020 at 6:25 PM Gé Weijers <[hidden email]> wrote:

> It may work on some platforms (like Windows with its structured exception handling), but it's not portable.

I am aware of the tradeoffs involved, and using extern "C" for the Lua library compiled in the C++ mode using throw has been found most practical for my particular needs.

Cheers,
V.