如何在C 项目中包括在GitHub上托管的库?(新的C 用户 - 以前使用的语言与软件包管理器使用)

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

本文关键字:用户 语言 管理器 软件包 新的 包括 项目 GitHub      更新时间:2023-10-16

我想在C 项目中包括uwebsockets,我不清楚如何执行此操作。我已经用.so依赖项编译了项目,在GitHub上列出的项目时,我仍然感到困惑。

特别:

  • 在将其包含在我的项目中之前,是否需要对第三方存储库进行编译?
  • 在源代码目录中将库存储在哪里?
  • 如何将源代码与第三方库联系起来?
  • 是否需要诸如CMAKE之类的工具?

事先表示歉意,如果这些问题显而易见或愚蠢,我来自带有软件包经理的语言,所以这对我来说是新的。

update

在GitHub存储库中的"入门"指令之后,在克隆存储库后,确保依赖项已安装并运行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

只是为了回答问题:

在将其包含在我的项目中之前,是否需要对第三方存储库进行编译?

是的,您必须以前编译库,因为没有图书馆就无法链接程序。

在源代码目录中将库存储在哪里?

make install

会照顾好这个。

如何将源代码与第三方库联系起来?

您不链接源代码,而是将对象文件与库一起链接以形成可执行文件。这看起来或多或少像

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

是否需要诸如CMAKE之类的工具?

不是在这种情况下, uWebSockets只使用make,别无其他。


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

对于debian/ubuntu,这是由

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