1

I'm trying to build a simple echo service with seasocks in c++. I've compiled and installed seasocks into /usr/local/* and it appears the libraries are included just fine, however I'm getting errors starting the server.

This is the example that I'm using:

    #include "seasocks/PrintfLogger.h"
    #include "seasocks/Server.h"
    #include "seasocks/StringUtil.h"
    #include "seasocks/WebSocket.h"

    #include <cstring>
    #include <iostream>
    #include <memory>
    #include <set>
    #include <sstream>
    #include <string>

    /* Simple server that echo any text or binary WebSocket messages back. */

    using namespace seasocks;

    class EchoHandler: public WebSocket::Handler {
    public:
        virtual void onConnect(WebSocket* /*connection*/) {
        }

        virtual void onData(WebSocket* connection, const uint8_t* data, size_t length) {
        connection->send(data, length); // binary
        }

        virtual void onData(WebSocket* connection, const char* data) {
        connection->send(data); // text
        }

        virtual void onDisconnect(WebSocket* /*connection*/) {
        }
    };

    int main(int /*argc*/, const char* /*argv*/[]) {
        std::shared_ptr<Logger> logger(new PrintfLogger(Logger::Level::DEBUG));

        Server server(logger);
        std::shared_ptr<EchoHandler> handler(new EchoHandler());
        server.addWebSocketHandler("/", handler);
        server.serve("/dev/null", 8000);
        return 0;
    }

These are my build errors:

    04:40:40 **** Build of configuration Debug for project HSServer ****
    make all 
    Building file: ../src/HSServer.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/HSServer.d" -MT"src/HSServer.o" -o "src/HSServer.o" "../src/HSServer.cpp"
    Finished building: ../src/HSServer.cpp

    Building target: HSServer
    Invoking: GCC C++ Linker
    g++  -o "HSServer"  ./src/HSServer.o   
    ./src/HSServer.o: In function `main':
    /home/xnite/workspace/HSServer/Debug/../src/HSServer.cpp:37: undefined reference to `seasocks::Server::Server(std::shared_ptr<seasocks::Logger>)'
    /home/xnite/workspace/HSServer/Debug/../src/HSServer.cpp:39: undefined reference to `seasocks::Server::addWebSocketHandler(char const*, std::shared_ptr<seasocks::WebSocket::Handler>, bool)'
    /home/xnite/workspace/HSServer/Debug/../src/HSServer.cpp:40: undefined reference to `seasocks::Server::serve(char const*, int)'
    /home/xnite/workspace/HSServer/Debug/../src/HSServer.cpp:37: undefined reference to `seasocks::Server::~Server()'
    /home/xnite/workspace/HSServer/Debug/../src/HSServer.cpp:37: undefined reference to `seasocks::Server::~Server()'
    collect2: error: ld returned 1 exit status
    make: *** [makefile:47: HSServer] Error 1

    04:40:45 Build Finished (took 5s.239ms)
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep Apr 30 '17 at 10:33

1 Answers1

1

You would have to link the library. For ex -

g++ -o myfile objfileA.o objfileB.o -lLibName

The libName here is the library's name. For ex in Linux - for a library libfoo.so you would only write -lfoo. But in Windows it would be foo.lib. You might have to add the directory using -L‹path_to_dir› without any space after -l or -L for the files to be located.

If using an IDE, specify it under Additional Library Directories depending on the IDE.

Rajeev Mehta
  • 649
  • 9
  • 18