Dmitriy Koval
Dmitriy Koval

Reputation: 156

MacOS ld: symbols not found (x86_64)

I have the following error:

    Undefined symbols for architecture x86_64:
  "_inflateEnd", referenced from:
      uWS::Hub::~Hub() in main.o
  "_inflateInit2_", referenced from:
      uWS::Hub::Hub(int, bool, unsigned int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have checked all the similar questions and their solutions but nothing worked for me.

I am trying to test uWebSockets (https://github.com/uNetworking/uWebSockets) and have the following file structure:

So, I am doing the following:

~/ext/uWebSockets$ make                                                                                                                                    
make `(uname -s)`
c++   -std=c++11 -O3 -I src -shared -fPIC src/Extensions.cpp src/Group.cpp src/Networking.cpp src/Hub.cpp src/Node.cpp src/WebSocket.cpp src/HTTPSocket.cpp src/Socket.cpp src/Epoll.cpp -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include -o libuWS.dylib

and obtain libuWS.dylib in /ext/uWebSockets

Then, I do the following:

~/my_app$ g++ -c main.cpp -o main.o -I../ext/uWebSockets/src -I/usr/local/opt/openssl/include -std=c++11

So now I have main.o in /my_app. But when I'm trying to:

~/my_app$ g++ -o start main.o -L../ext/uWebSockets -luWS

I receive the aforementioned error:

Undefined symbols for architecture x86_64:
  "_inflateEnd", referenced from:
      uWS::Hub::~Hub() in main.o
  "_inflateInit2_", referenced from:
      uWS::Hub::Hub(int, bool, unsigned int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is the same command with -v:

~/my_app$ g++ -o start main.o -L../ext/uWebSockets -luWS -v
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o start -L../ext/uWebSockets main.o -luWS -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_inflateEnd", referenced from:
      uWS::Hub::~Hub() in main.o
  "_inflateInit2_", referenced from:
      uWS::Hub::Hub(int, bool, unsigned int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

How can I link them correctly?

main.cpp:

#include <iostream>
#include "uWS.h"

int main() {
    uWS::Hub h;

    h.onError([](void *user) {
        std::cout << "WebSocket: Error has occured" << std::endl;
    });

    h.onConnection([](uWS::WebSocket<uWS::CLIENT> *ws, uWS::HttpRequest req) {
        std::cout << "Client established a remote connection over non-SSL" << std::endl;
    });

    h.onDisconnection([](uWS::WebSocket<uWS::CLIENT> *ws, int code, char *message, size_t length) {
        std::cout << "Client got disconnected with data: " << ws->getUserData() << ", code: " << code << ", message: <" << std::string(message, length) << ">" << std::endl;
    });

    // url, user, headers, timeout, group client
    h.connect("wss://www.test.com/", (void *) 0, {}, 5000);

    h.run();
    std::cout << "Falling through testConnections" << std::endl;

    return 0;
}

Upvotes: 1

Views: 967

Answers (1)

Dmitriy Koval
Dmitriy Koval

Reputation: 156

Finally, I was able to fix it. The problem was in zlib, which I didn't include.

So the correct way of running this is g++ -o start main.o -luWS -lz

Upvotes: 1

Related Questions