2017-08-31 41 views
1

我試圖編譯使用OpenSSL的與emscripten一些C代碼,但我得到解決的符號警告信息,如:如何將OpenSSL與emscripten鏈接?

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

我使用這個命令編譯的代碼:

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 

用下面的源代碼

#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\n", buffer); 

    return 0; 
} 
+0

請特別注意@ mrduclaw的回答以及OpenSSL庫位於鏈接命令中的位置。 – jww

+0

問題是,您期望使用(-lssl -lcrypto)鏈接到系統ssl和加密庫。這些在emscripten中不可用。您需要這些庫的源代碼並使用emscripten編譯它們才能成功運行您的代碼。 @jww這不是重複的,而是emscripten如何工作的誤解 –

+0

@Tarun - 謝謝,重新打開。對於那個很抱歉。爲了記錄,'emcc SHA256.c -lssl -lcrypto ...'看起來不對。當使用傳統的編譯器時,等式是'emcc SHA256.c ... -lssl -lcrypto'。圖書館走到最後,而不是第一個。這是因爲'ld'是一個單通道鏈接器。 – jww

回答

0

用emscripten編譯兩個庫:crypto和openssl,而不是使用系統版本。

使用這些庫生成的.bc文件作爲em ++命令的鏈接器參數,它應該可以工作。