Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Step By Step : How to write a C++ plugin and Deploy it to the Desktop (Windows) Player — Gideros Forum

Step By Step : How to write a C++ plugin and Deploy it to the Desktop (Windows) Player

SatheeshJMSatheeshJM Member
edited May 2012 in Step by step tutorials
OK! This is a step-by-step tutorial to deploy your C++ plugins to the desktop player.
Having 0 experience in developing for Windows, I almost went insane trying to build dll files. I hope Someone finds this useful.

1. Install Visual C++
Almost all the tutorials I found recommended using Visual C++ to build dlls. Maybe there are other ways, but I used Visual C++ 2010 Express Edition.
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express

2. Install Lua
Lua installation is not needed(I think) if you can add your C++ header files into the project yourself. But I find this method a hell of a lot easier. I used Lua for Windows.
http://code.google.com/p/luaforwindows/downloads/detail?name=LuaForWindows_v5.1.4-45.exe&can=2&q=

3. Add Gideros Header Files to Lua .
Again this is not compulsory. Just makes things easier
> Download the sample BitOp Library from this link http://giderosmobile.com/forum/uploads/FileUpload/a0/29cdedc9a3ecc63ba1608c9a88992b.zip
> Extract the rar file to a folder
> Inside the extracted folder, navigate to BitOp/jni/ folder
> Copy gexport.lua, gfile.lua, gideros.lua, gplugin.lua, gproxy.lua and greferenced.lua
> Go to the Lua installation folder (Mine was C:\Program Files\Lua\5.1/)
> Paste the copied files into the include folder (C:\Program Files\Lua\5.1\include)

4.Create a New Dll Project in Visual C++ 2010 Express Edition
> Open Visual C++ 2010 Express Edition
> Choose FILE --> NEW --> PROJECT
> In the New Project window, select WIN32 --> WIN32 CONSOLE APPLICATION. Enter a name for the project and also a name for the solution. Click OK
> A Win32 Application Wizard opens. Click Next.
> Choose APPLICATION TYPE as DLL and select EMPTY PROJECT. Click FINISH.
> A Project is created


5.Add plugin code in C++
> Right Click on SOURCE FILES --> ADD --> NEW ITEM
> In the window that opens, select C++ FILE, enter a name for the file and click OK
> A blank C++ document is added. Add your code there.
> Let us use the sample plugin code found in https://docs.google.com/document/pub?id=149g_P1YlE4_6v_hHbFx3YAaYpdU0HoM70XJSLTE38ww#h.fv5ug2ke945w
Copy and the paste the code in the C++ file.
#include "gideros.h"
#include "lua.h"
#include "lauxlib.h"
static int addTwoIntegers(lua_State *L)
{
        //retrieve the two integers from the stack
        int firstInteger = lua_tointeger(L, -1);
        int secondInteger = lua_tointeger(L, -2);
        int result = firstInteger + secondInteger;
 
        //place the result on the stack
        lua_pushinteger(L, result);
 
        //one item off the top of the stack, the result, is returned
        return 1;
}
static int loader(lua_State *L)
{
        //This is a list of functions that can be called from Lua
    const luaL_Reg functionlist[] = {
            {"addTwoIntegers", addTwoIntegers},
            {NULL, NULL},
      };
            luaL_register(L, "myFirstPlugin", functionlist);
 
    //return the pointer to the plugin
    return 1;
}
static void g_initializePlugin(lua_State* L)
{
    lua_getglobal(L, "package");
    lua_getfield(L, -1, "preload");
 
    lua_pushcfunction(L, loader);
    lua_setfield(L, -2, "myFirst");
 
    lua_pop(L, 2);    
}
static void g_deinitializePlugin(lua_State *L)
{   }
REGISTER_PLUGIN("myFirstPlugin", "1.0")


6. Add Lua librarites to the project. The following are the steps.
> Go to Lua Installation Directory
> Navigate to /lib/ folder ( in my system C:\Program Files\Lua\5.1\lib)
> Copy the files Lua5.1.dll and Lua5.1.lib
> Navigate to your project directory and paste the copied files there. (make sure you paste the files in the correct directory. If the .cpp file which you created is in there, then that is your project directory)


