lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2021/10/24 5:18, Kaz wrote:
> 
> I made c_cpp_properties.json like you said, but used '"compilerPath": "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe"' because I want to test embedding Lua in C. Since I’m using Lua 5.1, I changed -llua54 to -llua, and -liuplua54 to -liuplua51. Did I need the dynamic link section if I’m not using that? I left that out.
> 

that should be fine, the dynamic configuration and static configuration are independent of each other.


> What should I put into the files to test? 

try this piece of code which demonstrates the usage of Lua and iuplua from C. this should be copy-paste ready.


```C
#include <stdio.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include <iup.h>
#include <iuplua.h>


typedef struct {
	char const *name;
	lua_CFunction loaderfn;
} Preloader;

/* list the Lua modules you want to preload here */
static const Preloader preloaders[] = {
	{ "iuplua", &iuplua_open },
	{ NULL, NULL },
};

int main(int argc, char const *argv[])
{
	/* do something in C */
	printf("hello C!\n");

	/* init Lua runtime */
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);

	/* try run some Lua code */
	luaL_dostring(L, "print 'hello Lua!'");

	/* install preloaders into package.preload */
	lua_getglobal(L, "package");
	lua_getfield(L, -1, "preload");
	Preloader const *mod = &preloaders[0];
	while (mod->name != NULL) {
		lua_pushcfunction(L, mod->loaderfn);
		lua_setfield(L, -2, mod->name);
		++mod;
	}
	lua_pop(L, 2);

	/* try run some more Lua code with iup */
	luaL_dostring(L, "local iup = require 'iuplua'; iup.Message('hello', 'hello iuplua!');");

	lua_close(L);
	return 0;
}
```

> to appease VSCode. The build/debug said it couldn’t find ‘-llua’

this command line must match the file name you extracted from the Lua zip package, '-llua' asks the linker
to look for `liblua.a`; if the file name was `liblua5.1.a`, you need to use `-llua5.1` instead.

as of the order, the dependencies should listed after the dependents. e.g. iuplua must listed before both
lua and iup. if you are not sure, a common trick is to simply repeat the libraries several times in the same
order (typically twice would be enough). btw, there are ways to get away from this quirky linker behavior,
but I'm afraid you have to investigate more into it yourself.