Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Does gideros really support calling C on Mac? — Gideros Forum

Does gideros really support calling C on Mac?

sielersieler Member
edited February 2015 in General questions
I've been trying to use 'require' to load (and later call) a C function form Gideros.

If anyone has succeeded in calling a C function from Gideros on a (recent) Mac OSX, I'd
be really interested in knowing the steps you had to take!

(SatheeshJM's nice writeup is Windows specific, unfortunately.)

I'm on Yosemite, using a Mac Mini ... my normal R&D machine is bigger, faster, and on Snow Leopard :(

When I try to do: require "addtwo" I get this error:

dynamic libraries not enabled; check your Lua installation

Hmm... *WHAT* lua installation? Gideros comes with a copy of lua, in:

/Applications/Gideros Studio/Gideros Studio.app/Contents/Tools/lua

BUT... that's (a) lua 5.1.4, and (b) is **NOT** marked executable.
Additionally, after a run of sample program via Gideros Studio & Gideros Player,
we see that the access date does not change.

In short, the only obvious copy of lua that comes with Gideros Studio (on the Mac)
is never used.

So, perhaps it's compiled and linked into "Gideros Studio" (the executable for the app,
a file of about 1 MB)? Not obviously (I used "nm" to dump the symbols).

When I first started testing Gideros, lua wasn't installed on my Mac.
I've since installed it (downloaded, compiled, installed i /usr/local), and I conjectured
that GS was smart enough to use the local/newer lua if avaiable.
But, no ... it's not accessing it, either.

Ah .. Gideros *PLAYER*.

I see that it includes the lua dylib, and has externals like lua_tointeger ...
so I'm assuming the player dynamically loads the lua that is in
/Applications/Gideros Studio/Gideros Player.app/Contents/Frameworks/liblua.1.dylib.

But, that begs the question:

Does "check your installation" mean I should be rooting around "in the .app" and
potentially replacing liblua.1.dylib with the one I built myself (and which I know
was compiled with -DLUA_USE_MACOSX, and therefore supports dynamic loading)?

BTW, having a blank in the file name of "Gideros Studio" and "Gideros Player" is *VERY* annoying!
I highly recommend changing it to an underscore, or dropping it altogether (GiderosStudio).

And, why is there a non-functiona lua bundled with Gideros Studio?

thanks,

