What definitions exist like __LP64__ and __arm64__ in Cocoa that differentiate platforms at compile time? Where or how are they defined?
Asked Answered
A

3

16

With the introduction of arm64 as a standard architecture for the iphoneos platform it's necessary in some cases to implement compile-time conditions for code that is specific to the 64/32 architecture.

If you look at CoreGraphics/CGBase.h and how some popular open source projects are providing support for arm64 it's clear that you can check for the presence of the 64bit runtime like so:

#if defined(__LP64__) && __LP64__
...
#else
...
#endif

It's also possible to check specifically for arm64 (as opposed to just a 64bit runtime) as mentioned in this fix for erikdoe/ocmock

#ifdef __arm64__
...
#else
....
#endif

Is there a comprehensive list, or documentation for these kinds of definitions? Where or how are they defined?

Apical answered 9/12, 2013 at 2:27 Comment(0)
S
20

Those macros are not specific to Cocoa, they are specific to CLANG, and they can be listed on the command line with:

clang -dM -E -x c /dev/null

Different CLANG versions ship with varying amounts of feature flags which can get turned on and off at configuration time or depending on which platform and OS the compiler is running on. A fairly comprehensive list can be found in their testing headers with variants for each supported system also scattered around in the testing directory. Documentation for each depends on whether the flag is specific to CLANG, or defined in one of the standard libraries it links against (for example __llvm__ is defined by CLANG, but __WCHAR_WIDTH__ is defined by LibC). There is really no comprehensive list with definitive documentation for this reason. Different platforms are allowed to do things slightly differently so long as they adhere to language specifications.

The majority of the interesting public Objective-C macros exist in Foundation near the bottom of Foundation/NSObjCRuntime.h.

Stoltzfus answered 9/12, 2013 at 2:54 Comment(4)
Be warned that many of the compiler's predefined macros are implementation details that may change at any time.Jeffreyjeffreys
@GregParker: They probably won't change at any time, but they certainly might change between compiler versions =)Dialectic
For others visiting in the future, the CLANG call above is only dumping the active properties for the current default compiler settings. If your cross compiling to iOS it's a different set. You can get an idea of how to use xcodebuild to run CLANG for a specific scheme here, and will try to update more details to that or related. #20116975Stanislaw
clang -dM -arch arm64 -E -x c /dev/null will print out the defines for ARM64Heron
C
3

After some years, I landed here while searching and offer this update:

Note that __LP64__ is not a reliable check for the 64-bit runtime on any system that doesn't define __LP64__. This is because LP64 is the name of the data model in use, which is one of several 64-bit data models that might be selected.

Data model is the term used to describe the arrangement of standard C/C++ types by size. In this example, LP64 means that long and pointer are 64-bits, with the implication that everything else is smaller. This is a fairly standard GCC/Clang configuration, but by no means universal (other 64-bit platforms use other standards). Microsoft, I believe, uses LLP64 as their data model, with long long and pointer being 64 bits, while int and long are both allocated 32.

There is even a SILP64 data model - 64-bit shorts, yikes!

If you want to detect 64-bits, you will do better to look at the predefined-macros page, which has been mentioned already. You can detect the architecture and go from there. If you want to know the size of certain types, you will probably be able to determine that information more portably by either (1) checking the result of sizeof(...); or (2) checking the values for e.g., INT_MAX, LONG_MAX, etc.

Courson answered 28/11, 2021 at 5:21 Comment(1)
I appreciate the response after so many years.Apical
G
2

You may find this list useful.

The link points exactly to the list of architecture ifdef's, here you can find links to other lists (for compiler and platform detection).

Guttapercha answered 24/6, 2015 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.