MacOS ld:找不到符号(x86_64)

时间:2018-02-07 13:31:54

标签: c++

我有以下错误:

    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)

我已经检查了所有类似问题及其解决方案,但没有任何对我有用。

我正在尝试测试uWebSockets(https://github.com/uNetworking/uWebSockets)并具有以下文件结构:

  • my_app(有main.cpp)
  • ext / uWebSockets(上面的存储库的克隆)

所以,我正在做以下事情:

~/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

并在/ ext / uWebSockets中获取libuWS.dylib

然后,我执行以下操作:

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

所以现在我在/ my_app中有main.o。但是,当我试图:

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

我收到上述错误:

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)

这是与-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)

如何正确链接?

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

1 个答案:

答案 0 :(得分:1)

最后,我能够解决它。问题出在zlib上,我没有提到。

所以正确的运行方式是g++ -o start main.o -luWS -lz