7. Add external dependencies and incude directory to the project
> Right Click on Project and select Properties.
> Select Configuration Options --> VC++ Directories. Add the following path in INCLUDE DIRECTORIES
[Lua Installation Directory]\include ( in my case C:\Program Files\Lua\5.1\include)
> Select Configuration Properties --> Linker --> Input. Add "lua5.1.lib" (without quotes) to Additional Dependencies


8. Build the project
> Right Click on the Created C++ file and select BUILD . See if the C++ file comiles without errors.
> Right Click on the Project and click BUILD. If everything goes well, you will see the message
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
> The path of the new dll file will also be displayed along with the above message. Go to the path and Copy the dll file.


9. Include the created dll file
> Go to Gideros Installation Directory
> Paste the copied dll file (the one copied in Step 8) into the Plugins directory(in my case C:\Program Files\Gideros\Plugins)


10. Call the plugin from Gideros Desktop player
> Open Gideros and create a new Project. Create a main.lua
> In the main.lua include the following code
require "myFirst"
 
local result = myFirstPlugin.addTwoIntegers(14, 48)
print("\n\nSample 1 - Result of addTwoIntegers:", result, "\n\n")
> if Sample 1 - Result of addTwoIntegers: 62 is printed in the console, then the plugin has been successfully inported and called from the desktop player.



Thats it! Its quite a tedious process if you're doing this for the first time. And I have explained more in detail than necessary so bear with me. if you encounter any difficulties, please comment below

Cheers!
+1 -1 (+4 / -0 )Share on Facebook
«1

