Calling functions from C (example.i) in TCL code - c

I have a file called example.i that allows the calling of a number of C functions.
Is it possible to call these functions from my TCL code? I have seen some ways to call C functions from TCL but I found them very hard to understand or incomplete, at least for a TCL rookie such as myself. How can this be done?

The .i suffix suggests you are generating an interface using swig. If so, swig has a -tcl option that should generate code for an extension you can load into Tcl - see Swig page on the Tcl wiki

It's pretty extensively documented already.
See here hello_world and here SWIG for example.

Related

Including C libraries in Lua

Most of the examples online of C-to-Lua implementations show the C program messing around with Lua States and Compilers and even compiling both files in a special way to work properly.
But is there a way to call a C function from Lua without the C program knowing it will be used in a Lua program?
I mean something like loading a Lua library: to do that, I just call
module = require("/path/library")
module.doSomething()
from a standard Lua interpreter, while the examples of C-to-Lua I found online say not only you have to modify your C program to fit whit Lua, but you also need to compile them in a special way and things like that.
So, again, is it possible to call a C function from a standard Lua interpreter whitout the C module knowing it will be used in Lua?
I don't know if it can be of any help, but I'm using LuaJIT.
Use the FFI library that is integrated into LuaJIT.

calling c functions from lua script

I found some references on this but I was not able to make them work. I have a Debian box with mysql and mysql-proxy. I am intercepting the SQL queries with the LUA script.
function read_query(packet)
if packet:byte() ~= proxy.COM_QUERY then
print("error read (COM_QUERY)")
end
local query = packet:sub(2)
print ("query : " .. query )
//Transformation here
return proxy.PROXY_SEND_QUERY
end
I want to parse and process the query so I can rewrite it with some c functions I already have developed. I am trying to find the way to call this fucntions but the only way I have found asumes that the c MAIN function starts the LUA registering process.
Is there any way to make the LUA script call the function in a compiled C file?
Any example of how should I make (LUA) and receive (C) the call?
Extract from lua.org
When we say that Lua can call C functions, this does not mean that Lua can call any C function.(There are packages that allow Lua to call any C function, but they are neither portable nor robust.) ...
... for a C function to be called from Lua, we must register it, that is, we must give its address to Lua in an appropriate way.
You should have a look here
SWIG is a good option to generate bindings for you: www.swig.org. You create a .i file in which load your C headers, and SWIG generates all the binding code for you. You then compile the generated code, link it to your C library and Lua library, and in your script you put require 'yourCLibrary', you can do what you want. Very practical, and your wrapper can be used to access yourCLibrary from other languages like Python and C#.

Calling C function from lua

Can someone tell me is it possible to somehow call c function or simply wrap it into a lua function WITHOUT building a new module.
If it works for you, try the FFI library. See also luaffi.
Lua can't call arbitrary C functions - they have to be bound to something in the Lua namespace first. (This is intentional to prevent breaking out of sandboxes in embedded applications.)
Or the Alien library.

How would I make a C-function that I can call from Lua?

I would like to know how to make a C-function and be able to tell Lua about it, then call it from Lua. I have all the Lua Libraries installed on my Mac OSX 10.4 computer.
There's an excellent example of Lua-C integration here and here.
If you just need to export a function into the global namespace, then:
Declare the function (let's call it f) with signature lua_CFunction.
Call lua_register(L, "myfunc", f), with L being the Lua state, and function = f.
Run the lua code. Then f will be available in the global namespace as myfunc.
If you're going to use the stock interpreter then you might want to make a library. This guy wrote an article for Lua Programming Gems that explains how to do it. The sources are available online.
You can register functions using luaL_register
Look at some examples and explanation in PiL
My answer here includes a nice, short example about making a very simple game using C and Lua together. In my biased opinion, it's a great starting point.

How do I compile Perl code within a C program?

I have a C program with an embedded Perl interpreter. I want to be able to precompile some Perl code from within the program. How do I do that?
Rationale (if anyone is interested) is to be able to compile it once, store the parse tree, and execute many times (as long as the compiled code does not change).
Thanks!
Madhu
PS: I am using Perl-5.8, though it would be good to know if Perl-6.0 makes this easier in any way.
This is the default behavior when you embed the Perl interpreter in a C program. After you've run perl_parse() to parse the Perl program, you can use perl_run() and call_argv() over and over with the same parsed Perl program.
You can use perlcc to create executable or C sources.
To compile your C code, you would need to use perl's library (could be -lperl or -llibperl).
In reference to Perl 6, it's not complete. But 5.10.0 might have some bug fixes over 5.8.

Resources