【问题标题】:How to link the library files for a C program in makefile in UNIX?如何在 UNIX 的 makefile 中链接 C 程序的库文件?
【发布时间】:2012-05-24 08:05:35
【问题描述】:

我使用 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,所以我现在想知道如何包含和链接什么? 如何找到需要链接的链接名称。

【问题讨论】:

  • 如果它在你手动编译而不是在你使用 make 时工作,那么你的 Makefile 中有一些奇怪的东西。你的 Makefile 里有什么?

标签: c unix makefile openssl


【解决方案1】:

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

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

【讨论】:

  • 这里不是CFLAGS而是CPPFLAGS吗?
  • @alk CFLAGS 是 C 编译器的标志,CPPFLAGS 是预处理器的标志。 -I 标志只对预处理器有用。
【解决方案2】:

这些是编译器错误,您还没有进入链接阶段。

我猜想,在编译 tgl_frbsenddata.ec 时,编译器不知道 DES_cblock 或 DES_key_schedule 是什么。在编译器遇到错误的那一刻,我怀疑 des.h 尚未包含任何您可能相信的内容。

您的编译器可能包含一个仅进行预处理的选项(在 gcc 和 clang 中,它是 -E)。我建议您在源文件上使用该选项运行它,以查看 typedef 是否出现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多