在OS X El Capitan上使用libssl编译C程序?

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

我在 Stack Overflow 上找到一个相关的问题,但没有答案。

我也尝试过其他方法。

brew info openssl

我得知:

这个公式仅适用于桶装软件。 Mac OS X已经提供了这个软件,安装另一个版本可能会引起各种问题。

苹果已经弃用OpenSSL,而是采用自己的TLS和加密库。

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

或者,考虑到上面的警告,这本来就是一个坏主意吗?




更新:

我使用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

它已被弃用,有其原因...您应该在应用程序中使用苹果框架(如libcrypto)创建特定于OSX的代码... - Macmade
1
@Macmade libcrypto是OpenSSL的一部分。没有直接等效的本地库;最接近的等效库(并不是非常接近)是CommonCrypto。 - user149341
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

1
@Alex Reynolds的回答是正确的,但如果你想编译/配置别人的程序,那么你可以先运行以下命令:
export LDFLAGS=-L/usr/local/opt/openssl/lib
export CPPFLAGS=-I/usr/local/opt/openssl/include

是的,对于一个带有./configure步骤的软件包,我还需要执行以下操作:export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"。 - John Fowler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接