Okay I use Dev-C++ as my IDE. I have never linked my own files. I am pretty much a noob on C++, but not to programming in general.

My issue that I have been almost pulling my hair out with is this. I am using a premade MD5 header file (or rather am trying to use). I have yet to get it to work. Even using the sample I downloaded with the source files.

Here is the header file: md5.h

/////////////////////////////////////////////////////////////////////////
// MD5.cpp
// Implementation file for MD5 class
//
// This C++ Class implementation of the original RSA Data Security, Inc.
// MD5 Message-Digest Algorithm is copyright (c) 2002, Gary McNickle.
// All rights reserved.  This software is a derivative of the "RSA Data
//  Security, Inc. MD5 Message-Digest Algorithm"
//
// You may use this software free of any charge, but without any
// warranty or implied warranty, provided that you follow the terms
// of the original RSA copyright, listed below.
//
// Original RSA Data Security, Inc. Copyright notice
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
// rights reserved.
//
// License to copy and use this software is granted provided that it
// is identified as the "RSA Data Security, Inc. MD5 Message-Digest
// Algorithm" in all material mentioning or referencing this software
// or this function.
// License is also granted to make and use derivative works provided
// that such works are identified as "derived from the RSA Data
// Security, Inc. MD5 Message-Digest Algorithm" in all material
// mentioning or referencing the derived work.
// RSA Data Security, Inc. makes no representations concerning either
// the merchantability of this software or the suitability of this
// software for any particular purpose. It is provided "as is"
// without express or implied warranty of any kind.
// These notices must be retained in any copies of any part of this
// documentation and/or software.
/////////////////////////////////////////////////////////////////////////

typedef unsigned       int uint4;
typedef unsigned short int uint2;
typedef unsigned      char uchar;

char* PrintMD5(uchar md5Digest[16]);
char* MD5String(char* szString);
char* MD5File(char* szFilename);

class md5
{
// Methods
public:
	md5() { Init(); }
	void	Init();
	void	Update(uchar* chInput, uint4 nInputLen);
	void	Finalize();
	uchar*	Digest() { return m_Digest; }

private:

	void	Transform(uchar* block);
	void	Encode(uchar* dest, uint4* src, uint4 nLength);
	void	Decode(uint4* dest, uchar* src, uint4 nLength);


	inline	uint4	rotate_left(uint4 x, uint4 n)
	                 { return ((x << n) | (x >> (32-n))); }

	inline	uint4	F(uint4 x, uint4 y, uint4 z)
	                 { return ((x & y) | (~x & z)); }

	inline  uint4	G(uint4 x, uint4 y, uint4 z)
	                 { return ((x & z) | (y & ~z)); }

	inline  uint4	H(uint4 x, uint4 y, uint4 z)
	                 { return (x ^ y ^ z); }

	inline  uint4	I(uint4 x, uint4 y, uint4 z)
	                 { return (y ^ (x | ~z)); }

	inline	void	FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
	                 { a += F(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

	inline	void	GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
                     { a += G(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

	inline	void	HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
                     { a += H(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

	inline	void	II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
                     { a += I(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

// Data
private:
	uint4		m_State[4];
	uint4		m_Count[2];
	uchar		m_Buffer[64];
	uchar		m_Digest[16];
	uchar		m_Finalized;

};

Then we have their test file they are trying to call the functions with: md5test.cpp

// md5Test
// A simple test of the MD5 library.


#include <stdio.h>
#include "md5.h"


int main(void)
{
  printf("MD5 Key of the phrase 'md5Test' = <%s>\n", MD5String("md5Test"));

  printf("MD5 Key of the file 'md5.cpp' = <%s>\n", MD5File("md5.cpp"));

  return 0;
}

And now the error given is...

[Linker error] undefined reference to `MD5String(char*)'

I have even gone as far as to include all the header source in the file and remove the include, but that didnt even work :?:.

Thank you for anyone willing to read this long arsed post and help me.
JS

lol, a possible problem is because of the compiler you are using is not updated with the .lib files necessary for this project.

Try commenting out the line:

printf("MD5 Key of the phrase 'md5Test' = <%s>\n", MD5String("md5Test"));

This should get it to compile, however you will not be able to use the MD5Test.

Something else that might work for you (However this will limit some experience.. possibly) is this:

char * MyVar = "AStringWee";
printf(MD5String(MyVar));

I am not fully experienced with <stdio.h> so printf might not be what you want to use for the above example.

You may need to include the line:

using namespace std;

In the .h file after the includes statements. Can't remember the reason for it other than a change to the language a few years back. At any rate you have a scope problem and that might just fix it. - I am guessing though. I did something similar a few years back and we we had code that worked, and a year later didn't. Rather painful to work out why.

I appreciate both of your responses. Below is the code I came up with to test it... it actually tries a bit of what you both said.

#include <cstdlib>
#include <iostream>
#include <string>
#include <md5.h>

using namespace std;

char* szString = "Some string you want to generate an MD5 key for.";

int main(int argc, char *argv[])
{
    char* sString = MD5String(szString);
    
    cout << "Some string you want to generate an MD5 key for." << "MD5 Output: " << sString;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

If you can come up with anything else please let me know. (On the first post example I actually didnt copy both errors. It has a problem with any function from that header file :(.

JS

Okay... for some reason I had forgotten to add the files to the project, but I also thought I didnt have to since I "#included """ them :-|

Oh well I appreciate the help you offered.

JS

Glad to hear you got it sorted. I was just thinking that it sounds like the linker can't see the file... i.e. wrong directory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.