Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extern "C" needed #1

Open
jumogehn opened this issue Apr 25, 2014 · 2 comments
Open

extern "C" needed #1

jumogehn opened this issue Apr 25, 2014 · 2 comments

Comments

@jumogehn
Copy link

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..

@stevedonovan
Copy link
Owner

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

@jumogehn
Copy link
Author

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants