Program Tip

OpenSSL을 사용하여 .c 파일을 컴파일하는 방법은 무엇입니까?

programtip 2020. 11. 14. 11:00
반응형

OpenSSL을 사용하여 .c 파일을 컴파일하는 방법은 무엇입니까?


다음이 포함 된 작은 .c 파일을 컴파일하려고합니다.

#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/evp.h>

.c 파일이있는 동일한 폴더에 모든 파일 (및 기타)이 포함 된 / openssl이 있으며 시냅스 패키지 관리자에서도 OpenSSL이 설치된 것을 볼 수 있습니다.이 파일로 컴파일하려고합니다.

gcc -o Opentest Opentest.c -lcrypto

하지만 항상 오류가 발생합니다.

error: openssl/ssl.h: No such file or directory
error: openssl/rsa.h: No such file or directory
error: openssl/x509.h: No such file or directory
error: openssl/evp.h: No such file or directory

내가 컴파일하려는 파일은 .c 파일이며 Makefile 또는 ./configure가 없습니다.

나는 이미 시도했다 :

env CFLAGS=-I/path/to/openssl/

다시 컴파일을 시도했지만 동일한 오류가 발생합니다.

openssl include로 컴파일하려면 어떻게해야합니까?


포함 경로는 시스템의 OpenSSL 설치 에 대해 컴파일해야 함을 나타냅니다 . 당신은하지 말았어야 .h패키지 디렉토리에있는 파일을 - 그것은 그들을 데리러해야합니다 /usr/include/openssl.

일반 OpenSSL 패키지 ( libssl)에는 .h파일이 포함되어 있지 않습니다 . 개발 패키지도 설치해야합니다. libssl-devDebian, Ubuntu 및 유사 배포판과 libssl-develCentOS, Fedora, Red Hat 등에서 이름이 지정 됩니다 .


-I플래그를 사용하여 올바르게 gcc 하십시오 .

gcc -I/path/to/openssl/ -o Opentest -lcrypto Opentest.c

-I포함하는 디렉토리를 가리켜 야한다 openssl폴더를.


아래 스 니펫을 인용 된 문제에 대한 솔루션으로 사용하십시오.

yum install openssl
yum install openssl-devel

Keepalived 버전 1.2.7을 사용하여 CentOS 버전 5.4에서 테스트 및 효과가 입증되었습니다.


OpenSSL 헤더가 openssl현재 디렉토리 하위 디렉토리에있는 경우 다음을 사용합니다.

gcc -I. -o Opentest Opentest.c -lcrypto

전처리 기는 옵션 ./openssl/ssl.h의 " ."에서 " " 와 같은 이름을 만들고 -I꺾쇠 괄호로 지정된 이름을 만듭니다. 큰 따옴표 ( #include "openssl/ssl.h")로 이름을 지정했다면 질문 할 필요가 없었을 것입니다. Unix의 컴파일러는 일반적으로 현재 디렉터리에서 큰 따옴표로 묶인 헤더를 자동으로 검색하지만 꺾쇠 괄호 ( #include <openssl/ssl.h>)로 묶인 헤더에 대해서는 검색하지 않습니다 . 구현 정의 동작입니다.

OpenSSL 라이브러리가 어디에 있는지 말하지 않습니다. 적절한 옵션과 인수를 추가하여 ' -L /opt/openssl/lib' 와 같이 지정해야 할 수 있습니다 .


openssl.pc 파일에서

prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: OpenSSL
Description: Secure Sockets Layer and cryptography libraries and tools
Version: 0.9.8g
Requires:
Libs: -L${libdir} -lssl -lcrypto
Libs.private: -ldl -Wl,-Bsymbolic-functions -lz
Cflags: -I${includedir}

You can note the Include directory path and the Libs path from this. Now your prefix for the include files is /home/username/Programming . Hence your include file option should be -I//home/username/Programming.

(Yes i got it from the comments above)

This is just to remove logs regarding the headers. You may as well provide -L<Lib path> option for linking with the -lcrypto library.


You need to include the library path (-L/usr/local/lib/)

gcc -o Opentest Opentest.c -L/usr/local/lib/ -lssl -lcrypto

It works for me.


For this gcc error, you should reference to to the gcc document about Search Path.

In short:

1) If you use angle brackets(<>) with #include, gcc will search header file firstly from system path such as /usr/local/include and /usr/include, etc.

2) The path specified by -Ldir command-line option, will be searched before the default directories.

3)If you use quotation("") with #include as #include "file", the directory containing the current file will be searched firstly.

so, the answer to your question is as following:

1) If you want to use header files in your source code folder, replace <> with "" in #include directive.

2) if you want to use -I command line option, add it to your compile command line.(if set CFLAGS in environment variables, It will not referenced automatically)

3) About package configuration(openssl.pc), I do not think it will be referenced without explicitly declared in build configuration.

참고URL : https://stackoverflow.com/questions/3368683/how-to-compile-c-file-with-openssl-includes

반응형