Group Group Group Group Group Group Group Group Group

Bool in a header file

Hi there!
Is there any straightforward way of having a boolean in a header file? I’m sharing a struct on both sides (CPU and GPU) and I would like to put it inside a boolean value. I tried importing Foundation.h and using bool but the compiler complains unsupported architecture
Any ideas?
I know that I can use an int = 0/1 but…

My C isn’t great, but I don’t believe that bool is supported.

You could create an enum, being careful not to conflict with other names.

eg

typedef enum {
  False, True
} boolean;

If anyone is better with C than I am, please join in :smiley:

1 Like

Thks! enum is a more elegant solution!!!

I was trying this out and was surprised to see true was builtin to Metal. I tried to add #include <metal_stdlib> to Common.h to use Metal’s builtin bool but it failed. I’m guessing it’s only on the GPU? I also tried Metal/Metal.h but that was not supported on my architecture.

Is there really no way to use the Metal bool in Swift or common headers? The Metal Shading Language specifies that the bool just expands to 0 and 1, but still…

I did some exploring. I think this works too –

To Common.h add –
typedef signed char BOOL;
#define TRUE ((BOOL)1)
#define FALSE ((BOOL)0)
then declare your value in the struct as –
BOOL value

This creates an Obj-C Bool value. You can use Swift’s true and false in Swift and Metal’s true and false in Metal.

Found in this project: GitHub - MetalPetal/MetalPetal: A GPU accelerated image and video processing framework built on Metal.

2 Likes

Cool! Thank you very much!!!

1 Like