I'm writing a program to get myself acquainted with OpenSSL, libncurses, and UDP networking. I decided to work with OpenSSL's SHA256 to become familiar with industry encryption standards, but I'm having issues with getting it working. I've isolated the error to the linking of OpenSSL with the compiled program. I'm working on Ubuntu 12.10, 64 bit. I have the package libssl-dev installed.
Take, for instance, the C++ main.cpp:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
#include <openssl/sha.h>
string sha256(const string str)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str.c_str(), str.size());
SHA256_Final(hash, &sha256);
stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
int main()
{
cout << sha256("test") << endl;
cout << sha256("test2") << endl;
return 0;
}
I'm using the SHA256() function found here as a wrapper for OpenSSL's SHA256 functionality.
When I attempt to compile with the following g++ arguments, I receive the following error:
millinon@myhost:~/Programming/sha256$ g++ -lssl -lcrypto -o main main.cpp
/tmp/ccYqwPUC.o: In function `sha256(std::string)':
main.cpp:(.text+0x38): undefined reference to `SHA256_Init'
main.cpp:(.text+0x71): undefined reference to `SHA256_Update'
main.cpp:(.text+0x87): undefined reference to `SHA256_Final'
collect2: error: ld returned 1 exit status
So, GCC clearly recognizes OpenSSL's defined functions and types, but ld is failing to find the function symbols referred to in sha.h.
Do I need to manually point to a specific shared object or directory?
Thanks!
You make a very common beginners mistake... Putting the libraries you link with in the wrong place on the command line when you build.
Dependencies are reversed on the command line, so something that depends on something else should actually be put before what it depends on on the command line.
In your example, you have a source file main.cpp
that depends on some set of libraries, then the source file should be before the libraries it depend on:
$ g++ -o main main.cpp -lssl -lcrypto
To be safe, always put libraries last, after any source or object files listed on the command line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With