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

luaopen_ functions should be extern "C" #422

Open
phoppermann opened this issue Jan 15, 2024 · 11 comments
Open

luaopen_ functions should be extern "C" #422

phoppermann opened this issue Jan 15, 2024 · 11 comments

Comments

@phoppermann
Copy link

No description provided.

@alerque
Copy link
Member

alerque commented Jan 15, 2024

PRs welcome, plus a bit more explanation or links to documentation for why.

@phoppermann
Copy link
Author

phoppermann commented Jan 15, 2024

Compare e.g. https://github.com/lunarmodules/luafilesystem/blob/ff2cbc21f1a2fd34d30c0840ff2abb0121d46131/src/lfs.h#L34.

This is required if you want to compile as C++ (not C), otherwise the symbols are not found in require. This has been advised for Lua itself (see ThePhD/sol2#739), so I think it should be possible for the modules as well.

@alerque
Copy link
Member

alerque commented Jan 15, 2024

Thanks for the explanation and links. Yes I think this makes sense. Again PRs welcome. As a bonus some test tooling to show it working (or failing) for C and C++ builds would help too.

@daurnimator
Copy link
Contributor

Lua itself has no support for throwing with C++ exceptions, anyone doing that must be using custom patches against lua itself.

Lua itself also does not support declaring luaopen functions inside of extern "C", see https://www.lua.org/source/5.4/luaconf.h.html#LUAMOD_API
Instead I believe you have to include the entire lua header(s) inside of an extern "C".

Lua modules such as luasocket should similarly be included inside of an extern "C" block.

@phoppermann
Copy link
Author

phoppermann commented Jan 16, 2024

Instead I believe you have to include the entire lua header(s) inside of an extern "C".

Lua modules such as luasocket should similarly be included inside of an extern "C" block.

How would I do that in

local socket = require("socket")

?

@daurnimator
Copy link
Contributor

How would I do that in

local socket = require("socket")

?

huh? It's a question of how you compile (or include) luasocket; not how you load it into a running interpreter

@phoppermann
Copy link
Author

For instance, when requiring a.b.c, it will search for a C library for a. If found, it looks into it for an open function for the submodule; in our example, that would be luaopen_a_b_c.

https://www.lua.org/manual/5.4/manual.html

So when you require from a C++ Lua, it will not work without extern "C" because of different name mangling.

@daurnimator
Copy link
Contributor

So when you require from a C++ Lua, it will not work without extern "C" because of different name mangling.

Do you mean the library will have the wrong symbol name? or Lua will have the wrong symbol names?

Lua's C module loader uses dlsym to find symbols, it doesn't know about C++ name mangling

@phoppermann
Copy link
Author

Well, I've encountered the issue when require luasocket as is from C++ Lua. If you tell me that this cannot happen because the module loader doesn't care about C/C++, I have no clue what's going on. Also, if this is the case, why do other modules use extern "C" at all?

@daurnimator
Copy link
Contributor

@phoppermann how did you compile luasocket?

@phoppermann
Copy link
Author

Also as C++ (like Lua itself to be aligned). Maybe that's not the right way?

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

No branches or pull requests

3 participants