Linux 无法链接 OpenSSL 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11004314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:48:49  来源:igfitidea点击:

Can't link OpenSSL code

clinuxencryptioncryptographyopenssl

提问by AlexandruC

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

我正在尝试构建一个 openssl 简单程序。这是完整的代码:

#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];
    unsigned char out_[16];
    strcpy(userKey_,"0123456789123456");
    strcpy(in_,"0123456789123456");

    fprintf(stdout,"Original message: %s", 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", in_);      
    return 0;
}

I try to compile it using this command:

我尝试使用以下命令编译它:

gcc -I/home/aleksei/openSSL0.9.8/include -o app -L . -lssl -lcrypto tema1.c

and I get this:

我明白了:

 /tmp/ccT1XMid.o: In function `main':
 tema1.c:(.text+0x8d): undefined reference to `AES_set_encrypt_key'
 tema1.c:(.text+0xa7): undefined reference to `AES_encrypt'
 tema1.c:(.text+0xbf): undefined reference to `AES_set_decrypt_key'
 tema1.c:(.text+0xd9): undefined reference to `AES_decrypt'
 collect2: ld returned 1 exit status

I am under Ubuntu 10.04. How can I get this to work ?

我在 Ubuntu 10.04 下。我怎样才能让它工作?

采纳答案by indiv

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

您可能正在尝试静态链接,但该-L选项-lcrypto正在寻找要动态链接的文件。要静态链接到特定库,只需.a在所有源文件之后在编译器命令行上指定您的文件。

E.g.,

例如,

gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a

回答by lmiguelmh

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.athat is located in C:\OpenSSL-Win32\lib\MinGW\(after installing OpenSSL).

对于那些遇到同样问题但正在使用 Windows、Mingw 和适用于 Windows 的 OpenSSL(目前:Win32 OpenSSL v1.0.2a)的人。您需要链接到libeay32.a位于C:\OpenSSL-Win32\lib\MinGW\(安装 OpenSSL 后)。

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

就我而言,我使用的是 CMake 和强大的 CLion IDE,因此我不得不将库重命名为 ,libeay32.dll.a因为 CMake 没有找到该库。这是我的 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;
}