0

I created a very simple C dll and I'm trying to load it using Lua interactive mode

When I use require I get:

error loading module 'LuaExperiment' from file 'C:\Lua\5.2.4\LuaExperiment.dll' The specified procedure could not be found

When I use package.loadlib I get

multiple Lua VMs detected

Bellow is the contents of my Lua installation folder (LuaExperiment is my project):

Contents of my Lua Folder

I did the following to create the folder above: Opened a Developer Command Prompt for VS2015 as an Administrator and entered the following commands:

> vcvars32
> cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
> ren lua.obj lua.o
> ren luac.obj luac.o
> link /DLL /IMPLIB:lua5.2.4.lib /OUT:lua5.2.4.dll *.obj
> link /OUT:lua.exe lua.o lua5.2.4.lib
> lib /OUT:lua5.2.4-static.lib *.obj
> link /OUT:luac.exe luac.o lua5.2.4-static.lib

These are the changes I made to Project Properties:

Added C:\Lua\5.2.4; to VC++ Directories->Include Directories

Changed C\C++->Advanced->Compile As to Compile as C Code (/TC)

Added C:\Lua\5.2.4\*.lib; to Linker->Additional Dependencies

Here are the .h and .c files:

https://github.com/ImperfectVoid/C

FinnTheHuman
  • 1,115
  • 13
  • 29
  • 1
    Does LuaExperiment.ddl contain a function `luaopen_LuaExperiment`? – lhf Apr 14 '17 at 22:45
  • it does not. I didn't know the name was supposed to be that. I named my open function `init`. Will rename it and see how it goes. – FinnTheHuman Apr 14 '17 at 22:46
  • @lhf That kind of helped. Both `require "LuaExperiment"` and `package.loadlib("C:/Lua/5.2.4/LuaExperiment.dll", "luaopen_LuaExperiment")` fail with the same message now, `multiple Lua VMs detected`. – FinnTheHuman Apr 14 '17 at 22:55
  • This is a question about how to use the Lua API (which you normally would use from C and on Windows means you'll end up with a DLL). I've re-tagged this to reflect this; please don't re-tag again with a discussion. If you want to discuss how tagging works best, there is also [Meta]. – Martijn Pieters Apr 15 '17 at 07:23
  • @MartijnPieters The question will never get answered, since [lua-api] has only 8 followers. – FinnTheHuman Apr 15 '17 at 10:53
  • @FinnTheHuman: That's not how this works. `python-c-api` has only 59 followers, but *followers of the Python tag* can use the extra tag to filter further on what is answerable to them. Followers of the C tag on the other hand, **do not know how Lua modules work**. They know how to code something in the C language. Since you have no compile errors, this is not a C language issue. – Martijn Pieters Apr 15 '17 at 10:56
  • @FinnTheHuman: on the other hand, trying to broadcast to too many tags can have the opposite effect from what you are trying to achieve here, as you then attract annoyed experts that wonder why their attention is being drawn to something they can't help with, optionally followed by extended noisy comment discussions, heated arguments, and down- and close votes. – Martijn Pieters Apr 15 '17 at 10:58
  • @MartijnPieters 59 is a lot better than 8. And I'm almost certain it's a C _building_ problem. If only the experts would take a look at it instead of dismissing the question immediately, I'm almost sure they would spot the error with ease. – FinnTheHuman Apr 15 '17 at 10:58
  • Then *prove that*. Provide enough information for a C expert to be of help. **Make** it a C issue as right now it is not. You'll have to show that it is an issue with *the language*, not the binary that was produced or how Lua loads the resulting shared object. Note that you could build a library in machine code too, providing the same entry points, reproducing the same issue. – Martijn Pieters Apr 15 '17 at 11:01
  • And no, 59 is a lot worse when you look at the respective community sizes. The [tag:lua] tag has 7k followers, so 8 is about 0.1%. The [tag:python] tag has 328k followers. 59 is maybe 0.04%. Not that it matters, this is not about the number of people following a classification tag. I'm sorry you are having this problem, but you are not going to solve it by tagging this with the [tag:c] tag, because C experts are not going to be of help here. – Martijn Pieters Apr 15 '17 at 11:05
  • If you want to discuss this further, I suggest you either take it to [meta] or [chat]. – Martijn Pieters Apr 15 '17 at 11:07
  • And right now your error message makes this sound like a duplicate of [lua5.2's error: multiple Lua VMs detected](//stackoverflow.com/q/14213559). Perhaps the advice there applies to Windows too? – Martijn Pieters Apr 15 '17 at 11:09
  • @MartijnPieters Do you know C? Specially the builder options? I may have an idea on how to fix this, but I don't know how to build via command line. I merely copy pasted those commands. I think I'm getting `main` compiled 6 times. Both `lua.c` and `luac.c` have the function `main`. Lua.c has the `main` for the interpreter (lua.exe), `luac.c` for the compiler (luac.exe) and both `lua.c` and `luac.c` are part of `lua5.2.4.dll`. I need to fix that. – FinnTheHuman Apr 15 '17 at 11:11
  • 1
    What I'd do is look for other examples. The post I linked to links to http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/; the base64 example looks nice and simple. Strip that down, make that work on Windows, look at what is different. I also found [multiple Lua VM's detected](//stackoverflow.com/q/28701142), where someone using C++ gets the same error (different language, same issues), and the answer lists 2 reasons why you might get the error. – Martijn Pieters Apr 15 '17 at 11:23
  • I looked into those questions before posting mine. The answer to the first is too vague and, as I mentioned, I don't build things manually, I couldn't make sense of what was said. The second is less vague, but I don't know how to do any of that or even where to begin learning. I'm used to C# where there's very little you must do to build something. – FinnTheHuman Apr 15 '17 at 11:30
  • It seems you are linking your `LuaExperiment.dll` with `lua5.2.4.lib`, but you should link against the Lua DLL instead. – asandroq Apr 18 '17 at 12:00
  • @asandroq When I changed `C:\Lua\5.2.4\*.lib;` to `C:\Lua\5.2.4\lua5.2.4.dll;` (Visual Studio `Project->Properties->Linker->Input->Additional Dependencies`) I got this error: LNK1107 invalid or corrupt file: cannot read at 0x2B0 (Project: LuaExperiment, File: C:\Lua\5.2.4\lua5.2.4.dll). This makes sense, because I think that property should get only `.lib` files added to it. So how would I do that? – FinnTheHuman Apr 20 '17 at 15:11
  • @FinnTheHuman No, you still link with a `.lib`, but this library is just an import library, not really the full Lua runtime. This import library should forward your calls to the Lua DLL. When you use `*.lib`, you are linking against *everything*, including the static Lua library. – asandroq Apr 20 '17 at 15:20
  • @asandroq Thank you! I changed `C:\Lua\5.2.4\*.lib;` to `C:\Lua\5.2.4\lua.5.2.4.lib;` (in Visual Studio `Project->Properties->Linker->Input->Additional Dependencies`) and now everything seems to be working fine! You should maybe turn that comment into an answer, explaining what was wrong with my linking input arguments. Again, thank you very much! – FinnTheHuman Apr 20 '17 at 15:51

0 Answers0