I'm trying to call a function from a C project but I don´t know how to do it.
Here is my code ("treatments.c"):
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
static int treatments_load_image (lua_State * L) {
lua_pushnumber(L,10);
return 1;
}
static const luaL_Reg RegisterFunctions[] =
{
{ "treatments", treatments_load_image },
{ NULL, NULL }
};
int luaopen_treatments(lua_State *L)
{
lua_newtable(L);
#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, LuaExportFunctions);
#else
luaL_setfuncs(L, LuaExportFunctions, 0);
#endif
return 1;
}
In my .lua file, I´m trying to do something like this:
local treatments = require 'treatments'
And I get the error below:
lua: run.lua:15: module 'treatments' not found:
no field package.preload['treatments']
no file './treatments.lua'
...
The ".c" file is in the same folder than ".lua" file. I'm not using any MAKEFILE file.
If it helps, I'm using Lua 5.1 =)
Thanks!!
The ".c" file is in the same folder than ".lua" file. I'm not using
any MAKEFILE file
No, you must build your c source file into shared library (dll for windows, or so for linux), then put that shared library in lua package.cpath, see http://lua-users.org/wiki/ModulesTutorial
Lua's require deals with files containing Lua code or shared libraries in the case of C. You need to compile the C source code into a shared library and then load that, which should "return" a table (i.e. push it onto the stack), as usual.
Related
I want to experience with the Lua internals a little bit and affiliate myself with them. I figured digging into the C API would give me some neat knowledge. I wrote up a very small module (parser.dll) in pure C:
#include "parser.h"
#pragma comment(lib, "lua5.4.4.lib")
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int luaL_parse(lua_State* L) {
const char* path = lua_tostring(L, 1);
const char* cwd = lua_tostring(L, 2);
lua_getglobal(L, "print");
lua_pushstring(L, "Did this run?");
lua_callk(L, 1, 0, 0, NULL);
return 0;
}
static const struct luaL_Reg exports[] = {
{"parse", luaL_parse},
{NULL, NULL}
};
LUAMOD_API int luaopen_parser(lua_State* L) {
luaL_newlib(L, exports);
return 1;
}
Parser.h:
#pragma once
#ifndef lua_h
#include <lua.h>
#include <lauxlib.h>
#endif
int luaL_parse(lua_State* L);
And it compiles successfully. Whenever I try to require it (via require("parser")), I read this:
lua54.exe: error loading module 'parser' from file 'C:\Users\user\Desktop\Lua\parser.dll':
The specified module could not be found.
I feel like there's more of a mishap in my vs19 project configuration but I don't believe I can dump a text file of that configuration here. If you've any tips, please let me know! I'm not sure how to traverse the problem anymore, I've been attempting to hack at it for a little while.
One detail that may be important is that I'm building with the Lua 5.4.4 source code. As in, I downloaded the source, and just added it as an additional include directory.
On the Lua side, I don't get different results with package.loadlib either. That gives me nil even with funcname # "*".
Update, I went through a dependency walker (https://github.com/lucasg/Dependencies) and when I click on the Lua.5.4.4.dll dependency, it says it couldn't be found on the disk. Could the lib file perhaps be messed up? I'm not sure because the program compiles fine still, no unresolved symbols.
I am using qt and created a c project. I have created two files one header file and source file. I have declare a function into header file. So that I can call it from main function. But when I compile and run, I got "undefined reference to " error. How to solve this issue ? I am using qt 5.5 ide.
My Code:
header file
chapter_1.h
#ifndef CHAPTER_1_H
#define CHAPTER_1_H
//include all header files
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* function declaration */
int sum(int x, int y);
#endif // CHAPTER_1_H
source file
//include header files
#include "chapter_1.h"
int sum(int x, int y)
{
int result = x+y;
return result;
}
main file:
#include "chapter_1.h"
int main()
{
sum(23, 23);
return 0;
}
It's not a compiler error. It's a linker error. You just need to include both source files (main.cpp and chapter1.cpp) into your project.
I solved the problem. The problem was, file created as .cpp. Now I have changed it into .c and not it worked. Thanks to all.
You must tell the qmake which are the source files that you want to use to generate program. There is a couple of variables defined in project file ( *.pro) which are responsible for this and other information. SOURCES defines the source files to use, HEADERS - you guessed, the headers.
HEADERS = chapter_1.h
SOURCES = main.cpp chapter_1.cpp
I am restricted by very specific (and rather senseless...) filing system and I can't create header files or split existing files into files holding only functions and only execution calls. I can only create .c files that can be executed and have some sort of output.
A lot of code can be reused but I am being forced to copy some functions from file to file.
Is it possible to cure this mess by including but not compiling one or more file or omitting some functions in the included files? Maybe adding some debugging into the mix can allow to compile only part of included files?
Put the shared code like this, so that it can compile on it's own and at the same time you can include it from other .c files without getting duplicate main functions:
// shared.c
#ifndef SHARED_C
#define SHARED_C
#if __INCLUDE_LEVEL__ == 0
# include <stdio.h>
# include <stdlib.h>
int main() {
fprintf(stderr, "shared file, not to run!\n");
return EXIT_FAILURE;
}
#endif
int shared_func() { return 1; }
#endif
And use it from other files
#include "shared.c"
int x = shared_func();
Can you use the pre-processor? Something like:
// this is file1.c
int Foo(int bar);
#ifndef HEADERS
int Foo(int bar)
{
return 42;
}
#endif
and
// this is file2.c
#define HEADERS
#include "file1.c"
Foo(42);
I am new to eclipse, developing c program in eclipse
I am creating multiple source files in same project, could some one help me to create .h file for main () function and to call in multiple source file
for instance if I have created main.c file, now how to call this main.c into another .c file
The main() function should not be in a header file. It should be in one and only one .c file.
An example of simple layout can be:
//header.h
#ifndef MY_HEADER <----Notice the Inclusion Guards, read more about them in a good book
#define MY_HEADER
void doSomething();
#endif //MY_HEADER
//header.c
#include "header.h"
void doSomething()
{
}
//Main.c
#include "header.h"
int main(void)
{
doSomething();
return 0;
}
But please pick up a good book to learn these basics, You definitely need one.
I'm trying to embed LuaJIT into a C application. The code is like this:
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
int barfunc(int foo)
{
/* a dummy function to test with FFI */
return foo + 1;
}
int
main(void)
{
int status, result;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
/* Load the file containing the script we are going to run */
status = luaL_loadfile(L, "hello.lua");
if (status) {
fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
exit(1);
}
/* Ask Lua to run our little script */
result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
exit(1);
}
lua_close(L); /* Cya, Lua */
return 0;
}
the Lua code is like this:
-- Test FFI
local ffi = require("ffi")
ffi.cdef[[
int barfunc(int foo);
]]
local barreturn = ffi.C.barfunc(253)
io.write(barreturn)
io.write('\n')
It reports error like this:
Failed to run script: hello.lua:6: cannot resolve symbol 'barfunc'.
I've searched around and found that there're really little document on the ffi module. Thanks a lot.
ffi library requires luajit, so you must run lua code with luajit.
From the doc:
"The FFI library is tightly integrated into LuaJIT (it's not available as a separate module)".
How to embed luajit?
Look here http://luajit.org/install.html under "Embedding LuaJIT"
Under mingw your example run if i add
__declspec(dllexport) int barfunc(int foo)
at the barfunc function.
Under Windows luajit is linked as a dll.
As misianne pointed out, you need to export the function, which you can do by using extern if you are using GCC:
extern "C" int barfunc(int foo)
{
/* a dummy function to test with FFI */
return foo + 1;
}
If you are experiencing problems with undefined symbols under Linux using GCC, take care to have the linker add all symbols to the dynamic symbol table, by passing the -rdynamic flag to GCC:
g++ -o application soure.cpp -rdynamic -I... -L... -llua
For those of you trying to make this work on Windows with VC++ (2012 or later), using the C++ compiler:
make sure you use the .cpp extension, as this will do C++ compilation
make the function have external C linkage so that ffi can link to it, with extern "C" { ... }
export the function from the executable, with __declspec(dllexport)
optionally specify the calling convention __cdecl, not required because should be it by default and not portable
wrap the Lua headers in an extern "C" { include headers }, or better just #include "lua.hpp"
#include "lua.hpp"
extern "C" {
__declspec(dllexport) int __cdecl barfunc(int foo) {
return foo + 1;
}}