在Linux中安装新库,并从我的C代码访问它(Installing a new library in Linux, and accessing it from my C code)

编程入门 行业动态 更新时间:2024-10-28 18:32:37
在Linux中安装新库,并从我的C代码访问它(Installing a new library in Linux, and accessing it from my C code)

我正在开发一个项目,需要我下载并使用它 。 在下载的文件夹中,提取时我会看到三件事:

一个名为“include”的文件夹 一个名为“src”的文件夹 一个名为“Makefile”的文件

经过一些研究,我发现我必须导航到包含这些文件的目录,然后输入命令make 。

它似乎在我的系统中安装库。 所以我尝试了一些应该使用该库的代码示例:

csp_conn_t * conn; csp_packet_t * packet; csp_socket_t * socket = csp_socket(0); csp_bind(socket, PORT_4); csp_listen(socket, MAX_CONNS_IN_Q); while(1) { conn = csp_accept(socket, TIMEOUT_MAX); packet = csp_read(conn, TIMEOUT_NONE); printf(“%S\r\n”, packet->data); csp_buffer_free(packet); csp_close(conn); }

这就是代码的示例服务器端所给出的全部内容。 所以我决定将这些添加到顶部:

#include <csp.h> #include <csp_buffer.h> #include <csp_config.h> #include <csp_endian.h> #include <csp_interface.h> #include <csp_platorm.h>

我想我是在正确的轨道上,我试图用gcc编译代码,但是我得到了这个错误:

csptest_server.c:1: fatal error: csp.h: No such file or directory compilation terminated.

我以为我可能没有正确安装库,但为了确保,我发现我可以通过运行此命令来检查,并获得此结果:

find /usr -iname csp.h /usr/src/linux-headers-2.6.35-28-generic/include/config/snd/sb16/csp.h /usr/src/linux-headers-2.6.35-22-generic/include/config/snd/sb16/csp.h

所以似乎安装了csp.h,也许我在标题包含行中错误地引用它? 任何见解? 非常感谢。

I am working on a project which requires me to download and use this. Inside the downloaded folder, when extracted I am presented with three things:

A folder called "include" A folder called "src" A file called "Makefile"

After some research, I found out that I have to navigate to the directory which contains these files, and just type in the command make.

It seemed to install the library in my system. So I tried a sample bit of code which should use the library:

csp_conn_t * conn; csp_packet_t * packet; csp_socket_t * socket = csp_socket(0); csp_bind(socket, PORT_4); csp_listen(socket, MAX_CONNS_IN_Q); while(1) { conn = csp_accept(socket, TIMEOUT_MAX); packet = csp_read(conn, TIMEOUT_NONE); printf(“%S\r\n”, packet->data); csp_buffer_free(packet); csp_close(conn); }

That's all that was given for the sample server end of the code. So I decided to add these to the top:

#include <csp.h> #include <csp_buffer.h> #include <csp_config.h> #include <csp_endian.h> #include <csp_interface.h> #include <csp_platorm.h>

Thinking I was on the right track, I tried to compile the code with gcc, but I was given this error:

csptest_server.c:1: fatal error: csp.h: No such file or directory compilation terminated.

I thought I may not have installed the library correctly after all, but to make sure, I found out I could check by running this command, and getting this result:

find /usr -iname csp.h /usr/src/linux-headers-2.6.35-28-generic/include/config/snd/sb16/csp.h /usr/src/linux-headers-2.6.35-22-generic/include/config/snd/sb16/csp.h

So it seems like the csp.h is installed, maybe I am referencing it incorrectly in the header include line? Any insight? Thanks a lot.

最满意答案

make命令可能只构建库,但不安装它。 你可以尝试sudo make install 。 这是“常用”方法,但我建议您查看库的文档,如果有的话。

只有在没有权限编写系统的include和library目录时才需要sudo命令,这可能就是你的情况。

另一种可能性(而不是安装库)告诉GCC库的源代码和生成的二进制文件的位置(通过gcc命令的-I和-L选项)。

The make command is probably only building the library, but not installing it. You could try sudo make install. This is the "common" method, but I recommend you to check the library's documentation, if any.

The sudo command is only necessary if you have no permissions to write the system's include and library directories, which may be your case.

Another possibility (instead of installing the library) is telling GCC the location of the library's source code and generated binaries (by means of the -I and -L options of the gcc command.

更多推荐

本文发布于:2023-08-07 04:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并从   代码   Linux   Installing   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!