Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在同一个库中具有标头的gcc共享库_C_Gcc_Shared Libraries - Fatal编程技术网

在同一个库中具有标头的gcc共享库

在同一个库中具有标头的gcc共享库,c,gcc,shared-libraries,C,Gcc,Shared Libraries,我正在尝试使用以下代码编译共享库(.so): libreceive.h: #include <stddef.h> int receive(int sockfd, void *buf, size_t len, int flags); 我得到以下错误: fatal error: libreceive/libreceive.h: No such file or directory compilation terminated. 这里的问题是,我试图将.h包含在我正在构建的库中,并在同一

我正在尝试使用以下代码编译共享库(.so): libreceive.h:

#include <stddef.h>
int receive(int sockfd, void *buf, size_t len, int flags);
我得到以下错误:

fatal error: libreceive/libreceive.h: No such file or directory
compilation terminated.
这里的问题是,我试图将.h包含在我正在构建的库中,并在同一时间从.c中的同一个库中使用它。 我知道我想做的是可能的,但我做不到。
我该怎么做呢。

因为
libreceive.h
libreceive.c
似乎在同一个目录中(从编译器调用判断),通常的方法是

#include "libreceive.h"
为了使用

#include <libreceive/libreceive.h>
#包括

libreceive.h
必须位于名为
libreceive
的目录中,并且该目录必须是include路径的一部分。实现这一点是可能的,但我认为这在这里既没有必要也没有用处。

您在这里遗漏了一些步骤。
考虑下面的设置。

文件:
add.c

#include "header.h"
int add(int a, int b)
{
    printf("SIZE: %d\n", SIZE);
    return a+b;
}
文件:
sub.c

#include "header.h"
int sub(int a, int b)
{
    printf("SIZE: %d\n", SIZE);
    return a-b;
}
文件:
header.h
,位于名为
include
的目录中

#include <stdio.h>
#define SIZE 100
int add(int a, int b);
int sub(int a, int b);
上述命令将按名称
libsample.so
构建一个
.so

其中
.c
(类似函数)和
.h
(类似
#define
s)中的所有定义都将包含在您的库中


如何在代码中使用此选项:

以文件为例 文件:
main.c

#include <stdio.h>
int main()
{
    int a = 3, b = 4;
    printf("Return : %d\n", add(a, b));
    return 0;
}
上面的命令应该创建一个名为
exe
的二进制文件

$./exe
SIZE : 100              /* SIZE Defined in .h file */
Return : 7              /* Defined in add.c */

你可以参考这本指南:

最后我决定使用
#包括那些家伙建议的“libreceive.h”
。我遇到的问题是,当id do sudo gcc和我的usr在/usr/local/lib处有$LD_LIBRARY_路径时,编译器正在查找默认的so in/usr/lib,因此gcc在编译时找不到我的库 另一个问题是调用thos.so的程序在某个不存在的文件夹中查找.h,我不得不添加它。
谢谢大家的回答

考虑到你的
libreceive.h
在目录
include
中,你是否尝试过
gcc-o libreceive.o-c libreceive.c-I/Path/to/include
?考虑到你的
libreceive.h
是在目录
include
中,libreceive是我试图编译的库在这种情况下,你必须使用
-shared
-fPIC
选项与gcc一起,没有提到这个?我也试过这个:gcc-shared-o libreceive.so-fPIC libreceive.c在很多变体中,没有运气…@user1928596如果
libreceive.c
libreceive.h
在同一个文件夹中,你可以使用
libreceive.c
中包含“libreceive.h”
。在任何其他想要使用libreceive的项目中,您可以使用
#include
和编译器标志
-I/path/to
,其中/path/to包含libreceive文件夹(意思是
libreceive.h
的完整路径是
/path/to/libreceive/libreceive.h
),我在导入时在其他地方使用它,对于你答案的第二部分,.h不应该在路径上的另一个文件夹中,我不应该放在同一个文件夹中library@user1928596您是否在其他地方使用它与您在libreceive.c中如何使用它无关。在其他项目中使用该库时,您不必使用相同的
#include
指令。您可以在库本身中使用
#include“libreceive.h”
,其中标头位于同一目录中,也可以在客户端代码中使用
#include
,从某个目录
libreceive
的include路径中提取标头,但是当我这样做的时候,它确实可以编译,当我在另一个classgcc-o测试中使用这个库的时候。c-L如果它不起作用(我把库放在路径上)谢谢你,我发现了问题在其他地方,不管怎样,这两种技术现在都起作用了,所以我将投赞成票,并为我的问题所在写下我的答案,非常感谢
/* Build `.o` files first */
$ gcc -fPIC -c sub.c -I path/to/include/
$ gcc -fPIC -c add.c -I path/to/include/

/* Build shared library called libsample.so */
$ gcc -shared -o libsample.so add.o sub.o
#include <stdio.h>
int main()
{
    int a = 3, b = 4;
    printf("Return : %d\n", add(a, b));
    return 0;
}
$ export LD_LIBRARY_PATH=/path/to/direc/containing/.so/file
$ gcc -o exe main.c -lsample -L/path/to/direc/containing/.so/file
$./exe
SIZE : 100              /* SIZE Defined in .h file */
Return : 7              /* Defined in add.c */