Stan

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited March 2015
    Hello @sieler
    there are two ways.

    1) creating a plugin, where you could probably take an existing plugin (like https://github.com/gideros/giderosplugins/tree/master/Clipper/source where you mainly be interested in clipper.pro and clipperbinder.cpp) for desktop and build it with QT5 (you don't need to use QT specifics, you can use plain C here, it only needs to be built with QT cause Gideros itself uses QT on desktop) to get a dylib and put it in Plugins folder inside Gideros installation

    2) is using ffi through LuaJIT, firstly you would need to enable luajit on your Gideros installation: https://github.com/gideros/giderosplugins/tree/master/LuaJIT
    And then you could use ffi:
    http://luajit.org/ext_ffi.html
  • thanks...but that seems to be several steps past my problem. I should have mentioned that I've put my stuff in the plugins folder ... and I've also tried calling the sample plugins (like "bitop") ... in all cases I can't get past the "dynamic libraries not enabled" problem.

    I'm wondering: perhaps the free version of gideros deliberately omits this fuctionality?

    BTW, I'm creating my dynamic library via:

    ld -macosx_version_min 10.10 -dylib -ldl liblua.a addtwo.o -o addtwo.so

    ...would be nice to actually see a working Mac example documented on the gideros site :)


    The forum code wanted me to click 'accept' or 'reject' ... without explaining the semantics of either. Fearing that 'accept' might mark my question as "answered", which could limit future viewing/answers, I'm clicking on "reject". I wish I had a third option: "thanks, but although interesting it doesn't really solve the question".

    BTW, I'm trying to do all the C stuff via the command line, I don't even have QT installed.
    Why? I'm doing cross-platform development across many systems, some of which can't spell the word GUI :)

    thanks,

    Stan
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2015
    Have not done Mac plugin development I think in almost a year now, but I remember there was an addtional step of linking correct lua lib, that was in Gideros player like this:
    install_name_tool -change liblua.1.dylib "<a href="http://forum.gideros.rocks/profile/executable_path%2F" rel="nofollow">@executable_path/</a>../Frameworks/liblua.1.dylib" yourlibname
    And please provide your C code, cause you need to make the function available to
    LuaVM
  • Tried that, too :)
    BTW, I tried replacing the lua that Player uses, but your version had three C functions apparently not in standard: lua_isjit, lua_getprintfunc, and lua_setprintfunc. Adding dummy ones let Player load, but it won't usefully run (can't then load secondary .lua files the user app needs).

    You're missing an important point: It's not getting to the point where it can find or not find the function! Why? Because when you compiled your version of lua, included with Player, none of: LUA_DL_DLOPEN, LUA_DL_DLL, LUA_DL_DYLD were defined (note that this is for lua 5.1 ... lua 5.3 uses slightly different flags, you'd want to compile with LUA_USE_MACOSX).

    Perhaps you didn't compile Lua yourself, but just downloaded Mac binary of it?
    (There might have been more choices for the binaries ... perhaps some were built without the dynamic load ability, and some with.)

    BTW, I've tried slipping in 5.3 ... same problem with same missing C functions.

    --- my C code ... which works with command line lua 5.1 and 5.3 (from main.lua, below)
    Note that this isn't great code, it's the (probable) minimal needed to test with.

    Attached: addtwo.c, main.lua, Makefile

    wait... "upload file" rejects a file with suffix ".c". Sheesh.
    Trying as a tarball now... argh...disallowed.
    Trying after renaming "foo.tar" as "footar" ...
    disallowed.

    Can *any* kind of file be uploaded?

    I'll paste the files at the end of this note...sorry.

    Sample use with command line:

    sieler$ make
    gcc -c addtwo.c -o addtwo.o
    gcc -dynamiclib -undefined suppress -flat_namespace addtwo.o -o addtwo.so

    sieler$ lua main.lua
    Lua version: Lua 5.3
    require addtwo...
    in luaopen_addtwo
    in addtwo, finally!
    200

    The best way of helping is probably to simply show me something that works, preferably including how the .so was built ... which ought to be in the documentation anyway.
    (Once an answer is found, I'd be happy to write a small doc on how to do this :)

    Why? An approach of "let's fix yours" is likely to spend a lot of time on things that aren't relevant because of lower-level problems. (Particularly if my suspicion that this is based on having the wrong liblua.1.dylib in Player is right :)

    BTW, I tried using the liblua1.dylib from /Applications/Gideros Studio/Sdk/lib/desktop/liblua.dylib, and (after using install_name_tool appropriately), found that it also lacked dynamic library code.

    Aside: you might ask yourself: why does GS ship with *THREE* *different* copies of liblua.1.dylib? At the minimum, they should be the same copy, and then two of the three should probably be symlinks to the master one.

    thanks,

    Stan

    ------cut here for Makefile -----------------
    CC= gcc

    CFLAGS= -m64 -g -Wall -D__mac__ -Wall -Wno-unused-function -I ..

    MODULES= addtwo.o addtwo.so

    default_target: all

    all: $(MODULES)

    addtwo.o: addtwo.c
    $(CC) $(CFLAGS64) -c addtwo.c -o addtwo.o

    addtwo.so: addtwo.o
    $(CC) -dynamiclib -undefined suppress -flat_namespace addtwo.o -o addtwo.so

    clean:
    rm -f $(MODULES)

    ------cut here for addtwo.c ------
    // addtwo.c

    #include
    #include

    #include "lua.h"

    int addtwo (lua_State *L);

    //==========================================================================
    int luaopen_addtwo(lua_State *L){

    printf ("in luaopen_addtwo\n");

    lua_register ( L, /* Lua state variable */
    "addtwo", /* func name as known in Lua */
    addtwo); /* func name in this file */

    return 0;

    }
    //==========================================================================
    int addtwo (lua_State *L)
    {

    int firstInteger, secondInteger, result;

    printf ("in addtwo, finally!\n"); // debug output

    // retrieve the two integers from the stack

    firstInteger = lua_tointeger (L, -1);
    secondInteger = lua_tointeger (L, -2);

    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;

    } // end addtwo proc
    //=======================================================================

    ------cut here for main.lua -----
    print ("Lua version: " .. _VERSION)

    print ("require addtwo...")

    require "addtwo"

    print (addtwo (100, 100))
    --------------that's all --------------
  • ar2rsawseenar2rsawseen Maintainer
    Hello ;)
    I think what you missing is some Gideros headers,macros to hook the plugin creation time.
    But as you said, it is easier to provide you with example:

    Here's my take on the function with the project and generated dylib file, which you can drop into Plugins directory in your Gideros installation, restart the player and use as:
    require "addtwo"
    print(addtwo.addtwo(1,2))
    zip
    zip
    AddTwo.zip
    15K
  • Will try...thanks!
Sign In or Register to comment.