简体   繁体   中英

calling c code from lua

I tried to follow this: http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm

I tried to compile the c code like it says in the guide:

gcc -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 -llua5.1 hellofunc.c

but I got http://pastebin.com/KQvA0qFH which I recognized as the error you get when you forget to include a lib or framework but I did -I/usr/include/lua5.1

If it matters I am running os x and I installed lua with brew

You haven't told it where to find lua5.1

use -L to tell the compiler where the library is located

From your own example page it says to look at http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm#_Anatomy_of_a_Lua_Call

I can't comment yet so I have to post it as Answer BUT

from the error I'd guess you need to compile with the -m32 switch in order to build for 32bit library

This works for me but I don't use homebrew. Lua is installed in /usr/local .

% gcc -Wall -bundle -undefined dynamic_lookup -fPIC -o power.so hellofunc.c 
% lua
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> require"power"
> print(square(2))
Top of square(), nbr=2.000000
4
> print(cube(2))
Top of cube(), number=2.000000
8

Note in the compilation line the use of -bundle -undefined dynamic_lookup instead of -shared and the absence of -llua5.1 .

In general, you should not link the Lua core library into dynamic libraries such as this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM