diegoaguilar
diegoaguilar

Reputation: 8376

Compiling with gcc including .h files?

I'm trying to compile some AES implementation code from http://www.efgh.com/software/rijndael.htm, I got a txt file and splitted it up so I got 3 files:

encrypt.c
decrypt.c
rijndael.h

Having all this 3 files in the same folder, I try to compile any of encrypt.c or decrypt.c files but it throws some errors about undefined functions which actually are in rijndael.h

I'm performing compilation this way:

gcc -o encrypt encrypt.c or gcc -o decrypt decrypt.c

And I get:

/tmp/cch6JvXT.o: In function main:
encrypt.c:(.text+0x127): undefined reference to rijndaelSetupEncrypt
encrypt.c:(.text+0x1c6): undefined reference to rijndaelEncrypt
collect2: error: ld returned 1 exit status

But rijndaelSetupEncrypt and rijndaelEncrypt are in the rijndael.h file

Upvotes: 0

Views: 2106

Answers (2)

ionela.voinescu
ionela.voinescu

Reputation: 384

There is a difference between an "undeclared function" error and an "undefined function" error. The first one is given when it can not find the prototype (meaning only the function header) of a function you've used, prototypes being usually put in the .h files and included in your .c files. The second error appears when it finds the prototype but not the definition of the function. The definition of a function (meaning the entire body of a function) can be fount either in a library or in another .c file that you should add to your compile command.

For "undefined function" error you could try

gcc -o enc_dec encrypt.c decrypt.c

if the function it cannot find is in one of the two .c files you've mentioned. If it's not, you might have forgotten to link a library.

Later edit:
With the rijndael.c file:

gcc -o decrypt rijndael.c decrypt.c
gcc -o encrypt rijndael.c encrypt.c

It doesn't matter if rijndael.h hasn't got a main function. I suppose it has definitions for some of the functions used in decrypt.c and encrypt.c

Upvotes: 1

72DFBF5B A0DF5BE9
72DFBF5B A0DF5BE9

Reputation: 5012

Actually in your example, you should have 4 files, encrypt.c decrypt.c rijndael.c and rijndael.h

So you have to compile rijndael.c and encrypt.c or same with decrypt together. .h files will be automatically used during compilation of c file which included the .h (header) file.

Upvotes: 1

Related Questions