Detailed explanation of mutual call between C language and Lua

Posted by lcoscare on Sat, 26 Feb 2022 04:01:53 +0100

Write a C to call Lua's Demo to compile and run

add.c content

//How many files do you need to include
#include  <stdio.h>
#include  "lua.h"
#include  "lualib.h"
#include  "lauxlib.h"
lua_State* L;
int
luaadd(int x, int y)
{
 int sum;
 /*Function name*/
 lua_getglobal(L,"add");
 /*Parameter stack*/
 lua_pushnumber(L, x);
 /*Parameter stack*/
 lua_pushnumber(L, y);
 /*Start calling the function, with 2 parameters and 1 return value*/
 lua_call(L, 2, 1);
 /*Fetch return value*/
 sum = (int)lua_tonumber(L, -1);
 /*Clear stack of return values*/
 lua_pop(L,1);
 return sum;
}
int
main(int argc, char *argv[])
{
 int sum;
 L = luaL_newstate(); /* Create lua state machine */
 luaL_openlibs(L); /* Open all Lua Standard Libraries in Lua state machine */
 /*Load lua script*/
 luaL_dofile(L, "add.lua");
 /*Call C function, which will call lua function*/
 sum = luaadd(99, 10);
 printf("The sum is %d \n",sum);
 /*Clear Lua*/
 lua_close(L);
 return 0;
}

add.lua is placed in the directory at the same level as C, and a simple function is written in it for C to call

function add(x,y)
  print("this is a lua script")
  return x + y
end

Well, it's finally time to compile with GCC. Go directly to GCC add C see if it's OK.

Sure enough, I made a mistake!

This is because you didn't add The function in C is linked to the lua library compiled earlier. How can he specify which library to link to? You can see from the GCC document that the - l parameter can specify the library to be linked

-L parameter and - L parameter
-The l parameter is used to specify the library to which the program will link, and the - l parameter is followed by the library name. What is the relationship between the library name and the real library file name?
Take the math library for example. Its name is m and its file name is libm so, it's easy to see that the header lib and footer of the library file name so is the library name

Summary of C language calling Lua compilation problems

Correct compile command

Original link: https://blog.csdn.net/uisoul/article/details/60137134

gcc -o test test.c -llua -lm -ldl​

Let's try again. This time, we compiled it: test

Execution succeeded!

Problem 1: missing - lm parameter

/usr/local/lib/liblua.a(lobject.o): In function `numarith': 
lobject.c:(.text+0xc8d): undefined reference to `fmod'  
lobject.c:(.text+0xcb8): undefined reference to `pow'   
/usr/local/lib/liblua.a(lvm.o): In function `luaV_execute': 
lvm.c:(.text+0x2068): undefined reference to `pow'  
lvm.c:(.text+0x211c): undefined reference to `fmod' 
/usr/local/lib/liblua.a(lmathlib.o): In function `math_log10':  
lmathlib.c:(.text+0x21e): undefined reference to `log10'    
/usr/local/lib/liblua.a(lmathlib.o): In function `math_pow':    
lmathlib.c:(.text+0x338): undefined reference to `pow'  
........​

Problem 2: missing - ldl parameter

/usr/local/lib/liblua.a(loadlib.o): In function `lookforfunc':  
loadlib.c:(.text+0x748): undefined reference to `dlsym'loadlib.c:(.text+0x7b3): 
undefined reference to `dlopen'loadlib.c:(.text+0x851): undefined reference to 
`dlerror'loadlib.c:(.text+0x871): undefined reference to 
`dlerror'/usr/local/lib/liblua.a(loadlib.o): In function `gctm':loadlib.c:
(.text+0xa44): undefined reference to `dlclose'​

Problem analysis

1. Why is there an undefined reference to 'xxxxx' error? ​

First of all, this is a link error, not a compilation error. That is to say, if there is only this error, it means that there is no problem with your program source code itself. It is that you use the wrong parameters when compiling with the compiler, and you do not specify the library to be used by the linked program. For example, if some mathematical functions are used in your program, you should specify that the program should link the Mathematical Library in the compilation parameters, The method is to add - lm to the compilation command line.

2. - L parameter and - L parameter

-The L parameter is used to specify the library to which the program will link, and the - L parameter is followed by the library name. What is the relationship between the library name and the real library file name? Take the math library for example. Its name is m and its file name is libm So, it's easy to see that the header lib and footer of the library file name So is the library name. Well, now we know how to get the library name. For example, we need to use a library name provided by a third party, libtest So, then we just put libtest Copy so to / usr/lib, add the - ltest parameter during compilation, and we can use libtest So Library (of course, we need to use the functions in libtest.so library, and we also need the header file supporting libtest.so). The libraries placed in / lib and / usr/lib and / usr/local/lib can be linked directly with the - L parameter. However, if the library files are not placed in these three directories, but in other directories, if we only use the - L parameter, the link will still make an error. The error message is probably: "/ usr/bin/ld: cannot find -lxxx", that is, the linker ld cannot find libxxx in those three directories So, another parameter - L comes in handy. For example, the commonly used X11 library is placed in the / usr/X11R6/lib directory. When compiling, we need to use the - L/usr/X11R6/lib -lX11 parameter, and the - L parameter is followed by the directory name where the library file is located. Another example is that we put libtest So is placed in the / aaa/bbb/ccc directory, and the link parameter is - L/aaa/bbb/ccc -ltest. In addition, most libxxxx So is just a link. Take RH9 as an example, such as libm So it links to / lib / libm so. x,/lib/libm.so.6 link to / lib / libm-2.3.2 So, if there is no such link, there will still be an error, because ld will only find libxxxx So, if you want to use the xxxx library, only libxxxx so. X or libxxxx-x.x.x.so, just make a link ln - s libxxxx-x.x.x.so libxxxx so​

References:
http://blog.csdn.net/uisoul/article/details/60135764
http://blog.csdn.net/uisoul/article/details/60135383
http://blog.csdn.net/uisoul/article/details/60135764

Topics: C lua