I've put together a very simple Lua engine but it seems to reject bytecode which works in the lua console. The uncompiled version works in the engine. Am I using luac wrong somehow?
I compile using the given command and run as './a.out'.
res/default.lua:
print("Setting up world structure.")
luac command:
luac -o res/default.lux res/default.lua
MWE:
#define SCRIPTDIR "res/"
#define THROW_IF_NONZERO(x,m) if((x)!=0) throw std::runtime_error(m);
#define THROW_IF_ZERO(x,m) if((x)==0) throw std::runtime_error(m);
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include "sys/stat.h"
#include <string>
#include <system_error>
#include <iostream>
using std::string;
class Entity {
private:
lua_State *m_lua;
public:
Entity() : Entity(nullptr) { }
Entity(lua_State *lua) : m_lua{lua} { }
virtual ~Entity() { }
void load_and_run(string);
};
class WorldEntity : public Entity {
public:
WorldEntity(lua_State *lua) : Entity(lua) {
luaL_openlibs(lua);
}
~WorldEntity() { }
};
int main() {
lua_State *lua{nullptr};
try {
lua = luaL_newstate();
WorldEntity eWorld{lua};
eWorld.load_and_run("default"); // load default.lua/lux
} catch(std::exception &e) {
if (lua != nullptr) {
lua_close(lua);
}
std::cout << "Error: " << e.what() << std::endl;
}
return 0;
}
void Entity::load_and_run(string filename) {
THROW_IF_ZERO(m_lua, "Lua not started.");
filename = SCRIPTDIR + filename + ".lux";
struct stat sb;
int rc = stat(filename.c_str(), &sb);
if (rc == -1) {
filename.pop_back();
filename += "a";
rc = stat(filename.c_str(), &sb);
THROW_IF_NONZERO(rc, "File not found!");
}
std::cout << "File: " << filename << std::endl;
// Currently won't run compiled Lua scripts, not sure why.
rc = luaL_dofile(m_lua, filename.c_str());
THROW_IF_NONZERO(rc, "Could not load lua file.");
}
compile command:
gcc src/bug001mwe.cpp -std=c++14 -llua -lstdc++
correct output from script:
File: res/default.lua
Setting up world structure.
wrong output from bytecode:
File: res/default.lux
Error: Could not load lua file.
both files, output from lua console:
Setting up world structure.
What confused me was that it worked in the lua console but not in my program. I added a call to lua_tostring after the call to luaL_dofile, like this:
rc = luaL_dofile(m_lua, filename.c_str());
std::ostringstream ostr;
ostr << "Could not load lua file. ";
ostr << lua_tostring(m_lua, -1);
THROW_IF_NONZERO(rc, ostr.str());
The error string became:
Error: Could not load lua file. res/default.lux: version mismatch in precompiled chunk
What the heck?
Long story short, I had a previous version of Lua installed due to out of date package dependencies in some unrelated stuff. The older luac was intercepting the luac command and compiling to valid but incompatible bytecode. Uninstalled the unrelated packages which I didn't really need, and now everything works.
Moral of the story: always check for an error string on the Lua stack, it will (probably) tell you what's wrong.
Related
So I was trying to make a program that dynamically creates a dll file and loads it into the program and calls it.
Here is my code for
createFile:
void createFile(std::string data, std::string name) {
std::ofstream file;
name += ".cpp";
file.open(name,std::ofstream::out);
if (file.is_open()) {
file << data;
std::cout << "File saved successfully" << std::endl;
file.close();
}
else {
std::cout << "Error opening file" << std::endl;
exit(1);
}
}
compile:
void compile(std::string name) {
std::string temp;
temp = "c++.exe " + name + ".cpp -o dll/" + name + ".dll -shared -fPIC";
system(temp.c_str());
}
loadFunc:
typedef void (*function)(const char*);
void loadFunc(LPCSTR name, LPCSTR func) {
HMODULE temp;
function __cdecl myproc;
temp = LoadLibraryA(name);
if (temp != NULL) {
std::cout << "\nDll is loaded";
myproc = (function) GetProcAddress(temp, func);
if (myproc !=NULL)
{
std::cout << "\nFunction is loaded\n";
(myproc)("Somthing here");
}
// Free the DLL module.
FreeLibrary(temp);
}
}
The problem is when I compile a dll using visual studio 2019 it works just fine, but if I compile a dll file using gcc or c++ or g++ it doesn't load the dll. So I tried using dependency walker to see the difference between two dll files(one made with gcc and another with visual studio). I saw the file structures were different. dll made with gcc had larger size than the one made with visual studio. Can anyone help me with this?
Edit:
So after doing some trial and error, I have found that only dll files that have C libraries work. I don't know why. I tried using iostream earlier, but it always failed to load and on the other hand when I used stdio.h it worked fine.
dllfile.cpp(with iostream):
#ifdef __cpluplus
extern "C" {
#endif
#include <iostream>
__declspec(dllexport) void print(const char *x){
std::cout<<x<<std::endl;
}
#ifdef __cplusplus
}
#endif
dllfile.cpp(with stdio):
#ifdef __cpluplus
extern "C" {
#endif
#include <stdio.h>
__declspec(dllexport) void print(const char *x){
printf("%s",x);
}
#ifdef __cplusplus
}
#endif
I am trying to run a cpp program on raspberry pi 3 b+ (from 'pi' user) but when I try to open a file with 'fstream' library it doesn't work.
I am using the following code (from main):
std::ios::sync_with_stdio(false);
std::string path = "/NbData";
std::ofstream nbData(path);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
The program always fails there and stops because no file is created (I don't get a fatal error but the test fails and it outputs 'Error during process' which means no file was created).
I am compiling with the following command (there are no issues when I compile):
g++ -std=c++0x nbFinder.cpp -o nbFinder
I have already tried my program on Xcode and everything worked perfectly...
The problem is your path. You must put the file, you are using just the path and if the path do not exist will throw an error. In your case you just using std::string path = "/NbData";, that is you path not your file.
To be able to open your file you need make sure your path exist. Try use the code bellow, he will check if the path exist case not will create and then try to open your file.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
std::ios::sync_with_stdio(false);
std::string path = "./test_dir/";
std::string file = "test.txt";
// Will check if thie file exist, if not will creat
struct stat info;
if (stat(path.c_str(), &info) != 0) {
std::cout << "cannot access " << path << std::endl;
system(("mkdir " + path).c_str());
} else if(info.st_mode & S_IFDIR) {
std::cout << "is a directory" << path << std::endl;
} else {
std::cout << "is no directory" << path << std::endl;
system(("mkdir " + path).c_str());
}
std::ofstream nbData(path + file);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
return 0;
}
I define a function In a Lua Script and call it from my C++ program. The Lua Script use cjson module. I can executes the Lua script by Lua bin, but it can't run in my C++ program.
Error Message:
error loading module 'cjson' from file '/usr/local/app/cswuyg/test_lua/install/cjson.so':
/usr/local/app/cswuyg/test_lua/install/cjson.so: undefined symbol: lua_getfield
cpp code:
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
void test_dostring(lua_State* L, const std::string& file_path) {
std::ifstream ifs;
ifs.open(file_path.c_str());
if (!ifs.is_open()) {
return ;
}
std::stringstream buffer;
buffer << ifs.rdbuf();
std::string file_info(buffer.str());
// test luaL_dostring
std::cout << luaL_dostring(L, file_info.c_str()) << std::endl;
std::cout << "error msg:" << lua_tostring(L, -1) << std::endl;
lua_getglobal(L, "comment2");
lua_pushstring(L, "xxx");
lua_call(L, 1, 0);
std::string lua_ret = lua_tostring(L, -1);
std::cout << "ret:" << lua_ret << std::endl;
}
int main(int argc, char* argv[]) {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
test_dostring(L, "test.lua");
lua_close(L);
return 0;
}
Lua code:
local Json = require('cjson')
function comment2(test)
print(test)
end
comment2("xx")
How to fix it ? Any help would be appreciated.
If you're using Linux and the Lua core library is linked statically into your program, you need to expose the Lua C API by using -Wl,-E when you build your program. That's the incantation used to build the Lua command line interpreter from lua.org.
So I'm trying to load a .dylib file at runtime in c++ and calling a function within it. It does not seem to be any problem loading the file but when i try to create a function-pointer to the "print" function it's result is NULL.
Here is my code:
/* main.cpp */
#include <iostream>
#include <string>
#include <dlfcn.h>
#include "test.hpp"
int main(int argc, const char * argv[]) {
std::string path = argv[0];
std::size_t last = path.find_last_of("/");
// get path to execution folder
path = path.substr(0, last)+"/";
const char * filename = (path+"dylibs/libtest.dylib").c_str();
// open libtest.dylib
void* dylib = dlopen(filename, RTLD_LAZY);
if (dylib == NULL) {
std::cout << "unable to load " << filename << " Library!" << std::endl;
return 1;
}
// get print function from libtest.dylib
void (*print)(const char * str)= (void(*)(const char*))dlsym(dylib, "print");
if (print == NULL) {
std::cout << "unable to load " << filename << " print function!" << std::endl;
dlclose(dylib);
return 2;
}
// test the print function
print("Herro Word!");
dlclose(dylib);
return 0;
}
test dylib headerfile
/* test.hpp */
#ifndef test_hpp
#define test_hpp
void print(const char * str);
#endif
the dylib c++ file
#include <iostream>
#include "test.hpp"
void print(const char * str) {
std::cout << str << std::endl;
}
the output when running is:
unable to load /Users/usr/Library/Developer/Xcode/DerivedData/project/Build/Products/Debug/dylibs/libtest.dylib print function!
Program ended with exit code: 2
I am quite new to c++ and have never loaded dylibs before. Any help would be much appreciated!
Try qualifying the print function declaration with extern "C" to get around the name mangling that is likely going on.
Here's a nice article on the topic: http://www.tldp.org/HOWTO/C++-dlopen/theproblem.html (solution discussion on page following)
Hey this is more of a question, i want to know if it is possible to modify code through GUI asking because i was asked to see if i could create a GUI where the user can change certain attributes. i.e an exmaple is below
start %= -(status)
> lexeme[elementV]
> -(lexeme[elementF])
> +(inboundGroup);
Above is part of my code which is Boost SPIRIT which parses Strings so for example would it be possible to change the + to a * or - etc
+ = One
- = optional
* = multiple
Do you think it would be possible to change that through a GUI i think it could be just not sure on how to do it?
Any help i will be very grateful
Thanks Shamari
Everything is possible in programming ;-)
For dynamic modification of a program during execution, there are several solutions :
Use a dynamic language like LUA
Use a plugin system with dynamic loading
Since you require C++ and Boost Spirit, I think the best solution is to generate a plugin on the fly and load it afterwards.
Your program will generate code, compile it into a shared library (.so) and then load and execute it. (Some people will find that dirty. It's insecure also. But it's simple and it works.)
Here is an exemple for linux : plugin.h :
#ifndef PLUGIN_H__
#define PLUGIN_H__
#ifdef __cplusplus
extern "C" {
#endif
int process();
typedef int (*plugin_process_fn_ptr)();
#ifdef __cplusplus
}
#endif
#endif // PLUGIN_H__
Note that we must use extern C or else, C++ name mangling will make it difficult to import symbols.
plugin.cpp :
#include "plugin.h"
#include <iostream>
using namespace std;
int process()
{
int return_value = 0;
#include "plugin_content.inc.cpp"
return return_value;
}
Note that I use a hack here, the code will be included from another file, "plugin_content.inc.cpp". The code from user will be put inside.
a script to build the plugin, "build_plugin.sh" :
#! /bin/sh
g++ -c -Wall -fPIC plugin.cpp -o plugin.o
gcc -shared -o libplugin.so plugin.o
Now the calling program, main.cpp :
#include <iostream>
#include <fstream> // to open files
#include <dlfcn.h> // C lib to load dynamic libs
#include "plugin.h"
using namespace std;
// load the plugin and call the process() function fom it
static int process_via_plugin()
{
int return_value = -1;
void *lib_handle(NULL);
char *error(NULL);
char *plugin_lib = "./libplugin.so";
lib_handle = dlopen(plugin_lib, RTLD_LAZY);
if (!lib_handle)
{
cerr << "Error loading lib " << plugin_lib << " : " << dlerror() << endl;
exit(1);
}
char *plugin_fn = "process";
plugin_process_fn_ptr fn = (plugin_process_fn_ptr)dlsym(lib_handle, plugin_fn);
error = dlerror();
if (error)
{
cerr << "Error finding lib " << plugin_fn << " : " << error << endl;
exit(1);
}
// call the function loaded from lib
return_value = (*fn)();
dlclose(lib_handle);
lib_handle = NULL; // useless but for good habits ^^
return return_value;
}
// build or rebuild the plugin,
// we must call it when we change the plugin code code
static int build_plugin(string code)
{
{
char *plugin_code_file = "plugin_content.inc.cpp";
ofstream plugin_code(plugin_code_file, ios::out);
plugin_code << code << endl;
}
system("build_plugin.sh");
return 0;
}
// our program
int main(int argc, char *argv[])
{
cout << "Hello World !" << endl;
string code = ""
"cout << \"Hello from plugin !\" << endl;"
"";
// build a first version of the plugin and call it
build_plugin(code);
process_via_plugin();
// now we modify the code (use a GUI here)
code = ""
"cout << \"Hello from plugin, updated !\" << endl;"
"";
// rebuild the plugin and call it again
build_plugin(code);
process_via_plugin();
// do it again as much as you want.
return 0;
}
Now, build your program :
g++ -Wall -rdynamic -ldl main.cpp
and execute it :
a.out
and you get :
Hello World !
Hello from plugin !
Hello from plugin, updated !
The code I give you is very basic. For example, we should check if the compilation of the plugin is successful and report errors to the user. Now it's up to you to add more stuff.