4

My project requires OpenSSL libraries crypto and ssl. After updating Xcode to 8.2.1, compiling my project fails.

In my project Build Settings, in Linking, I have -lssl -lcrypto -lxml2 that indicates to load those libraries. Going back to my app Build Settings, I add /usr/local/opt/openssl/lib/ in the Library Search Paths to tell Xcode where to look.

Hurray, it compiles, builds and run properly. Cool.

BUT

When launching the app on a different mac, the app crashes, with this linking error:

Dyld Error Message:
  Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
  Referenced from: /Applications/MyApp.app/Contents/MacOS/MyApp
  Reason: image not found

That means the app is trying to load the open ssl libraries that might not be present on that mac. There might be just an older version, or that folder might not exist at all? So that's definitely a no-solution.

Here lies my issue, I have no idea how to solve this problem.

Removing -lssl -lcrypto from the linking doesn't help, as the has errors during linking.

I guess I need a way to tell Xcode to look there when compiling only. Would that be possible?

James Risner
  • 5,451
  • 11
  • 25
  • 47
Link14
  • 896
  • 1
  • 15
  • 33
  • Since you mentioned Brew: [Homebrew refusing to link OpenSSL](http://stackoverflow.com/q/38670295), [Update OpenSSL on OS X with Homebrew](http://stackoverflow.com/q/15185661), [How to install latest version of openssl Mac OS X El Capitan](http://stackoverflow.com/q/35129977), [How to upgrade OpenSSL in OS X?](http://apple.stackexchange.com/q/126830), [Openssl installation using HomeBrew fails](http://superuser.com/q/486389), etc. – jww Apr 01 '17 at 00:24
  • Perhaps you should include your updated `libssl.dylib` and `libcrypto.dylib` with your app bundle. It seems like it will address the *Reason: image not found*. Also see [Using Dylibs Inside App Bundle](http://stackoverflow.com/q/20695805/608639) and [Including dylib in application bundle](https://forums.macrumors.com/threads/including-dylib-in-application-bundle-carbon.609963/). – jww Apr 01 '17 at 00:30
  • @jww I can add the `dylib`s in my app bundle, but the main problem remains. When compiling, I need to specify the path to openssl in the Library Search Paths `/usr/local/opt/openssl/lib` so compiling succeeds. But when launching the app, it searches for the `dylib` it found in `/usr/local/opt/openssl/lib`, which is `/usr/local/opt/openssl/lib/libssl.1.0.0.dylib` (I guess it just searches for the `dylib` it found at compile time). How can I tell the app to load the `dylib` that's embedded instead of looking for the one in `/usr/local/opt/openssl/lib/` ? Is that even possible ? – Link14 Apr 10 '17 at 22:40
  • @jww another weird thing. I found another `libssl.dylib` and `libcrypto.dylib` in `/usr/lib`. But when I add that path to the Library Search Paths, the compile command just throws it away, it doesn't appear/it's not used. – Link14 Apr 10 '17 at 22:45
  • *"I can add the dylibs in my app bundle, but the main problem remains"* - It seems like you need to run `install_name_tool` to unset the hard coded path of `/usr/local/opt/openssl/lib`. Maybe you should make it relative to the app. I think you can do that in a pre-Build script or at the pre-Build stage. – jww Jun 13 '17 at 20:33
  • 1
    @JamesRisner The question was posted 5 years and unfortunately I don't have access to that project anymore and can't test your solution. – Link14 Oct 19 '22 at 23:05

1 Answers1

0

If you don't wish to ship libraries and deal with making sure the linker can find them, you can link your code against a static library.

Using the code in this question as a linking example, I compiled with this command:

cc -I/opt/homebrew/Cellar/openssl@1.1/1.1.1q/include -o Link14 Link14.cpp /opt/homebrew/Cellar/openssl@1.1/1.1.1q/lib/libcrypto.a -lc++

This uses system libraries for most things, but the static version of libcrypto in libcrypto.a for the encryption. You can add multiple by placing them on the compile/link line.

James Risner
  • 5,451
  • 11
  • 25
  • 47