-1

Ok, So I've been going through the various cases for this error and so far I have not been able to figure out exactly what is causing it this time (I am a little dyslexic, so apologies if it is staring me in the face) I keep finding references to problems like this: Undefined reference to main - collect2: ld returned 1 exit status and QtCreator build returns collect2: ld returned exit status 1 but these types of cases don't seem to fit.

I am writing a program that will automatically re-sign a message after it's been edited. The signing function takes in some information, hashes it with md5 (I know, but legacy requirements), and then encodes the output with Base64 (which takes a series of bytes via const unsigned char*, and the total size of the message)

I first tried Crypto++ libray for MD5, and it would provide expansion in the future for other hashes and protocols. After a massive number of undefined reference errors in the linking stage, I tried another md5 implementation I found online. This one gives me only two linking errors, but I think they may be caused by the same reason so I'll go with the second library to save post space.

A snippet of my C++ code for the message signing:

string createMessageSignature(string input)
{
    //byte digest[ CryptoPP::MD5::DIGESTSIZE ];

    //CryptoPP::MD5().CalculateDigest( digest, (byte*) input.c_str(), input.length() );

    //cout << "md5 signature: " << digest << endl;

    //string encodedDigest = base64_encode(digest, 32);

    int size = md5(input).length();
    string stringDigest = md5(input);

    const unsigned char* digest = reinterpret_cast<const unsigned char*>(stringDigest.c_str());

    cout << "md5 signature hash: " << digest << endl;

    string encodedDigest = base64_encode(digest, size);

    cout << "signature base64 encoded: " << encodedDigest << endl;

    return encodedDigest;
}

The md5 library I'm currently using is here: http://www.zedwood.com/article/121/cpp-md5-function (though I want to move to Crypto++ in the end, or maybe OpenSSL).

Error Messages from Linker (using MinGW on Win7, though also happens on GCC in a linux environment):

C:\Users\j********\Desktop\MessageSigner>C:/MinGW/bin/g++ -o MessageSigner.exe M
essageSigner.cpp
C:\Users\J****~1\AppData\Local\Temp\ccLejcEo.o:MessageSigner.cpp:(.text+0xc54):
 undefined reference to `md5(std::string)'
C:\Users\J****~1\AppData\Local\Temp\ccLejcEo.o:MessageSigner.cpp:(.text+0xc9f):
 undefined reference to `md5(std::string)'
collect2: ld returned 1 exit status

Any thoughts? Everything seems defined, and the code compiles. I've never had much experience debugging linker errors. Only compile option I've been using is -o for a file name, '-o MessageSigner.exe'

-Jimmy

Community
  • 1
  • 1
DaJJHman
  • 15
  • 3

1 Answers1

1

you have to link your program against the library you are using. Add -lmd5 or whatever your library is called to your g++ command and add a library path ( option -Lpath) if necessary

In case you don't work with a library but rather copied the md5.h and md5.cpp into your source directory you have to add the md5.cpp to your compiler call.

Draess
  • 218
  • 1
  • 11
  • Doh, I'm so stupid. Got too used to using an IDE with multiple source files, and now that I'm a restricted computer with command line I forgot to add the extra cpp files as parameters. But I do remember when I used g++ on the command line when I first started out, I would get compile errors for a cpp file being left out rather than deal with the linker. Could just be my bad memory. – DaJJHman Nov 16 '12 at 17:20