lua-users home
lua-l archive

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


I was trying to load my own custom binary, and I used some sample code from the PIL 2nd edition, and edited a bit because I using Lua 5.2. 

I am using Ubuntu 12.04 64-bit, and I managed to compile the binary just fine, but when I call require to load it, I get the error: 

Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
> require'test'
error loading module 'test' from file './test.so':
./test.so: cannot dynamically load executable
stack traceback:
[C]: in ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: in ?

I have no idea as to why this would happen, I've spent hours looking around google to no avail.

I have attached the code I am using, and I compile with GCC using g++ dir.cpp -o test.so -llua

Any help is greatly appreciated.
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>

extern "C" {
	#include "lua.h"
	#include "lauxlib.h"
	#include "lualib.h"
}

static int l_dir ( lua_State *L ) {
	DIR *dir;
	dirent *entry;
	int i;
	const char *path = luaL_checkstring( L, 1 ); 

	dir = opendir( path ); 
	if( dir == NULL ) {
		lua_pushnil( L ); 
		lua_pushstring( L, strerror(errno) ); 
		return 2;
	}

	lua_newtable( L );
	i = 1;
	while( (entry = readdir(dir)) != NULL ) {
		lua_pushnumber( L, i++ );
		lua_pushstring( L, entry->d_name ); 
		lua_settable( L, -3 ); 
	}

	closedir( dir ); 
	return 1; 
}

static const struct luaL_Reg mylib[] = {
	{ "dir", l_dir },
	{ NULL, NULL }
}; 

int luaopen_mylib( lua_State *L ) {
	luaL_newlib( L, mylib ); // Lua 5.2 way to load libraries?
 	return 1; 
}

int main() {
	return 0;
}