Calling C function from lua - c

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.

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.

Is there mmseg in Go or can I call a self-defined C function from Go?

I want to use mmseg in Go as in other languages, for example, in python you can use pymmseg
So, is there is a similar Go package?
Or, how can I call the C functions from Go?
Or, how can I call shared library (*.so) from Go like in python?
you can call use C Libs as described in this tutorial:
http://cheesesun.blogspot.de/2009/12/basic-cgo.html
and:
http://siganakis.com/linking-c-libraries-with-go-lang-and-stdc99
or you may have a look at this library:
https://github.com/awsong/MMSEGO
its for chinese, but you may could use it to put your own algorithm there.
Use cgo to call C code from Go.
Command cgo
C? Go? Cgo!

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.

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 one can achieve late binding in C language?

How one can achieve late binding in C language ?
Late binding is not really a function of the C language itself, more something that your execution environment provides for you.
Many systems will provide deferred binding as a feature of the linker/loader and you can also use explicit calls such as dlopen (to open a shared library) and dlsym (to get the address of a symbol within that library so you can access it or call it).
The only semi-portable way of getting late binding with the C standard would be to use some trickery with system() and even that is at least partially implementation-specific.
If you're not so much talking about deferred binding but instead polymorphism, you can achieve that effect with function pointers. Basically, you create a struct which has all the data for a type along with function pointers for locating the methods for that type. Then, in the "constructor" (typically an init() function), you set the function pointers to the relevant functions for that type.
You still need to include all the code even if you don't use it but it is possible to get polymorphism that way.
Symbol binding in C is always done at compile time, never runtime.
Library binding, or dynamic linking as it's called, is done via dlopen() and dlsym() on *nix, and LoadLibrary() and GetProcAddress() on Windows.
How one can achieve late binding in C language ?
The closest would be through dynamic loading of library (DLL) such as with dlopen & dlsym on Linux. Otherwise, it is not directly available in C.
Use Objective-C or Lua. Both are late-bound languages which can easily interface with C.
Of course you could implement your own name resolution scheme, but why re-invent the wheel?
Unfortunately you did not specify an OS. For Unix you can use shared libraries, or create a configurable (plugin) module structure. For details you may find the source code of an apache 1.3 webserver useful. http://httpd.apache.org/download.cgi
cppdev seems to be the one and only to hit the spot with his/her remark.
Please, have a look at the definition itself. In a few words:
Late binding, or dynamic binding, is a computer programming mechanism
in which the method being called upon an object is looked up by name
at runtime.
All other answers just miss the main point, that is "look up by name".
The needed solution would be very similar to a lookup table of pointers to functions along with a function or two to select the right one by name (or even by signature). We call it a "hash table".

Resources