Comments

  • So I followed your instructions to the letter and the output window displays
    main.lua:1: module 'myFirst' not found:
    no field package.preload['myFirst']
    no file '.\myFirst.lua'
    no file 'C:\Program Files (x86)\Gideros\lua\myFirst.lua'
    no file 'C:\Program Files (x86)\Gideros\lua\myFirst\init.lua'
    no file 'C:\Program Files (x86)\Gideros\myFirst.lua'
    no file 'C:\Program Files (x86)\Gideros\myFirst\init.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\lua\myFirst.luac'
    no file '.\myFirst.dll'
    no file 'C:\Program Files (x86)\Gideros\myFirst.dll'
    no file 'C:\Program Files (x86)\Gideros\loadall.dll'
    stack traceback:
    If I try it using Windows 7 Ultimate 64bit and Visual Studio 2008 Professional. I get similar problem on Windows XP 32bit and Visual C++ 2010 Express.

    The dll file is created and copied to the "C:\Program Files (x86)\Gideros\Plugins" folder. If I copy the dll to the "C:\Program Files (x86)\Gideros\" folder then I get a different error
    Uploading finished.
    error loading module 'myFirst' from file 'C:\Program Files (x86)\Gideros\myFirst.dll':
    The specified module could not be found.

    stack traceback:
    The project is called myFirst, I even called the cpp file myFirst.cpp and yet the problem persists. I'm running Gideros Studio v2012.2.2.1

    This problem persists if I build a debug or release module. I really would like to get this working as it would save no end of time for what I'm trying to do.

    Dislikes: seppsepp

    +1 -1 (+0 / -1 )Share on Facebook
  • I forgot to mention, the plugin works fine on a proper device. I may try it on my laptop with an earlier version of Gideros Studio when I get home .
  • Well. The I tried again with the latest version of Gideros. Works fine for me.
    Have attached my dll. See if that works for you.
    rar
    rar
    Hello Gideros.rar
    8K
  • How very strange, your plugin worked so I deleted your plugin and started again from scratch, and my plugin worked, then I deleted that plugin and copied my old one and it also worked. Thanks anyway, now I can start testing some of my other stuff.
  • @SatheeshJM: I just tried your plugin on my Windows 7 Ultimate (64bit) machine and I got the same results as I did with my own plugin, it doesn't work. I'll see if I can get a handle on it but I think it's a Win7 feature and probably has something to do with permissions or location of the plugin.
  • ThiengoThiengo Member
    edited June 2012
    I am new in development and I am using MSVC++ 2010 xpress.
    I want to do a dll extension to run in script lua embedded on aplications that support LUA scripts, my doubt is:
    Is your tutorial usefull to create a dll for function mentioned above without use of Gederos?

    how can i create a dll in msvc++ 2010 xpress to run with require funtion on LUA script on windows 7?

    thanks


    tiago@thiengo.com
  • i think we really need a detailed step by step tutorial on how to produce windows/osx player plugins and device ios/android lib.

    on windows with visual c++ i had the same error of @scouser.


    ...I am a bit 'discouraged ... :((


    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • atilimatilim Maintainer
    edited July 2012
    Hi all,

    Here are the steps I usually follow:

    1. Download Qt from http://qt.nokia.com/downloads/ For windows, we'll be mostly using mingw and qmake, not Qt libraries. And I recommend to download offline installer (1.7 GB).
    2. Install it. But I use Qt only for desktop development. Therefore I deselect all the components related to mobile development (I've attached a screenshot)
    3. Add the paths qmake.exe and mingw32-make.exe's paths to the PATH environment variable. On my computer these paths are:
    C:\QtSDK\Desktop\Qt\4.8.1\mingw\bin
    C:\QtSDK\mingw\bin
    4. Copy All Plugins and Sdk directories to a temporary directory (let's say c:\temp)
    5. Open a command line, go to C:\Temp\All Plugins\BitOp\source and type qmake and then mingw32-make

    If everything builds correctly, now you're ready to make modifications on BitOp and make your own plugin.
    qt.png
    822 x 936 - 119K
    qt.png 118.7K
  • thanks @atilim

    when i go home i'll test! :)

    ps: is possible to call from c code native gideros functions (non gfx function ex: getBounds?)

    thanks! :)
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • atilimatilim Maintainer
    you're welcome :) Within C/C++, you can anything you want as in Lua.
  • GregBUGGregBUG Guru
    edited July 2012
    you're welcome :) Within C/C++, you can anything you want as in Lua.

    a little example ? :\"> (I mean calling gideros function from c code...)

    just installed (in win7) the qt-mingw system and now i can create (win desktop) plugins !!!! ;) wow!

    PS: which project template do you use in QTCreator ?

    in bitop
    you use

    -------------------
    QT -= core gui

    TEMPLATE = lib
    -------------------

    but i can't find in project templates...

    Dislikes: Yan

    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
    +1 -1 (+0 / -1 )Share on Facebook
  • atilimatilim Maintainer
    edited July 2012
    Here is an example of getting bounds of the stage. First the Lua code:
    local x, y, width, height = stage:getBounds(stage) -- as you know this is same as stage.getBounds(stage, stage)
    print("x: ", x)
    print("y: ", y)
    print("width: ", width)
    print("height: ", height)
    and the C code:
    lua_getglobal(L, "stage"); // push global stage variable to the stack
    lua_getfield(L, -1, "getBounds"); // push stage.getBounds function to the stack
    lua_getglobal(L, "stage"); // push global stage variable to the stack
    lua_getglobal(L, "stage"); // push global stage variable to the stack
    lua_call(L, 2, 4); // the number of arguments is 2 (stage, stage) and the number of results is 4 (x, y, width, height)
    lua_Number x =  lua_tonumber(L, -4); // lua_Number is double by default
    lua_Number y =  lua_tonumber(L, -3);
    lua_Number width =  lua_tonumber(L, -2);
    lua_Number height =  lua_tonumber(L, -1);
     
    printf("x: %f\n", x);
    printf("y: %f\n", y);
    printf("width: %f\n", width);
    printf("height: %f\n", height);
     
    lua_pop(L, 5); // leave the stack as it is: pop 4 numbers and the stage that's pushed at the first line
    I haven't tested it but most probably it doesn't have any errors.

    qmake has a couple of predefined templates like "lib" or "app" as described here: http://doc.qt.nokia.com/qmake-variable-reference.html#template here "lib" is used for creating dynamic libraries


  • thanks @atilim!
    i'm working hard this days trying to create new library for "fast" collision detection (bbox, obbox and circle collision) (without using box2d) using native code (where needed) and integrate into my animation system.

    for now (thanks to your suggestion) i successfully created plugin for windows (dll) and for mac... next step will be create libs for ios and andorid!

    I think I'll make "torrents" of questions next days!!! o->

    get ready! ~O)
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • I use wine gideros on linux, and compile the lib .so,but cann't test it,because it only support the dll lib. Is there a method to test??
  • I believe when you compile you can set the output type, by default it will make the .so file
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Thanks @OZApps,I will try it
  • Hi all,

    Here are the steps I usually follow:

    1. Download Qt from http://qt.nokia.com/downloads/ For windows, we'll be mostly using mingw and qmake, not Qt libraries. And I recommend to download offline installer (1.7 GB).
    2. Install it. But I use Qt only for desktop development. Therefore I deselect all the components related to mobile development (I've attached a screenshot)
    3. Add the paths qmake.exe and mingw32-make.exe's paths to the PATH environment variable. On my computer these paths are:
    C:\QtSDK\Desktop\Qt\4.8.1\mingw\bin
    C:\QtSDK\mingw\bin
    4. Copy All Plugins and Sdk directories to a temporary directory (let's say c:\temp)
    5. Open a command line, go to C:\Temp\All Plugins\BitOp\source and type qmake and then mingw32-make

    If everything builds correctly, now you're ready to make modifications on BitOp and make your own plugin.
    I followed these steps on mac

    I download the qt from http://releases.qt-project.org/qt4/source/qt-mac-opensource-4.8.4.dmg

    and for step 5 I run qmake and then open the generated Xcode project and build it and got an libmd5.dylib file

    but after I run it in the mac player ,it says

    ./md5.lua:22: loop or previous error loading module 'md5'
    stack traceback:

    I can run the code on the device player

    What steps am I missing?
  • there is the plugin file

    #include "gideros.h"
    #include "lua.h"
    #include "lauxlib.h"

    extern "C" {
    LUALIB_API int luaopen_md5_core(lua_State *L);
    }

    static void g_initializePlugin(lua_State *L)
    {
    lua_getglobal(L, "package");
    lua_getfield(L, -1, "preload");

    lua_pushcfunction(L, luaopen_md5_core);
    lua_setfield(L, -2, "md5");

    lua_pop(L, 2);
    }

    static void g_deinitializePlugin(lua_State *L)
    {
    }
    REGISTER_PLUGIN("MD5", "1.0")
  • atilimatilim Maintainer
    hmm.. interesting..
    1. did you copy libmd5.dylib to Plugins folder?
    2. can you execute otool -L libmd5.dylib and write the output here?
  • alexzhengalexzheng Guru
    edited December 2012
    Yes, I copied it to Plugins folder.

    Today,I build the bitop and copied the libbitop.dylib,it simply says
    main.lua:80: module 'bit' not found:

    the libmd5.dylib and libbitop.dylib file are compiled by myself, and the bitop.dylib is the original one(this one works).

    the libbitop.dylib file is copied from /Users/zhengyi/Library/Developer/Xcode/DerivedData/bitop-fpvmasurgekilzdckjgdcwrgmcbq/Build/Products/Debug


    zhengyimatoMacBook-Pro:Documents zhengyi$ otool -L libmd5.dylib
    libmd5.dylib:
    /usr/local/lib/libmd5.dylib (compatibility version 1.0.0, current version 1.0.0)
    liblua.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    zhengyimatoMacBook-Pro:Documents zhengyi$ otool -L libbitop.dylib
    libbitop.dylib:
    /usr/local/lib/libbitop.dylib (compatibility version 1.0.0, current version 1.0.0)
    liblua.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    zhengyimatoMacBook-Pro:Documents zhengyi$ otool -L bitop.dylib
    bitop.dylib:
    libbitop.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    @executable_path/../Frameworks/liblua.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
  • alexzhengalexzheng Guru
    edited December 2012
    I aslo built the bitop on windows, and it works, both the debug and release version of the dll work ok.

    I can not figure out what's wrong on mac. :((
  • atilimatilim Maintainer
    Your dynamic shared library install names are wrong: "liblua.1.dylib" should be "@executable_path/../Frameworks/liblua.1.dylib"

    If you look at bitop.pro, there is a line at the end of it:
    QMAKE_POST_LINK += install_name_tool -change liblua.1.dylib "<a href="https://forum.giderosmobile.com/profile/executable_path%2F" rel="nofollow">@executable_path/</a>../Frameworks/liblua.1.dylib" $(TARGET);
    You should apply the same step to libmd5.dylib.
  • alexzhengalexzheng Guru
    edited December 2012
    It seems QMAKE_POST_LINK does not excute automatically, Only after I run the command manually:
    install_name_tool -change liblua.1.dylib "@executable_path/../Frameworks/liblua.1.dylib" libmd5.dylib

    the final libmd5.dylib works.

    is anything wrong in my final step:
    step 5 I run qmake and then open the generated Xcode project to build it and got an libmd5.dylib file


    My pro file:

    QT -= core gui

    TARGET = md5
    TEMPLATE = lib

    INCLUDEPATH += ../../../Sdk/include

    SOURCES += \
    md5lib.c \
    md5.c \
    md5_stub.cpp \


    HEADERS +=

    LIBS += -L"../../../Sdk/lib/desktop" -llua

    macx {
    QMAKE_POST_LINK += install_name_tool -change liblua.1.dylib "@executable_path/../Frameworks/liblua.1.dylib" $(TARGET);
    }
  • atilimatilim Maintainer
    edited December 2012
    oh.. maybe there are some changes on Qt side. Anyway, I'm glad it works.

    And as a side note, if you're working with dynamic libraries on Mac, you need to use install_name_tool and otool -L a lot.
  • Thanks for your great help. :))

    I will take some time to be familiar with these tools.
  • @scouser Did you able to solve the problem? I encounter the same problem.
  • evsevs Member
    Hello,

    Here's how I got bitop working on my mac

    From the terminal type:

    1. cd /Applications/Gideros Studio/All Plugins/BitOp/source

    2. /Users/evs/QtSDK/Desktop/Qt/4.8.1/gcc/bin/qmake bitop.pro (where your qmake is)

    3. make

    4. copy libbitop.1.0.0.dylib to /Applications/Gideros Studio/Plugins

    In your Gideros Studio project add:
    require 'bit'
    cheers

    evs

  • I'm trying to build a Steam plugin for Gideros Desktop. This is the unique tutorial i found for this topic. I try to recreate the example cited on first post:

    require "myFirst"

    local result = myFirstPlugin.addTwoIntegers(14, 48)
    print("\n\nSample 1 - Result of addTwoIntegers:", result, "\n\n")
    With Visual Studio Express 2010 I created a DLL file that go on my plugin gideros directory. After run my test i got the following error:

    main.lua is uploading.
    Uploading finished.
    main.lua:1: module 'myFirst' not found:
    no field package.preload['myFirst']
    no file '.\myFirst.lua'
    no file 'C:\Program Files (x86)\Gideros\lua\myFirst.lua'
    no file 'C:\Program Files (x86)\Gideros\lua\myFirst\init.lua'
    no file 'C:\Program Files (x86)\Gideros\myFirst.lua'
    no file 'C:\Program Files (x86)\Gideros\myFirst\init.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\lua\myFirst.luac'
    no file '.\myFirst.dll'
    no file 'C:\Program Files (x86)\Gideros\myFirst.dll'
    no file 'C:\Program Files (x86)\Gideros\loadall.dll'
    stack traceback:
    main.lua:1: in main chunk
    It's curious, because instead i use the DLL provided by SatheeshJM and works fine. So i think there is something missing on the creation that cause this fail.

    On the other way, i tried to do with QT as atilim said. It's a bit confusing because i found a QT 5.5 version withg qmake and minggw32-make files. I use the BitOp project example provided by Gideros.

    C:\Mysia\temp\bitop\source>qmake -o Makefile bitop.pro

    C:\Mysia\temp\bitop\source>mingw32-make
    mingw32-make -f Makefile.Release
    mingw32-make[1]: Entering directory 'C:/Mysia/temp/bitop/source'
    Makefile.Release:65: *** missing separator (did you mean TAB instead of 8 spaces
    ?). Stop.
    mingw32-make[1]: Leaving directory 'C:/Mysia/temp/bitop/source'
    Makefile:34: recipe for target 'release' failed
    mingw32-make: *** [release] Error 2
    qmake generate the Makefiles in right way. But i got several errors when try to use mingw32-make. I reviewed the plugin process and i understand the BitOp technique in order to create comunication with Gideros Lua, but getting this problems on generating the right DLL file could be frustating.

    Any recomendations updated? Maybe the tutorials are too old. I'm using Windows 8.1 and Gideros 20915.09

  • ar2rsawseenar2rsawseen Maintainer
    edited October 2015
    Unfortunately you can't make it in Visual Studio, because it has MSVC compiler, you need to install QT, specifically with mingw, so it would use the correct compiler and then Gideros will understand the dll ;)

    The second part depends on what errors do you get, maybe it is the problem with steam SDK, that their code is not mingw compatible, for example
Sign In or Register to comment.