dimanche 2 décembre 2018

AngelScript on C++ won't compile correctly (undefined references)

I've been working on a C++11 OpenGL game and I'd like to use AngelScript as it's scripting language but I've not been able to compile it. When I try to compile AngelScript alone it gives undefined reference errors. This isn't a game-related question. I just pointed out my intended use of the library.

I use Linux Mint 18.2 Cinnamon x64
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)

My compile command: g++ -o t test.cpp -std=c++11
Output:

/tmp/cc8kTBxp.o: In function `main':
test.cpp:(.text+0x3ce): undefined reference to `asCreateScriptEngine'
test.cpp:(.text+0x47c): undefined reference to `RegisterStdString(asIScriptEngine*)'
test.cpp:(.text+0x4c4): undefined reference to `CScriptBuilder::CScriptBuilder()'
test.cpp:(.text+0x4e2): undefined reference to `CScriptBuilder::StartNewModule(asIScriptEngine*, char const*)'
test.cpp:(.text+0x58e): undefined reference to `CScriptBuilder::AddSectionFromMemory(char const*, char const*, unsigned int, int)'
test.cpp:(.text+0x618): undefined reference to `CScriptBuilder::BuildModule()'
collect2: error: ld returned 1 exit status

The code:

#include <iostream>
#include <string>
#include "logger.hpp" /* My custom logger (Made for Linux) */
#include "angelscript/include/angelscript.h"
#include "angelscript/add_on/scriptstdstring/scriptstdstring.h"
#include "angelscript/add_on/scriptbuilder/scriptbuilder.h"

typedef std::string string;
typedef unsigned int uint;
typedef unsigned char byte;

// = Loggers ==========================================================================================
Logger mainLogger  (" main ");
Logger scriptLogger("script");

// = AngelScript callbacks and global functions =======================================================
void as_message_callback(const asSMessageInfo* msg, void* param) {
    string output = "Error at [" + std::to_string(msg->row) + "," + std::to_string(msg->col) + "]: " + string(msg->message);
    if      ( msg->type == asMSGTYPE_WARNING )     scriptLogger.warn(output);
    else if ( msg->type == asMSGTYPE_INFORMATION ) scriptLogger.info(output);
}
void print(const char* str) {
    std::cout << str << std::endl;
}
// ====================================================================================================

int main() {
    mainLogger.info("Testing AngelScript...");

    asIScriptEngine* scriptEngine = asCreateScriptEngine();
    int engineResponse = scriptEngine->SetMessageCallback(asFUNCTION(as_message_callback), 0, asCALL_CDECL);
    engineResponse     = scriptEngine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(as_message_callback), asCALL_CDECL);
    RegisterStdString(scriptEngine);

    string script = R"(
        int main() {
            print("AngelScript!");
        }
    )";

    CScriptBuilder builder;
    int builderResponse = builder.StartNewModule(scriptEngine, "core");
    if (builderResponse < 0) {scriptLogger.error("Failed to start core scripting module"); return -1;}

    builderResponse = builder.AddSectionFromMemory("Test", script.c_str());
    if (builderResponse < 0) {scriptLogger.error("Bad script"); return -1;}

    builderResponse = builder.BuildModule();
    if (builderResponse < 0) {scriptLogger.error("Failed to compile script"); return -1;}

    mainLogger.info("Successfully tested AngelScript!");
}

This is my first question so if I'm missing info sorry about that...

Aucun commentaire:

Enregistrer un commentaire