I want to make so that I can call lua functions from my C program but I dont know how to load the lua library.
So far I have tried to download all the lua source code from lua.org, and then put them into the same folder as my C program. And then included them in the C code.
How my code looks now:
#include "lua.h"
#include "lauxlib.h"
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, "test.lua");
return 0;
}
Error msg when compiling:
test.c: In function ‘main’:
test.c:10:5: warning: implicit declaration of function ‘luaL_openlibs’ [->Wimplicit-function-declaration]
luaL_openlibs(L);
^
In file included from test.c:5:0:
lauxlib.h:122:24: warning: value computed is not used [-Wunused-value]
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
^
test.c:11:5: note: in expansion of macro ‘luaL_dofile’
luaL_dofile(L, "test.lua");
^
/tmp/ccHJTAY8.o: In function main':
test.c:(.text+0x9): undefined reference toluaL_newstate'
test.c:(.text+0x1e): undefined reference to luaL_openlibs'
test.c:(.text+0x34): undefined reference toluaL_loadfilex'
test.c:(.text+0x5f): undefined reference to `lua_pcallk'
collect2: error: ld returned 1 exit status
Related
This is the following program.
#include <graphics.h>
int main(void)
{
drawRect(50,100,200,150);
drawLine(50,100,150,25);
drawLine(150,25,250,100);
drawRect(130,190,40,60);
drawRect(70,195,40,30);
drawRect(190,195,40,30);
drawRect(70,125,40,30);
drawRect(190,125,40,30);
return 0;
}
and following is the error
C:\Users\Balavardhan\Desktop\New folder\c language>gcc z.c -o z.exe
z.c: In function 'main':
z.c:4:6: warning: implicit declaration of function 'drawRect'; did you mean 'drawpoly'? [-Wimplicit-function-declaration]
drawRect(50,100,200,150);
^~~~~~~~
drawpoly
z.c:5:6: warning: implicit declaration of function 'drawLine'; did you mean 'drawpoly'? [-Wimplicit-function-declaration]
drawLine(50,100,150,25);
^~~~~~~~
drawpoly
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x2e): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x52): undefined reference to `drawLine'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x76): undefined reference to `drawLine'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x9a): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0xbe): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0xe2): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x106): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x12a): undefined reference to `drawRect'
collect2.exe: error: ld returned 1 exit status
Can anyone tell me how can I solve this problem?
This question already has answers here:
Ordering of object files and libraries in static linking
(1 answer)
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 3 years ago.
I try to Compile this Simple Lua Tutorial Program:
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void) {
char buff[256];
int error;
lua_State *L = lua_open(); /* opens Lua */
luaopen_base(L); /* opens the basic library */
luaopen_table(L); /* opens the table library */
luaopen_io(L); /* opens the I/O library */
luaopen_string(L); /* opens the string lib. */
luaopen_math(L); /* opens the math lib. */
while (fgets(buff, sizeof(buff), stdin) != NULL) {
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
}
lua_close(L);
return 0;
}
With the Following Command:
gcc -I/usr/include/lua50 -L/usr/lib/liblua50.a -llua50 luainterpret.c
So the Headers are Linked and Library Binary should also be Linked right?
However i get the following undefined References:
/tmp/ccA3kOUt.o: In function `main':
luainterpret.c:(.text+0x1b): undefined reference to `lua_open'
luainterpret.c:(.text+0x31): undefined reference to `luaopen_base'
luainterpret.c:(.text+0x40): undefined reference to `luaopen_table'
luainterpret.c:(.text+0x4f): undefined reference to `luaopen_io'
luainterpret.c:(.text+0x5e): undefined reference to `luaopen_string'
luainterpret.c:(.text+0x6d): undefined reference to `luaopen_math'
luainterpret.c:(.text+0xa1): undefined reference to `luaL_loadbuffer'
luainterpret.c:(.text+0xc3): undefined reference to `lua_pcall'
luainterpret.c:(.text+0xf6): undefined reference to `lua_tostring'
luainterpret.c:(.text+0x11f): undefined reference to `lua_settop'
luainterpret.c:(.text+0x152): undefined reference to `lua_close'
collect2: error: ld returned 1 exit status
I checked the /usr/lib/liblua50.a file with nm and the Functions above are indeed there! Why is gcc then not able to find said Functions?
Can someone tell me what im doing wrong?
Instead of putting the library before the source file (which makes use of the functions present in the library), try putting it afterwards, like
gcc -I/usr/include/lua50 -L/usr/lib/liblua50.a luainterpret.c -llua50
From the online gcc manual
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
I have been working on a C code for which I needed some functions of libxls.h header files. I have already downloaded the corresponding files and followed the installation instruction. The files also contained a test C code (test.c) which I tried compiling. The results were as follow:
Command to compile the test.c file-
gcc test.c -lm
Results:
[root#XXXXXXX95549 test]# gcc test.c -lm
test.c:28:24: error: libxls/xls.h: No such file or directory
test.c: In function ‘main’:
test.c:33: error: ‘xlsWorkBook’ undeclared (first use in this function)
test.c:33: error: (Each undeclared identifier is reported only once
test.c:33: error: for each function it appears in.)
test.c:33: error: ‘pWB’ undeclared (first use in this function)
test.c:34: error: ‘xlsWorkSheet’ undeclared (first use in this function)
test.c:34: error: ‘pWS’ undeclared (first use in this function)
test.c:39: error: ‘WORD’ undeclared (first use in this function)
test.c:39: error: expected ‘;’ before ‘t’
test.c:53: error: ‘t’ undeclared (first use in this function)
test.c:58: error: ‘tt’ undeclared (first use in this function)
test.c:60: error: dereferencing pointer to incomplete type
test.c:63: error: dereferencing pointer to incomplete type
test.c:64: error: dereferencing pointer to incomplete type
test.c:66: error: dereferencing pointer to incomplete type
test.c:67: error: dereferencing pointer to incomplete type
test.c:68: error: dereferencing pointer to incomplete type
test.c:70: error: dereferencing pointer to incomplete type
test.c:70: error: dereferencing pointer to incomplete type
test.c:71: error: dereferencing pointer to incomplete type
To solve this problem, I copied the library files at the '/user/include' location
After which the results for same compilation commands were:
[root#XXXXXX95549 test]# gcc test.c -lm
/tmp/cc8DJETV.o: In function `main':
test.c:(.text+0x14): undefined reference to `xls_open'
test.c:(.text+0xd4): undefined reference to `xls_getWorkSheet'
test.c:(.text+0xe4): undefined reference to `xls_parseWorkSheet'
test.c:(.text+0xf0): undefined reference to `xls_getCSS'
test.c:(.text+0x49c): undefined reference to `xls_showBookInfo'
collect2: ld returned 1 exit status
Please, explain how can I rectify this problem.
The code test.c is given below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <libxls/xls.h>
int main()
{
xlsWorkBook* pWB;
xlsWorkSheet* pWS;
FILE *f;
int i;
struct st_row_data* row;
WORD t,tt;
pWB=xls_open("files/test2.xls", "ASCII"); // "KOI8-R"
if (pWB!=NULL)
{
f=fopen ("test.htm", "w");
for (i=0;i<pWB->sheets.count;i++)
printf("Sheet N%i (%s) pos %i\n",i,pWB->sheets.sheet[i].name,pWB->sheets.sheet[i].filepos);
pWS=xls_getWorkSheet(pWB,0);
xls_parseWorkSheet(pWS);
fprintf(f,"<style type=\"text/css\">\n%s</style>\n",xls_getCSS(pWB));
fprintf(f,"<table border=0 cellspacing=0 cellpadding=2>");
for (t=0;t<=pWS->rows.lastrow;t++)
{
row=&pWS->rows.row[t];
// xls_showROW(row->row);
fprintf(f,"<tr>");
for (tt=0;tt<=pWS->rows.lastcol;tt++)
{
if (!row->cells.cell[tt].ishiden)
{
fprintf(f,"<td");
if (row->cells.cell[tt].colspan)
fprintf(f," colspan=%i",row->cells.cell[tt].colspan);
// if (t==0) fprintf(f," width=%i",row->cells.cell[tt].width/35);
if (row->cells.cell[tt].rowspan)
fprintf(f," rowspan=%i",row->cells.cell[tt].rowspan);
fprintf(f," class=xf%i",row->cells.cell[tt].xf);
fprintf(f,">");
if (row->cells.cell[tt].str!=NULL && row->cells.cell[tt].str[0]!='\0')
fprintf(f,"%s",row->cells.cell[tt].str);
else
fprintf(f,"%s"," ");
fprintf(f,"</td>");
}
}
fprintf(f,"</tr>\n");
}
fprintf(f,"</table>");
printf("Count of rows: %i\n",pWS->rows.lastrow);
printf("Max col: %i\n",pWS->rows.lastcol);
printf("Count of sheets: %i\n",pWB->sheets.count);
fclose(f);
xls_showBookInfo(pWB);
}
return 0;
}
Thanks in advance,
Got the answer to this question on the following location:
http://www.cprogramdevelop.com/2018568/
Those who might be facing the problem with libxls library can refer the above mentioned website.
cheers
I am getting an undefined reference error when trying to link my application with code blocks and command line gcc. The file giving the error has the function prototype in the header. The source file the function the linker is complaining about has other functions in it that aren't giving this error. I copied and pasted the function declarator to the header and calling function (fixing the parameters of course) to ensure it is not a typo. What am I missing? Other functions called from linked_list.c don't give me linker errors.
Here are the code snippets:
mw_proxy.h:
struct addr_l_node
{
uint32_t address;
struct addr_l_node *next;
};
...
typedef struct addr_l_node addr_l_node;
...
void delete_addr_list(addr_l_node *first);
parse_config.c:
#include "mw_proxy.h"
...
addr_l_node *first;
...
delete_addr_list(first);
linked_list.c:
void delete_addr_list(addr_l_node *first)
{
...
}
Finally I am getting this error from code blocks:
obj/Debug/parse_config.o||In function `parse_config':|
/media/sf_H_DRIVE/src/codeblocks/mw_proxy/parse_config.c|240|undefined reference to `delete_addr_list'|
||=== Build finished: 1 errors, 0 warnings ===|
From a bash shell:
gcc int_array.o cleanup.o check_datagram.o mw_proxy.o parse_config.o linked_list.o
parse_config.o: In function `parse_config':
/media/sf_H_DRIVE/src/codeblocks/mw_proxy/parse_config.c:240: undefined reference to `delete_addr_list'
collect2: error: ld returned 1 exit status
I'm trying to run a example from the "Using Graphviz as a library" in http://www.graphviz.org/Documentation.php.
#include <gvc.h>
int main(int argc, char **argv)
{
Agraph_t *g;
Agnode_t *n, *m;
Agedge_t *e;
Agsym_t *a;
GVC_t *gvc;
/* set up a graphviz context */
gvc = gvContext();
/* parse command line args - minimally argv[0] sets layout engine */
gvParseArgs(gvc, argc, argv);
/* Create a simple digraph */
g = agopen("g", Agdirected);
n = agnode(g, "n", 1);
m = agnode(g, "m", 1);
e = agedge(g, n, m, 0, 1);
/* Set an attribute - in this case one that affects the visible rendering */
agsafeset(n, "color", "red", "");
/* Compute a layout using layout engine from command line args */
gvLayoutJobs(gvc, g);
/* Write the graph according to -T and -o options */
gvRenderJobs(gvc, g);
/* Free layout data */
gvFreeLayout(gvc, g);
/* Free graph structures */
agclose(g);
/* close output file, free context, and return number of errors */
return (gvFreeContext(gvc));
}
I'm compiling and linking with : gcc -Wall pkg-config libgvc --cflags --libs *.c -o EXE -lgvc
and then I see this result:
graph.c: In function ‘main’:
graph.c:14:18: error: ‘Agdirected’ undeclared (first use in this function)
graph.c:14:18: note: each undeclared identifier is reported only once for each function it appears in
graph.c:15:2: error: too many arguments to function ‘agnode’
In file included from /usr/include/graphviz/types.h:717:0,
from /usr/include/graphviz/gvc.h:20,
from graph.c:1:
/usr/include/graphviz/graph.h:185:22: note: declared here
graph.c:16:2: error: too many arguments to function ‘agnode’
In file included from /usr/include/graphviz/types.h:717:0,
from /usr/include/graphviz/gvc.h:20,
from graph.c:1:
/usr/include/graphviz/graph.h:185:22: note: declared here
graph.c:17:2: error: too many arguments to function ‘agedge’
In file included from /usr/include/graphviz/types.h:717:0,
from /usr/include/graphviz/gvc.h:20,
from graph.c:1:
/usr/include/graphviz/graph.h:192:22: note: declared here
graph.c:7:11: warning: unused variable ‘a’ [-Wunused-variable]
graph.c:6:12: warning: variable ‘e’ set but not used [-Wunused-but-set-variable]
Could anyone help me understand what is going on? Why the compiler is complaining about those arguments in those functions?
Thank you!!!!
I saved your code as g.c, then issued this command line
gcc -Wall `pkg-config libgvc --cflags --libs` g.c -o EXE -lgvc
that yields
g.c: In function ‘main’:
g.c:14:5: error: too few arguments to function ‘agopen’
/usr/local/include/graphviz/cgraph.h:266:18: note: declared here
g.c:7:14: warning: unused variable ‘a’ [-Wunused-variable]
g.c:6:15: warning: variable ‘e’ set but not used [-Wunused-but-set-variable]
then I added the miss parameter
g = agopen("g", Agdirected, 0);
and the miss library
gcc -Wall `pkg-config libgvc --cflags --libs` g.c -lgvc -lcgraph
now the code compile and link with just 2 warnings:
g.c: In function ‘main’:
g.c:7:14: warning: unused variable ‘a’ [-Wunused-variable]
g.c:6:15: warning: variable ‘e’ set but not used [-Wunused-but-set-variable]
I think it works because I've built graphviz from source, then pkg-config is up-to-date...
The program still need some debug, running it i get:
./a.out
There is no layout engine support for "a.out"
Use one of: circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
Error: Layout was not done. Missing layout plugins?
The message is because by default the layout engine use the exe name (i.e. a.out, default of gcc compile-and-link) as layout string...