3

I am trying to use lessfs and learning how it uses mhash to produce its cryptographic fingerprints, so I am taking a look at mhash to see how it handles the hashing algorithms, so I am trying to run some of the examples provided in the program, but I am running into complications and errors

The Mhash example that I was trying to solve is found here: http://mhash.sourceforge.net/mhash.3.html (or below)

#include <mhash.h>
 #include <stdio.h>

 int main()
 {

        char password[] = "Jefe";
        int keylen = 4;
        char data[] = "what do ya want for nothing?";
        int datalen = 28;
        MHASH td;
        unsigned char *mac;
        int j;


        td = mhash_hmac_init(MHASH_MD5, password, keylen,
                            mhash_get_hash_pblock(MHASH_MD5));

        mhash(td, data, datalen);
        mac = mhash_hmac_end(td);

 /* 
  * The output should be 0x750c783e6ab0b503eaa86e310a5db738
  * according to RFC 2104.
  */

        printf("0x");
        for (j = 0; j < mhash_get_block_size(MHASH_MD5); j++) {
                printf("%.2x", mac[j]);
        }
        printf("\n");


        exit(0);
 }

But I get the following errors:

mhash.c.text+0x6c): undefined reference to `mhash_get_hash_pblock'
mhash.c.text+0x82): undefined reference to `mhash_hmac_init'
mhash.c.text+0x9c): undefined reference to `mhash'
mhash.c.text+0xa8): undefined reference to `mhash_hmac_end'
mhash.c.text+0xf9): undefined reference to `mhash_get_block_size'
collect2: error: ld returned 1 exit status
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
humblebeast
  • 303
  • 3
  • 16

1 Answers1

3

This is a linker error — ld is the linker program on Unix systems. The linker is complaining because you're using library functions (mhash_get_hash_pblock, etc.) but you didn't provide a definition for them.

The preprocessor directive #include <mhash.h> declares functions (and types, etc.) from the mhash library. That's good enough to compile your program (produce a .o file) but not to link it (to produce an executable): you also need to define these functions.

Add -lmhash at the end of your compilation command line. This instructs the linker that it can look for functions in the library libmhash.a on its search path; at run time, the functions will be loaded from libmhash.so on the search path. Note that libraries must come on the command line after they're used: the linker builds up a link of required functions, which need to be provided by a subsequent argument.

gcc -o myprogram myprogram.c -lmhash
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • Great! Thank you! Everything runs smoothly, but now I cannot properly run it in terminal, any suggestions on how I might be able to do that? This the error I get: – humblebeast Jul 03 '14 at 21:04
  • @humblebeast You called the program `example` (that's what the `-o` option is for). So run `./example` – Gilles 'SO- stop being evil' Jul 03 '14 at 21:09
  • Got it, thanks. Currently I am running ./example and no complains yet, the terminal has been running for about 3mins now and still nothing, the hashing might take that long? – humblebeast Jul 03 '14 at 21:15
  • do you have a chance to look at the mhash files? I am trying to trace how it actually reaches the MD5.c file for to produce the algorithms, thanks – humblebeast Jul 03 '14 at 22:24