Can't link OpenSSL code

前端 未结 2 1501
轻奢々
轻奢々 2020-12-11 05:52

I am trying to build an openssl simple program. Here is the complete code:

#include 
#include 
#include 
#incl         


        
相关标签:
2条回答
  • 2020-12-11 06:35

    For those of you who have this same problem but are using Windows, Mingw and this OpenSSL for Windows (at this time: Win32 OpenSSL v1.0.2a). You need to link to libeay32.a that is located in C:\OpenSSL-Win32\lib\MinGW\ (after installing OpenSSL).

    In my case I am using CMake and the powerful CLion IDE, so I had to rename the library to libeay32.dll.a because CMake wasn't locating the library. This is my CMakeLists.txt:

    cmake_minimum_required(VERSION 3.1)
    project(openssl_1_0_2a)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    include_directories(C:\\OpenSSL-Win32\\include)
    
    set(SOURCE_FILES main.cpp)
    
    link_directories(C:\\OpenSSL-Win32\\lib\\MinGW)
    
    add_executable(openssl_1_0_2a ${SOURCE_FILES})
    
    target_link_libraries(openssl_1_0_2a eay32)
    

    I made the test with this example (which is borrowed from this answer):

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "openssl/aes.h"
    
    int main(int argc, char* argv[])
    {
        AES_KEY aesKey_;
        unsigned char userKey_[16];
        unsigned char in_[16] = {0};
        unsigned char out_[16] = {0};
        strcpy((char *) userKey_,"0123456789123456");
        strcpy((char *) in_,"0123456789123456");
    
        fprintf(stdout,"Original message: %s\n", in_);
        AES_set_encrypt_key(userKey_, 128, &aesKey_);
        AES_encrypt(in_, out_, &aesKey_);
    
        AES_set_decrypt_key(userKey_, 128, &aesKey_);
        AES_decrypt(out_, in_,&aesKey_);
        fprintf(stdout,"Recovered Original message: %s XXX \n", in_);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-11 06:41

    You may be trying to statically link, but the -L option and -lcrypto are looking for a file to link with dynamically. To statically link to a specific library, just specify your .a file on the compiler command line after all your source files.

    E.g.,

    gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a
    
    0 讨论(0)
提交回复
热议问题