1

I would like to embed a scripting language(js, python, perl, even php, anything that's easy to use) in an OpenGL C++ application. I'd like to do this in order to be able to do things like:

  • print values of various C++ class members at runtime
  • cause interrupts that would wake up gdb at runtime
  • after I find a bug I'd like to write a small script oneliner to replicate it

I'm pretty sure this won't be easy. I want to ask whether this is a good/bad idea, and if it's worth the effort.

Example usecase

Let's suppose I rotate a line until it collides something and my collision detection has some SIGSEGV which occurs upon collision. I print out all the angles, find out which one was the one before the SIGSEGV and I write a small python thingie which displays some values so I can figure out what went wrong etc.

I guess basically What I'm trying to do is to avoid gdb and uhm.. I'd like if the program blows to have a way to check things in Python instead.

It's not that I don't like gdb, it's that I don't like the limited commands it has..

UPDATE: GDB can now be extended with Python out of the box. That solves a lot of the limitations of canned sequences of commands.

5 Answers 5

2

I don't think that for the purpose of debugging embedding a scripting language is a good idea. It's definitely possible, but for everything that you'd want to be able to access from the scripting language, you would have to provide some interface, since there's -- to the best of my knowledge at least -- no way to directly call C++ or read C++ data structures from a scripting language. I'd suggest you learn gdb or look for gdb frontends if you don't want to use gdb directly. I've used ddd and found it quite useful. The gdb frontend of Eclipse CDT is usable, too. Using a debugger gives you more flexibility, since the debugger knows about C++ and its data structures and thus allows you to inspect anything at runtime, without having to manually write much support code for that.

Sign up to request clarification or add additional context in comments.

Comments

1

First of all: You can print all those out using GDB quite easily. Once in GDB you might want to try what "help data" shows.

Which IDE do you use? You might want to try the cross-platform IDE Code::Blocks, which interfaces GDB quite neatly.

If you want to interface with another language, you might want to have a look on "Lua". It is very easy to learn. See http://en.wikipedia.org/wiki/Lua_%28programming_language%29

Lua is intended to be embedded into other applications, and accordingly it provides a robust, easy-to-use C API. The API is divided into two parts: the Lua core and the Lua auxiliary library.

This works well with C++, too, of course.

6 Comments

I understand what you mean. GDB is simple to use for simple stuff, and I know how to use it but I'd like more control. I don't use an IDE exactly because of that. I intend to build a console inside the application I'm writing in order to be able to query things and see values in there. I will consider your suggestion with Lua although I don't know the syntax of it. Do you know why Lua is so used in gamedev? I've seen it used many times, dunno why
1: GDB is not "simple" for "simple" stuff. You can debug almost anything, even injected machine code. (I am actually doing this currently while working on a JIT compiler.) You might want to read en.wikipedia.org/wiki/GDB#Features - There have been books published about it, because it is anything but simple. ;) --- 2: You can add extensions to your program using Lua very quickly. (Aka "Mods")
Upvote for Lua. I've used it with a simple OpenGL app and it did what I needed in that case (simple user-editable functions for generating motion).
@RahulBanerjee that's cool Rahul. Can you throw your code on github so I can have a look ? Thanks
Just forgot: For a little "By-example"-Overview of Lua this page is a good start: cc.byexamples.com/2008/06/07/how-to-embed-lua-51-in-c
|
0

i can´t help with the questions if it´s a good or bad idea.

but you could take a look at Chaiscript that´s really easy to use with c++

Comments

0

Python is a very good choice as application scripting language. Its not a strong argument but I know that lua is widely used in game programming (I d'ont know why), it may be useful since you are using OpenGL.

Comments

0

Use lua - it's simple stack based interpreter. With very clean API.

E.g.: Simple code that add dt_format|dt_convert functions to lua

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.