Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

c - How to link OpenSSL with emscripten?

I'm trying to compile some C code that uses OpenSSL with emscripten, but I get unresolved symbol warnings like:

warning: unresolved symbol: SHA256_Init
warning: unresolved symbol: SHA256_Final
warning: unresolved symbol: SHA256_Update

I compiled the code using this command:

emcc SHA256.c -lssl -lcrypto -L /usr/local/openssl-1.0.2k/lib/ -I /usr/local/openssl-1.0.2k/include -s WASM=1 -o SHA256.html --emrun

With the following source code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>


void sha256(char *string, char outputBuffer[65])
{
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, string, strlen(string));
    SHA256_Final(hash, &sha256);
    int i = 0;
    for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
    }
    outputBuffer[64] = 0;
}


int main (void)
{
    static unsigned char buffer[65];
    sha256("string", buffer);
    printf("%s
", buffer);

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Compile the two libraries : crypto and openssl with emscripten, rather than using the system versions.

Use the generated .bc files for these libraries as linker arguments to the em++ command and it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...