Results 1 to 20 of 22

Thread: Lua into Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2015
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Lua into Qt

    I need to use lua language in my Qt-application. I add necessary files into my project and write this code:

    Qt Code:
    1. #include "stdafx.h"
    2. #include "podkl.h"
    3. #include <QtWidgets/QApplication>
    4.  
    5. #pragma comment(lib, "lua52.lib")
    6.  
    7. #include <lua.hpp>
    8.  
    9. int Write(lua_State* s) {
    10. return 0;
    11. }
    12.  
    13. int main(int argc, char *argv[]) {
    14. QApplication a(argc, argv);
    15. PODKL w;
    16. w.show();
    17.  
    18. lua_State* s = luaL_newstate();
    19. luaopen_base(s);
    20.  
    21. lua_pcall(s, 0, 0, 0);
    22.  
    23. //lua_register(s, "Write", Write);
    24.  
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    49068-clip-32kb.png

    If I compile the program that way I have a linker message:
    1> main.cpp
    1>main.obj : error LNK2019: unresolved external symbol lua_pcall referenced in function main
    1>D:\Programming\VisualStudio\Projects\STUDYING\PO DKL\x64\Debug\\PODKL.exe : fatal error LNK1120: 1 unresolved externals
    But if I launch it this way:
    Qt Code:
    1. //lua_pcall(s, 0, 0, 0);
    2. lua_register(s, "Write", Write);
    To copy to clipboard, switch view to plain text mode 

    Then I get an error message: "Unhandled exception at 0x00007FFDA7120810 (lua52.dll) in PODKL.exe: 0xC0000005: Access violation reading location 0x0000002F0ABDA198."

    I have tried to launch lua from usual console project. And I have done it. Don't know why I can't do it on Qt.

    What should I do to use lua on Qt-application?
    (Win 8.1, x64)

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    Your link error says that the lua_pcall method isn't found in any of the libraries you link to. Is it defined in one of the lua source files you show in your screen shot? If so, is the .obj file for that being linked in to your application?

    You don't check the value of "s" returned by your call to luaL_newstate(). How do you know it isn't a NULL pointer and is the cause of the access violation?

  3. #3
    Join Date
    Jun 2015
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    It has to be linked. I downloaded project (.c and .h) files and .dll, .lib. .pdb. Then included it to my project. How to check if the .obj file is linked or no?

    I checked the 's' with debugger. It wasn't NULL
    Last edited by Defake; 14th June 2015 at 08:14.

  4. #4
    Join Date
    Jun 2015
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    Why when I create a simple console project, include lua files and turn it on, everything is ok.
    But when I create qt project and do exactly the same, I get errors about unresolved external..
    Why?? I hate it already >_<

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Lua into Qt

    My guess is, unsurprisingly, that the program crashes because the link failed to find exported Lua symbols. Frankly, I am surprised you got an executable out at all: do a completely clean build first. Check that you have linked the lua51 library through the LIBS pro file variable ( or whatever the Visual Studio obscured equivalent is).

  6. #6
    Join Date
    Jun 2015
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    No, I couldn't seem to add LINK line properly.
    That are my efforts:
    Qt Code:
    1. LIBS += -L"$$PWD/lua/lua5.3.0.lib"
    2. LIBS += -L"$$PWD/lua/" -llua5.3.0
    3. LIBS += L"$$_PRO_FILE_PWD_/lua" -llua
    4. LIBS += L"$$_PRO_FILE_PWD_/lua/" -llua.dll
    5. (And other looks like this variations)
    To copy to clipboard, switch view to plain text mode 

    But no one works. How to do it?

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    Why when I create a simple console project, include lua files
    So exactly what are you doing to "include lua files"? The header files, the .c / cpp files, what? I think that when you create your Qt project you simply aren't compiling and linking the .c / .cpp file from the lua distro that contains the unresolved call.

    LIBS += L"$$_PRO_FILE_PWD_/lua" -llua
    LIBS += L"$$_PRO_FILE_PWD_/lua/" -llua.dll
    The files shown in your screen shot are named "lua51.lib and .dll, not "lua.lib" and "lua.dll" so these LIBS statements refer to non-existent files.

  8. #8
    Join Date
    Jun 2015
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    I've alreadly downloaded new version lua (5.3.0). What exactly I do now:
    Create Qt project in Visual stuido. Change x32 to x64 and check if the project launches correctly.
    After checking I add into the project folder (in the windows explorer) a new folder "lua" and put into this all necessary lua-files (lua .c, .h, one .hpp, .lib, .dll and .exe files). Then I create into solution explorer (in Visual Studio) a new folder "lua" and put (drag n drop) into this all files from the folder "lua" in the windows explorer.

    Then I include .hpp file (that contains including lua.h, lualib.h and lauxlib.h) and create new variable lua_State.

    What I have at this moment:

    111.jpg

    Then it seems I have to open QtCreator and adjust .pro file, but my results I've posted above.
    How to adjust it properly?
    Last edited by Defake; 15th June 2015 at 08:59.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    If you are using Visual Studio for building your project, why are you also using Qt Creator? Stick with one or the other. I use Visual Studio (only) along with the Qt VS plugin for all of my Qt projects. The only thing I use Qt Creator for is to build and run the demos that come with Qt, and that only because I don't want to be bothered creating a Visual Studio project for them.

    Why are you putting LIB, DLL and EXE files into the VS project? What do you think Visual Studio (or Qt Creator for that matter) is going to do with them?

    By the way, if you are using Visual Studio to build your own project, any LIBs or DLLs also have to be built by the Visual C++ compiler. If your Qt Creator kit is using mingw, anything compiled in Qt Creator will be incompatible with code built in VS.

  10. #10
    Join Date
    Jun 2015
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Lua into Qt

    Im using Qt creator to adjust my .pro file, because I can launch qmake there (And I think that it's necessary for applying changes in .pro file). I couldn't find it in Visual Studio.

    I'm putting all files from the lua folder for 100% working. I will clean my project after it works.

    I downloaded lua5.3.0.tar.gz, opened Visual Studio command prompt and made the files with proper commands. Anyway it works in console project.

    I think the problem is in .pro file. I should link the lua library. How can I do it properly? (Maybe in Visual Studio, if it's possible)

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Lua into Qt

    Assuming your Lua deployment folder C:\Lua contains:
    Qt Code:
    1. Archive: lua-5.3_Win32_dll10_lib.zip
    2. Length Date Time Name
    3. --------- ---------- ----- ----
    4. 8433 10-30-2014 02:11 include/lauxlib.h
    5. 14734 12-27-2014 03:24 include/lua.h
    6. 191 12-23-2004 10:53 include/lua.hpp
    7. 20308 05-19-2015 11:04 include/luaconf.h
    8. 1173 02-07-2014 03:32 include/lualib.h
    9. 231936 05-20-2015 00:47 lua53.dll
    10. 30044 05-20-2015 00:47 lua53.lib
    11. --------- -------
    12. 306819 7 files
    To copy to clipboard, switch view to plain text mode 
    Then this does it
    Qt Code:
    1. TEMPLATE = app
    2. TARGET = test
    3. DEPENDPATH += .
    4. INCLUDEPATH += .
    5.  
    6. INCLUDEPATH += C:/Lua/include
    7. LIBS += -LC:/Lua -llua53
    8.  
    9. # Input
    10. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <lua.hpp>
    3.  
    4. int main(int argc, char *argv[]) {
    5. QCoreApplication a(argc, argv);
    6. lua_State* s = luaL_newstate();
    7. luaopen_base(s);
    8. lua_pcall(s, 0, 0, 0);
    9.  
    10. int result = a.exec();
    11.  
    12. lua_close(s);
    13.  
    14. return result;
    15. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.