Xcode architecture independent LDFLAGS arm64 x86 Apple Silicon M1

I have a project which comes in project.pbxproj with this

OTHER_LDFLAGS = (
    "-lgit2",
    "-force_load",
    External/libgit2.a,
    /usr/local/lib/libssh2.a,  <-- The problem
    "-lcrypto",
    "-lssl",
    "-lcurl",
);

but on a Apple Silicon M1 homebrew comes with

/opt/homebrew/lib/libssh2.a instead

/usr/local/lib/libssh2.a like in x86

I already tried to add both, but with less success

OTHER_LDFLAGS = (
    "-lgit2",
    "-force_load",
    External/libgit2.a,
    /usr/local/lib/libssh2.a,
    /opt/homebrew/lib/libssh2.a,
    "-lcrypto",
    "-lssl",
    "-lcurl",
);

How to make it work on x86 and on arm64 without alternate code changes here ?

e.g. a variable which is set during a pre-build script, but how ?

You usually do this sort of thing using a per-architecture conditional build setting. For example, consider this setup below:

And a command-line tool like this:

func main() {
    #if INTEL
        print("QQQ INTEL")
    #endif
    #if APPLE_SILICON
        print("QQQ APPLE_SILICON")
    #endif
    #if UNKNOWN
        print("QQQ UNKNOWN")
    #endif
}

main()

If I build this universal, I see that the per-architecture build setting has caused different strings to be included in each architecture:

% strings -arch x86_64 xxst | grep QQQ
QQQ INTEL
% strings -arch arm64 xxst | grep QQQ 
QQQ APPLE_SILICON

In your case I think you could get away with setting OTHER_LDFLAGS to use a relative path and then customising the search path with the Library Search Paths build setting (LIBRARY_SEARCH_PATHS). I haven’t tried that though (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you @eskimo it sounds promising. I run into two questions:

A:

I found this setting for Swift only, how to do this with ObjectiveC ? Will this setting valid here in ObjectiveC as well ?

This is the repo https://github.com/gitx/gitx

B:

Where to put this script (sorry, I've no clue about this)

func main() { ...

I found this setting for Swift only, how to do this with Objective-C?

There is a direct Objective-C equivalent (Preprocessor Macros, GCC_PREPROCESSOR_DEFINITIONS) but that’s not what you need. I used Active Conditional Compilations of an example of how you can create architecture-specific build settings. I chose that for my example because it’s easy to see the result in the built tool. However, this build setting doesn’t make sense in your case because you want to affect the linker, not the compiler. Rather, I suggest you investigate varying the Library Search Paths build setting (LIBRARY_SEARCH_PATHS) on a per-architecture basis.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Xcode architecture independent LDFLAGS arm64 x86 Apple Silicon M1
 
 
Q