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

Create your own binding with swig #25

Closed
igoumeninja opened this issue Mar 27, 2015 · 20 comments
Closed

Create your own binding with swig #25

igoumeninja opened this issue Mar 27, 2015 · 20 comments

Comments

@igoumeninja
Copy link

Hi Dan, again,
I am trying to create my own binding by following your instruction. I am taking the following errors:
screen shot 2015-03-27 at 7 54 00 pm

The project is here: https://github.com/igoumeninja/Gea/tree/working_branch
If you find some time to help, I 'll be greattful.
Thanks in advance

@danomatika
Copy link
Owner

Add "#include "MyCode.h" to the top of your interface file and regenerate the bindings:

%module my
%{
    // include any needed headers here
    #include "ofMain.h"
    #include "MyCode.h"
%}

Also, you may not need to include ofMain.h if you're not using any openFrameworks classes, functions, etc.

@danomatika
Copy link
Owner

Also, you'll need to create an implementation for those functions & class methods. The example code is not actually tested.

@danomatika
Copy link
Owner

I updated the example in the readme which should actually compile now.

@igoumeninja
Copy link
Author

Thanks again Dan, there is typographical error at

            aString = "hello world"i;

an extra i.

@danomatika
Copy link
Owner

I fixed it already. Lots of typos, should be fine now.

@igoumeninja
Copy link
Author

It's working.
Thanks again and greetings from Greece.

@igoumeninja
Copy link
Author

Something more Dan, although the project has a successful compile with the following lua script:


function setup()
   of.setWindowTitle("startUp")
   of.background(0)

   aNumber = my.function(3.45)

end
----------------------------------------------------
function update()

end
----------------------------------------------------
function draw()
   of.fill()
   of.setColor(0,0,0,20)
   of.rect(0,0,of.getWidth(), of.getHeight())

   of.setColor(250,230,230,230)
   of.rect(100,100,100,20)
end
----------------------------------------------------
function keyPressed(key)
   if key == string.byte("s") then
   end
end

I am taking the error:

[notice ] got a script error: ...sx_release/apps/GEA/gea_swig/bin/data/scripts/my-lua-scripts/startup.lua:6: '<name>' expected near 'function' 

@igoumeninja igoumeninja reopened this Mar 27, 2015
@danomatika
Copy link
Owner

Might be because the function name is wrong. It's actually: "my.myFunction" and the class constructor is "my.MyCoolClass". I'll update the example.

@igoumeninja
Copy link
Author

Change to

   aNumber = my.myFunction(3.45)

taking the result:

[notice ] got a script error: Error running setup(): ...gea_swig/bin/data/scripts/my-lua-scripts/startup.lua:6: attempt to index global 'my' (a nil value)
[verbose] ofxLua: Cleared state

@danomatika
Copy link
Owner

Did you open your custom lua lib after initing the lua state? You have to do this every time you call init(). https://github.com/danomatika/ofxLua#opening-your-lua-library

@igoumeninja
Copy link
Author

Yes, here is my ofApp.cpp file https://github.com/igoumeninja/Gea/blob/working_branch/src/ofApp.cpp.

@danomatika
Copy link
Owner

But not after this one: https://github.com/igoumeninja/Gea/blob/working_branch/src/ofApp.cpp#L37 :D Calling init() again completely clears the state.

@danomatika
Copy link
Owner

@igoumeninja
Copy link
Author

I add

 luaopen_my(lua);

at positions, but still got an error.

[notice ] got a script error: Error running setup(): ...gea_swig/bin/data/scripts/my-lua-scripts/startup.lua:4: attempt to call field 'myFunction' (a nil value)
[verbose] ofxLua: Cleared state

@danomatika
Copy link
Owner

You broke it here: https://github.com/igoumeninja/Gea/blob/working_branch/src/MyBindings.i

My earlier comment about adding the #include "MyCode.h" line didn't mean to replace all of the text with that, you still need to run the %include "MyCode.h" which actually tells SWIG what it should generate bindings for. As you have it now, you only tell SWIG you want to create a "my" lua module but nothing else which is why "my.myFunction()" fails since there's nothing there.

As it says in my readme updates, your interface file should contain this:

%module my
%{
    // include any needed headers here
    #include "MyCode.h"
%}

// include custom code you want to be wrapped,
// note the '%' instead of '#' as '%include' is a SWIG
// command to wrap code in the given header
%include "MyCode.h" 

@danomatika
Copy link
Owner

You can check what's loaded in the lua state by printing all the tables after init() and luaopen_my: lua.printTable().

@igoumeninja
Copy link
Author

I change the MyBindings.i file, run swig -c++ -lua -fcompact -fvirtual -I../../../libs/openFrameworks MyBindings.i added to the project the new file and getting the error:
screen shot 2015-03-27 at 9 46 35 pm

@danomatika
Copy link
Owner

Remove #include "MyCode.h" from ofApp.h. You don't actually need to include it anywhere since it's built into the lua module.

@igoumeninja
Copy link
Author

DONE, thanks a lot Dan. Have a nice evening. I getting out to a concert: https://www.youtube.com/watch?v=TfoisYwKAVg. Have a nice day!

@danomatika
Copy link
Owner

There's one last thing you'll need to update. See the latest version of the example MyBindings.i in https://github.com/danomatika/ofxLua#swig-interface

I forgot to add the %includes SWIG needs in order to correctly handle std::string conversion to lua strings. I tested with you're code and the example now works correctly. Thanks for going through this so we know it works.

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

No branches or pull requests

2 participants