Why do I get "undefined reference" errors when linking against OpenSSL?

The solution is as simple as adding the -l flags at the end:

gcc test.c -o test -lssl -lcrypto

The order matters because ld since Ubuntu 11.04 is invoked with the -as-needed switch by default, so that files/libraries which depend on other libraries must come before these other libraries, i.e. test.c needs libcrypto, so it must come before -lcrypto.

For more information, see Toolchain Transition in Natty Narwhal.