使用Seasocks Echo服务器构建错误

Build error with seasocks echo server

本文关键字:构建 错误 服务器 Echo Seasocks 使用      更新时间:2023-10-16

我正在尝试在C 中建立一个简单的回声服务。我已经编译并安装了seasocks到/usr/local/*中,看来库的包含在内,但是我遇到了启动服务器的错误。

这是我正在使用的示例:

    #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;
    }

这些是我的构建错误:

    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)

您必须链接库。对于ex-

g -o myfile objfilea.o objfileb.o -llibname

libname这是库的名称。对于Linux中的EX-对于库libfoo。因此,您只会写-lfoo。但是在Windows中,它将是foo.lib。您可能必须使用-l ‹path_to_dir›在-l或-l之后没有任何空间才能找到文件。

如果使用IDE,请根据IDE在其他图书馆目录下指定它。