llua icon indicating copy to clipboard operation
llua copied to clipboard

extern "C" needed

Open jumogehn opened this issue 10 years ago • 2 comments

In C++ project, now, user must apply it himself. But noticing that this is needed is not straight away. User will just find link error and the gcc error message is not that rich enough to provide such information..

jumogehn avatar Apr 25 '14 08:04 jumogehn

Yes, good point. llua.h must have extern "C" - I want to try it out in C++ mode for the Microsoft compiler anyway.

stevedonovan avatar Apr 25 '14 08:04 stevedonovan

I found a way lua takes for this kind of issue:

Because you can compile Lua both as C and as C++ code, lua.h does not include this typical adjustment code that is present in several other C libraries:

#ifdef __cplusplus
extern "C" {
#endif
   ...
#ifdef __cplusplus
}
#endif

Therefore, if you have compiled Lua as C code (the most common case) and are using it in C++, you must include lua.h as follows: extern "C" { #include <lua.h> } A common trick is to create a header file lua.hpp with the above code and to include this new file in your C++ programs.

But from 5.1 lua includes lua.hpp as shown below:

// lua.hpp // Lua header files for C++ // <<extern "C">> not supplied automatically because Lua also compiles as C++

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

jumogehn avatar Apr 27 '14 06:04 jumogehn