17

我有一个使用 libssl 的简单 C 程序。

在 Linux 上,我安装了 openssl-dev 包并使用以下行编译程序:

gcc test_libssl.c -o test_libssl -lcrypto -lssl

现在我想在我的 Mac 上做同样的事情。同一行导致:

fatal error: 'openssl/conf.h' file not found

我尝试通过 home-brew 安装 openssl(openssl-dev 不起作用)brew install openssl

这给了我:

...
==> 安装 openssl
==> 下载https://www.openssl.org/source/openssl-1.0.2a.tar.gz curl: (22) The requested URL returned error: 404 Not Found

我发现了一个没有答案的相关 SO 问题

我也试过

brew info openssl

并获悉

此公式仅适用于小桶。Mac OS X 已经提供了这个软件,并且并行安装另一个版本可能会导致各种麻烦。

Apple 已弃用 OpenSSL,转而使用自己的 TLS 和加密库

为了能够在 OS X 上使用 C 程序编译 libssl,我需要做什么/安装什么?

或者,这是一个坏主意(鉴于上面的警告)?




更新:

我使用 brew 安装了 openssl。我不确定这是否是问题,但我更新了 brew。接受 brew 的建议

您可能应该将 /usr/local 的所有权和权限更改回您的用户帐户。 sudo chown -R $(whoami):admin /usr/local

并将这个问题考虑在内。

然后,按照@Alex Reynolds 的建议,我成功编译了它

gcc test_libssl.c -o test_libssl -lssl -lcrypto -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include
4

2 回答 2

11

我在 El Capitan (10.11.1) 上安装了 Homebrew,并安装了当前版本的 OpenSSL,没有明显的不良影响:

$ uname -a
Darwin hostname.local 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64

$ brew info openssl
openssl: stable 1.0.2d (bottled)
OpenSSL SSL/TLS cryptography library
https://openssl.org/

This formula is keg-only.
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

/usr/local/Cellar/openssl/1.0.2d_1 (464 files, 17M)
  Built from source
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/openssl.rb
==> Dependencies
Build: makedepend ✔
==> Options
--universal
    Build a universal binary
--without-check
    Skip build-time tests (not recommended)
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

您是否尝试将它建议的标志添加到您的应用程序的构建语句中?您可以编辑您的应用程序makefile或其他构建语句,并在brew install openssl. 这可以帮助您的编译器找到并链接它需要的库和头文件。

看起来一切都在那里。以下是标题:

$ ls -al /usr/local/opt/openssl/include/openssl/
total 3688
drwxr-xr-x  77 alexpreynolds  admin    2618 Aug 24 13:46 .
drwxr-xr-x   3 alexpreynolds  admin     102 Aug 24 13:46 ..
-rw-r--r--   1 alexpreynolds  admin    6182 Aug 24 13:46 aes.h
-rw-r--r--   1 alexpreynolds  admin   63142 Aug 24 13:46 asn1.h
-rw-r--r--   1 alexpreynolds  admin   24435 Aug 24 13:46 asn1_mac.h
-rw-r--r--   1 alexpreynolds  admin   34475 Aug 24 13:46 asn1t.h
-rw-r--r--   1 alexpreynolds  admin   38566 Aug 24 13:46 bio.h
-rw-r--r--   1 alexpreynolds  admin    5351 Aug 24 13:46 blowfish.h
...

以及静态和动态库:

$ ls -al /usr/local/opt/openssl/lib
total 11664
drwxr-xr-x  10 alexpreynolds  admin      340 Aug 24 13:46 .
drwxr-xr-x  11 alexpreynolds  admin      374 Aug 24 13:46 ..
drwxr-xr-x  14 alexpreynolds  admin      476 Aug 24 13:46 engines
-r--r--r--   1 alexpreynolds  admin  1861780 Aug 24 13:46 libcrypto.1.0.0.dylib
-r--r--r--   1 alexpreynolds  admin  3206344 Aug 24 13:46 libcrypto.a
lrwxr-xr-x   1 alexpreynolds  admin       21 Aug 24 13:46 libcrypto.dylib -> libcrypto.1.0.0.dylib
-r--r--r--   1 alexpreynolds  admin   364144 Aug 24 13:46 libssl.1.0.0.dylib
-r--r--r--   1 alexpreynolds  admin   524424 Aug 24 13:46 libssl.a
lrwxr-xr-x   1 alexpreynolds  admin       18 Aug 24 13:46 libssl.dylib -> libssl.1.0.0.dylib
drwxr-xr-x   5 alexpreynolds  admin      170 Aug 24 13:46 pkgconfig
于 2015-11-04T20:17:37.717 回答
1

@Alex Reynolds 的回答是正确的,但是如果你想编译/配置别人的程序,那么你可以预先运行它:

export LDFLAGS=-L/usr/local/opt/openssl/lib
export CPPFLAGS=-I/usr/local/opt/openssl/include
于 2018-04-06T08:53:17.510 回答