如何在compliling的同时将自定义库添加到GCC?

Emb*_*Prg 2 c gcc compilation

我从网上下载了一个名为libsal.a的C库,以访问该库中的API,我在main.c中包含一个名为#include的头文件.我使用以下命令编译它:

gcc -L /home/traana/Desktop/opensal-1.0.0/libsal.a main.c
Run Code Online (Sandbox Code Playgroud)

但在编译时遇到以下错误

main.c:3:17: fatal error: sal.h: No such file or directory
compilation terminated.
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

Bar*_*den 7

我相信您需要设置三个参数:

-I <include directory>
-L <library directory>
-l <library name>
Run Code Online (Sandbox Code Playgroud)

包含目录将是头文件的位置。库目录将是库的位置(.a 或 .so),库名将是库文件的名称,没有前导的“lib”前缀或其扩展名(即 -lsal 而不是 -l libsal 。一种 )。


Ern*_*ill 6

在编译源代码时,它不是库,而是相关的包含文件; 所以你需要告诉gcc这些是什么样的

gcc -c -I/home/traana/Desktop/opensal-1.0.0/include main.c
Run Code Online (Sandbox Code Playgroud)

然后将应用程序与类似的东西链接起来

gcc -L/home/traana/Desktop/opensal-1.0.0 -lsal main.o 
Run Code Online (Sandbox Code Playgroud)