简体   繁体   中英

How to link the library files for a C program in makefile in UNIX?

I have written a decryption function using openssl which I tested in a standalone program and it worked fine. But this function is a part of a huge project so it has to be included in that program. To execute my standalone program I used the following commands which worked fine:

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

I have made a makefile for my main program inside which this function will be called.

Both the programs are working fine individually but when I inserted the definition on the function in my program, it gave me errors for those variables which were in one of the header file of openssl (ie des.h). I have made use of a few variables of type DES_cblock:

typedef unsigned char DES_cblock[8];

There is another structure with the following definition:

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

I have made use of this structure in my program like this

DES_key_schedule keysched1,keysched2,keysched3;

But it is not recognizing these variables. And since there was no such error when I was executing my standalone program it means that I am not able to link the library files properly in the main program. How do I make this work. These are the errors that I am getting:

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))

Now I just need to link the library files properly in my program to make the whole program running. The header file as mention before is des.h which is a part of openssl. I tried including the crypto library also by -lcrypto Previously this des.h was not getting included properly but now have I included the des.h successfully without error. Someone also suggested that merely including the header file is not enough and its implementation file also needs to be linked , so I now I want to know how to include and link what? How to find out the name of the link which needs to be linked.

Typically, you define -l options to the linker using LDLIBS, and -L flags using LDFLAGS. Edit the Makefile and add the appropriate options.

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

These are compiler errors, you haven't got to the linking stage yet.

I would guess that, when compiling tgl_frbsenddata.ec, the compiler doesn't know what a DES_cblock is or a DES_key_schedule. At the point where the compiler hits the lines with the errors in, I suspect des.h has not yet been included whatever you may believe about whether you have or not.

Your compiler probably includes an option to do preprocessing only (in gcc and clang, it is -E ). I suggest you run it with that option on your source file to see if the typedefs appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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