c - Undefined symbols for architecture x86_64 (clang) -


i'm trying use openssl compute sha1 hash c program. compiling clang on mac os x yosemite intel i7 (so 64 bit).

the relevant piece of code so:

#include <openssl/evp.h> ... unsigned char outhash[20];  hash("sha1","abcd", 20, outhash); 

the thing is, when using "hash" function openssl/evp.h, compiling clang yields following error:

undefined symbols architecture x86_64:   "_hash", referenced from:       _getrandomsha1 in main-68ccd6.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation) 

so looks openssl not found linker (the "hash" function unidentified). ideas on how fix this?

edit:

it turns out trying use function not exist ("hash") - sorry misleading that.

however i'm still having quite same problem: including openssl/evp.h not seem work.

this hash function using, uses evp perform sha1 encoding:

unsigned int hash(const char *mode, const char* datatohash, size_t datasize, unsigned char* outhashed) {     unsigned int md_len = -1;     const evp_md *md = evp_get_digestbyname(mode);     if(null != md) {         evp_md_ctx mdctx;         evp_md_ctx_init(&mdctx);         evp_digestinit_ex(&mdctx, md, null);         evp_digestupdate(&mdctx, datatohash, datasize);         evp_digestfinal_ex(&mdctx, outhashed, &md_len);         evp_md_ctx_cleanup(&mdctx);     }     return md_len; } 

and call:

hash("sha1","abcd", 20, outhash); 

i compiling same before, compile command (pretty simple):

 clang main.c 

and i'm getting following error linker:

undefined symbols architecture x86_64:   "_evp_digestfinal_ex", referenced from:       _hash in main-935849.o   "_evp_digestinit_ex", referenced from:       _hash in main-935849.o   "_evp_digestupdate", referenced from:       _hash in main-935849.o   "_evp_md_ctx_cleanup", referenced from:       _hash in main-935849.o   "_evp_md_ctx_init", referenced from:       _hash in main-935849.o   "_evp_get_digestbyname", referenced from:       _hash in main-935849.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation) 

#include <openssl/evp.h> ... unsigned char outhash[20];  hash("sha1","abcd", 20, outhash); 

openssl not have int hash(...) or char* hash(...) function.

$ cat /usr/include/openssl/evp.h | grep hash returns 0 hits. man 3 hash returns bsd's "hash database access method".


undefined symbols architecture x86_64:   "_hash", referenced from:       _getrandomsha1 in main-68ccd6.o 

latching on _getrandomsha1, there sha function, available sha.h, , not evp.h (i filtered out deprectaed_in... macro):

$ cat /usr/include/openssl/sha.h | grep sha | grep char ... unsigned char *sha(const unsigned char *d, size_t n, unsigned char *md); ... 

i believe uses static buffer, , use discouraged.

the project recommends using evp message digests interface now.


(edit): compiling same before, compile command (pretty simple):
clang main.c

see linking openssl libraries program. need add -lcrypto end of compile command.


(edit, comment): have openssl @ /opt/local/include/openssl....

you going have more problems because runtime linking against wrong version of openssl library. problem, see statically link openssl in xcode. same problem exists command line , xcode.

here's easiest solution. notice does not use -lcrypto (-lfoo solution, except when runtime linking wrong shared object becomes problem):

 clang main.c -i /opt/local/include /opt/local/lib/libcrypto.a -o my_hash.exe  

an archive collection of object files, archive can used in place object file required.

ox s not honor rpaths another solution build openssl , program rpath correct version of library loaded @ runtime. that, see compilation , installation | using rpaths on openssl wiki , build openssl rpath? on stack overflow..


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -