如何在 C++ 项目中包含托管在 Github 上的库? (新 C++ 用户 - 以前使用包管理器的语言)

How to include libraries hosted on Github in a C++ project? (New C++ user - previously used language with package manager)

我想在 C++ 项目中包含 uWebSockets,但我不清楚如何执行此操作。我已经编译了具有 .so 依赖项的项目,当涉及到 github 上列出的项目时,我仍然感到困惑。

具体来说:

如果这些问题看起来很明显或很愚蠢,请提前致歉,我来自一种有包管理器的语言,所以这对我来说很新。

更新

按照 Github 存储库中的 "Getting Started" 指令,克隆存储库后,确保安装了依赖项并 运行 make,以下输出是观察到:

dave@desktop:~/gitrepositories/uWebSockets$ make
make `(uname -s)`
make[1]: Entering directory '/home/dave/gitrepositories/uWebSockets'
g++   -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 -s -o libuWS.so
In file included from src/WebSocketProtocol.h:5:0,
                 from src/WebSocket.h:4,
                 from src/Group.h:4,
                 from src/Group.cpp:1:
src/Networking.h:7:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
In file included from src/Networking.cpp:1:0:
src/Networking.h:7:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
In file included from src/WebSocketProtocol.h:5:0,
                 from src/WebSocket.h:4,
                 from src/Group.h:4,
                 from src/Hub.h:4,
                 from src/Hub.cpp:1:
src/Networking.h:7:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
In file included from src/Socket.h:4:0,
                 from src/Node.h:4,
                 from src/Node.cpp:1:
src/Networking.h:7:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
In file included from src/WebSocketProtocol.h:5:0,
                 from src/WebSocket.h:4,
                 from src/WebSocket.cpp:1:
src/Networking.h:7:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
In file included from src/Socket.h:4:0,
                 from src/HTTPSocket.h:4,
                 from src/HTTPSocket.cpp:1:
src/Networking.h:7:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
In file included from src/Socket.h:4:0,
                 from src/Socket.cpp:1:
src/Networking.h:7:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
Makefile:8: recipe for target 'Linux' failed
make[1]: *** [Linux] Error 1
make[1]: Leaving directory '/home/dave/gitrepositories/uWebSockets'
Makefile:6: recipe for target 'default' failed
make: *** [default] Error 2

只是回答问题:

Does the third-party repository need to be compiled prior to including it in my project?

是的,你必须先编译库,因为没有库你就不能link你的程序。

Where to store the library in the source code directory?

make install

会处理这件事。

How to link the source code with third-party libraries?

您没有 link 源代码,而是您的目标文件与库一起形成可执行文件。这看起来或多或少像

g++ -o sample main.o second.o more.o -L/path/to/libs -luWS -lmorelibs

Are tools such as CMake needed?

不是在这种情况下,uWebSockets 只使用 make,没有别的。


错误消息是关于缺少头文件的。这意味着,您必须为先决条件安装适当的开发包,即 openssl 和 zlib。

对于Debian/Ubuntu,这是由

完成的
sudo apt-get install libssl-dev zlib1g-dev