简体   繁体   English

如何使用共享库编译文件?

[英]How to compile a file using a shared library?

I am trying to compile a source given a .so file libfoo.so . 我正在尝试编译给定.so文件libfoo.so的源。 The only thing in this library is a function that just returns a number (yeah, I know, advanced stuff). 该库中唯一的东西是一个仅返回数字的函数(是的,我知道,高级的东西)。 The header file equivalent (I was provided with both, but am only supposed to use the .so) is named foo.h and the function is named int foo() . 等效的头文件(我提供了两者,但只能使用.so)被命名为foo.h ,函数被命名为int foo()

My source file is main.c : 我的源文件是main.c

#include <stdio.h>

#include "foo.h"

int main()
{
    int x = foo();

    printf("%d", x);

    return 0;   
}

Now, when trying to compile I have the following commands: 现在,当尝试编译时,我有以下命令:

gcc -Wall -fPIC -c main.c -o main.o

gcc -Wall -fPIC main.o -o main -lfoo -L.

The first command fails to create the object file, outputting the following error: 第一条命令无法创建目标文件,输出以下错误:

fatal error: foo.h: No such file or directory

I am using Ubuntu 16.04. 我正在使用Ubuntu 16.04。

I have also tried exporting the current location to LD_LIBRARY_PATH as I've seen suggested on a few other answers. 我还尝试将当前位置导出到LD_LIBRARY_PATH,正如我在其他一些答案中所看到的那样。

export LD_LBIRARY_PATH=$LD_LIBRARY_PATH:machine/Desktop/lib_test

You need to have the interface definition from the .h file and that file must be in the current directory or a directory on the include search path. 您需要具有.h文件中的接口定义,并且该文件必须位于当前目录或包含搜索路径中的目录中。

Note that on some systems filenames and paths are case dependent. 请注意,在某些系统上,文件名和路径取决于大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM