MD5 Function problem

i copy md5 function from this site ,
http://www.zedwood.com/article/cpp-md5-function
Here is 3 files .
main.cpp
md5.cpp
md5.h


main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "md5.h"
 
using std::cout; using std::endl;
 
int main(int argc, char *argv[])
{
    cout << "md5 of 'grape': " << md5("grape") << endl;
    return 0;
}


Why Example not work ,
And when I add #include "md5.cpp" It seems to work but is this safe or should I avoid it?
Last edited on
What is the error that it gives you?
You need to compile both your main.cpp and md5.cpp file, and then link them.
this is the one I based out of several years ago, so it does work. I found it to use a slow method to generate the hash hex-text, once you fix that, the rest is quick and fairly well done.
Ganado

Thanks for reply
Error
1
2
3
4
5
||=== Build: Debug in encript (compiler: GNU GCC Compiler) ===|
main.cpp|12|undefined reference to `MD5::MD5(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'|
main.cpp|12|undefined reference to `operator<<(std::ostream&, MD5)'|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


You need to compile both your main.cpp and md5.cpp file, and then link them.
how can compile both ? you main include md5.cpp sorry i'm beginner
Last edited on
If you're using an IDE, it depends on the particular IDE, but normally you simply add both .cpp to your project, and it knows how to handle the rest.

If you're on the command-line, read this short document: https://www.cs.fsu.edu/~myers/c++/notes/compilation.html
g++ -Wall -c md5.cpp                // translates frac.cpp into object code, frac.o 
g++ -Wall -c main.cpp               // translates main.cpp into object code, main.o 
g++ -Wall md5.o main.o -o sample    // links object code files into an executable called "sample" 


Can be simplified (at the cost of re-compiling object files) as:
g++ -Wall md5.cpp main.cpp -o sample


("-Wall" is the minimum warning level you should be compiling with. There are more such as -Wextra and -Wpedantic, to just name a few, which are also recommended.)
Last edited on
jonnin
Thanks for reply ,


Ganado
If you're using an IDE, it depends on the particular IDE, but normally you simply add both .cpp to your project, and it knows how to handle the rest.

The problem has been resolved thanks so much ,

Last edited on
Topic archived. No new replies allowed.