c - 如何在 UNIX 中的 makefile 中链接 C 程序的库文件?

标签 c unix makefile openssl

我已经使用 openssl 编写了一个解密函数,我在独立程序中对其进行了测试并且运行良好。但是这个功能是一个巨大项目的一部分,所以它必须包含在那个程序中。 为了执行我的独立程序,我使用了以下运行良好的命令:

cc -c aaa.c -I/usr/local/ssl/include
gcc -o aaa aaa.o -I/usr/local/ssl/include -L/usr/local/ssl/lib -lcrypto -lm
./aaa

我已经为我的主程序创建了一个 makefile,这个函数将在其中被调用。

这两个程序都单独运行良好,但是当我在我的程序中插入函数的定义时,它给了我 openssl 头文件之一(即 des.h)中的那些变量的错误。 我使用了一些 DES_cblock 类型的变量:

typedef unsigned char DES_cblock[8];

还有一个结构,定义如下:

typedef struct DES_ks
{
    union
    {
        DES_cblock cblock;
        DES_LONG deslong[2];
    }ks[16];
} DES_key_schedule;

我已经在我的程序中像这样使用了这个结构

DES_key_schedule keysched1,keysched2,keysched3;

但它没有识别这些变量。因为当我执行我的独立程序时没有这样的错误,这意味着我无法在主程序中正确链接库文件。我如何使这项工作。 这些是我收到的错误:

Syntax error at line 1399, column 16,file/export/home/jayesho/src/custom/FRB/tgl_frbsenddata.ec:
    Error at line 1399, column 16 in file /export/home/jayesho/src/custom/FRB/tgl_fr
bsenddata.ec
    DES_cblock hex_key1,hex_key2,hex_key3,hex_ivec,iv;
...............1
PCC-S-02201, Encountered the symbol "hex_key1" when expecting one of the followi
ng:
   ; , = : ( [ * ? | & < > + - / % . ^ *= /= %= += -= <<= >>=
   &&= ||= ^= | & == != <= >= << >> ++ -- ->
The symbol ";" was substituted for "hex_key1" to continue.
Syntax error at line 1402, column 22, file /export/home/jayesho/src/custom/FRB/tgl_frbsenddata.ec:
Error at line 1402, column 22 in file /export/home/jayesho/src/custom/FRB/tgl_fr
bsenddata.ec
    DES_key_schedule keysched1,keysched2,keysched3;
.....................1
PCC-S-02201, Encountered the symbol "keysched1" when expecting one of the follow
ing:
   ; , = : ( [ * ? | & < > + - / % . ^ *= /= %= += -= <<= >>=
   &&= ||= ^= | & == != <= >= << >> ++ -- ->
The symbol ";" was substituted for "keysched1" to continue.
Syntax error at line 1436, column 38, file /export/home/jayesho/src/custom/FRB/tgl_frbsenddata.ec:
Error at line 1436, column 38 in file /export/home/jayesho/src/custom/FRB/tgl_fr
bsenddata.ec
   if (DES_set_key_checked((C_Block *)hex_key1, &keysched1))

现在我只需要在我的程序中正确链接库文件就可以使整个程序运行起来。前面提到的头文件是 des.h,它是 openssl 的一部分。 我也尝试通过 -lcrypto 包含加密库 以前这个 des.h 没有被正确包含,但现在我成功地包含了 des.h 而没有错误。 也有人提出仅仅包含头文件是不够的,它的实现文件也需要是linked。 ,所以我现在想知道如何包含和链接什么? 如何找出需要链接的链接名称。

最佳答案

通常,您使用 LDLIBS 为链接器定义 -l 选项,使用 LDFLAGS 为链接器定义 -L 标志。编辑 Makefile 并添加适当的选项。

CPPFLAGS += -I/usr/local/ssl/include 
LDFLAGS += -L/usr/local/ssl/lib 
LDLIBS += -lcrypto -lm

关于c - 如何在 UNIX 中的 makefile 中链接 C 程序的库文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733493/

相关文章:

c++ - SSE指令中的UnsignedSaturate是什么意思?

java - Jmeter:- 线程组在 Unix 上执行两次

linux - tar 命令不能正确排除文件

makefile - ***配方在第一个目标之前开始。停止

makefile - 为什么Make中的 `shell`命令删除换行符?

c - 出现 C 错误,提示我对函数有 undefined reference ,但该函数已定义

c - "int j = (strlen) (str)"是什么意思?

c - c中memset()的使用

c - Sockets-客户端双重打印

unix - 如何在第 1 行 libsvm 处获得正确的输入格式