using namespace std;
#include <cstdio>
#include <string>
#include "angelscript.h"
#include "./scriptbuilder.h"
CScriptBuilder builder;
asIScriptEngine *engine;
string modules[1000];
int moduleslength = 0;
void MessageCallback(const asSMessageInfo *msg, void *param)
{
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
//utils----------
bool check(int in);
//---------------
bool as_register();
bool as_compile(string path,string modulename);
bool as_init()
{
engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
int r = engine->SetMessageCallback(asFUNCTION(MessageCallback),0,asCALL_CDECL);
if (r>=0) return false;
// RegisterStdString(engine);
if (as_register())
{
return false;
}
return true;
}
bool as_compile (string path,string modulename)
{
for(int i = 0;i<moduleslength;i++)
{
if(modules[i]==modulename)
{
printf("FFFFFUUUUUU. Module name already exist");
return false;
}
}
// The CScriptBuilder helper is an add-on that loads the file,
// performs a pre-processing pass if necessary, and then tells
// the engine to build a script module.
int r = builder.StartNewModule(engine, modulename.c_str());
if( r < 0 )
{
// If the code fails here it is usually because there
// is no more memory to allocate the module
printf("Unrecoverable error while starting a new module.\n");
return false;
}
r = builder.AddSectionFromFile(path.c_str());
if( r < 0 )
{
// The builder wasn't able to load the file. Maybe the file
// has been removed, or the wrong name was given, or some
// preprocessing commands are incorrectly written.
printf("Please correct the errors in the script and try again.\n");
return false;
}
r = builder.BuildModule();
if( r < 0 )
{
// An error occurred. Instruct the script writer to fix the
// compilation errors that were listed in the output stream.
printf("Please correct the errors in the script and try again.\n");
return false;
}
modules[moduleslength]=modulename;
moduleslength++;
return true;
}
//Here goes function declaration prototypes stuff
void print(const string &in);
//-----------------------------------------------
bool as_register()
{
int r = engine->RegisterGlobalFunction("void print(const string &in)",asFUNCTION(print), asCALL_CDECL);
if (check(r)) return false;
return true;
}
bool check(int in)
{
return in >= 0;
}
bool runmodule(string modulename)
{
// Find the function that is to be called.
asIScriptModule *mod = engine->GetModule(modulename.c_str());
asIScriptFunction *func = mod->GetFunctionByDecl("void main()");
if( func == 0 )
{
// The function couldn't be found. Instruct the script writer
// to include the expected function in the script.
printf("The script must have the function 'void main()'. Please add it and try again.\n");
return false;
}
// Create our context, prepare it, and then execute
asIScriptContext *ctx = engine->CreateContext();
ctx->Prepare(func);
int r = ctx->Execute();
if( r != asEXECUTION_FINISHED )
{
// The execution didn't complete as expected. Determine what happened.
if( r == asEXECUTION_EXCEPTION )
{
// An exception occurred, let the script writer know what happened so it can be corrected.
printf("An exception '%s' occurred. Please correct the code and try again.\n", ctx->GetExceptionString());
}
}
return true;
}
int main()
{
printf("OK");
}
That's code I using.
But when I compile it using :
g++ test.cpp scriptbuilder.cpp scriptbuilder.h -L. -langelscript -lpthread -o test
It returns :
/tmp/ccTRpvvj.o: In function `as_register()':
test.cpp:(.text+0x28d): undefined reference to `print(std::string const&)'
collect2: error: ld returned 1 exit status
Can anybody say what I do wrong?
Used code from 'Your first script' and changed it a bit.
Also, theres scriptbuilder.cpp and scriptbuilder.h and angelscript.a in catalog I execute command from