Including C libraries in Lua - c

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.

Related

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#.

Turning strings into code?

So let's say I have a string containing some code in C, predictably read from a file that has other things in it besides normal C code. How would I turn this string into code usable by the program? Do I have to write an entire interpreter, or is there a library that already does this for me? The code in question may call subroutines that I declared in my actual C file, so one that only accounts for stock C commands may not work.
Whoo. With C this is actually pretty hard.
You've basically got a couple of options:
interpret the code
To do this, you'll hae to write an interpreter, and interpreting C is a fairly hard problem. There have been C interpreters available in the past, but I haven't read about one recently. In any case, unless you reallY really need this, writing your own interpreter is a big project.
Googling does show a couple of open-source (partial) C interpreters, like picoc
compile and dynamically load
If you can capture the code and wrap it so it makes a syntactically complete C source file, then you can compile it into a C dynamically loadable library: a DLL in Windows, or a .so in more variants of UNIX. Then you could load the result at runtime.
Now, what normally would lead someone to do this is a need to be able to express some complicated scripting functions. Have you considered the possibility of using a different language? Python, Scheme (guile) and Lua are easily available to add as a scripting language to a C application.
C has nothing of this nature. That's because C is compiled, and the compiler needs to do a lot of building of the code before the code starts running (hence receives a string as input) that it can't really change on the fly that easily. Compiled languages have a rigidity to them while interpreted languages have a flexibility.
You're thinking of Perl, Python PHP etc. and so called "fourth generation languages." I'm sure there's a technical term in c.s. for this flexibility, but C doesn't have it. You'll need to switch to one of these languages (and give up performance) if you have a task that requires this sort of string use much. Check out Perl's /e flag with regexes, for instance.
In C, you'll need to design your application so you don't need to do this. This is generally quite doable, as for its non-OO-ness and other deficiencies many huge, complex applications run on well-written C just fine.

Can c++ libraries run in c?

I'm sorry if this is a basic question(I'm new to c/c++, but I'm a little confused at how to get the answer. stxxl is a c++ library but some of my code is in c. I know c++ can use c code(my c code is embedded in c++), but does it work the other way around so c can run c++ code?
Their site only mentions c++ but I'm wondering if there's something special that can be done to run c++ libraries within c?
Sorry the books I have read talk about using c code in c++ and the c book I read was written before c++ came out. Right now my c function is sending data to my c++ code which is using the c++ library and then sending results back so I'm thinking I want to test performance if I cut the middle man(c++).
You can link to a C++ library from C only when the C++ library has been designed to be used from C. Specifically, the functions the library provides need to be exported with extern "C" {} block to avoid name mangling, and the interface should be designed in a way to be usable from plain C (i.e. no classes or member functions, only functionless structs and plain functions).
It is worth mentioning that you can compile your C code with a C++ compiler, and it will for the most part be OK. This lets you pretend that your C code is a C++ code, and freely mix in functionality provided through C++ - specific interfaces.
Here's a links that may help you:
How to mix C and C++

How do I use C libraries in assembler?

I want to know how to write a text editor in assembler. But modern operating systems require C libraries, particularly for their windowing systems. I found this page, which has helped me a lot.
But I wonder if there are details I should know. I know enough assembler to write programs that will use windows in Linux using GTK+, but I want to be able to understand what I have to send to a function for it to be a valid input, so that it will be easier to make use of all C libraries. For interfacing between C and x86 assembler, I know what can be learned from this page, and little else.
One of the most instructive ways to learn how to call C from assembler is to:
Write a C program that calls the C function of interest
Compile it, and look at the assembly listing (gcc -S)
This approach makes it easy to experiment by starting with something that is already known to work. You can change the C source and see how the generated code changes, and you can start with the generated code and modify it yourself.
push parameter on the stack
call the function
clear the stack
The links you have in your question show all these steps.
The OS may define the calling standard (it pretty well must define the standard for invoking system calls), in which case you need only find where that is documents and read it closely.

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

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.

Resources