# Bool in a header file

**URL:** <https://forums.kodeco.com/t/bool-in-a-header-file/132442>\
**Category:** Metal by Tutorials\
**Created:** [February 11, 2021, 8:39am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442 "2021-02-11T08:39:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![quaternionboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/quaternionboy/32/110937_2.png) [@quaternionboy](https://forums.kodeco.com/u/quaternionboy)\
**Post date:** [February 11, 2021, 8:39am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/1 "2021-02-11T08:39:24Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [February 11, 2021, 10:59am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/2 "2021-02-11T10:59:15Z")

</div>

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

```auto
typedef enum {
  False, True
} boolean;

```

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

---

<div class="post-metadata">

**Author:** ![quaternionboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/quaternionboy/32/110937_2.png) [@quaternionboy](https://forums.kodeco.com/u/quaternionboy)\
**Post date:** [February 11, 2021, 4:52pm UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/3 "2021-02-11T16:52:10Z")

</div>

Thks! `enum` is a more elegant solution!!!

---

<div class="post-metadata">

**Author:** ![killertv](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/killertv/32/81494_2.png) [@killertv](https://forums.kodeco.com/u/killertv)\
**Post date:** [February 12, 2021, 4:55pm UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/4 "2021-02-12T16:55:47Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![killertv](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/killertv/32/81494_2.png) [@killertv](https://forums.kodeco.com/u/killertv)\
**Post date:** [February 12, 2021, 6:12pm UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/5 "2021-02-12T18:12:03Z")

</div>

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.](https://github.com/MetalPetal/MetalPetal/)

---

<div class="post-metadata">

**Author:** ![quaternionboy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/quaternionboy/32/110937_2.png) [@quaternionboy](https://forums.kodeco.com/u/quaternionboy)\
**Post date:** [February 15, 2021, 7:57am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/6 "2021-02-15T07:57:14Z")

</div>

Cool! Thank you very much!!!

---

<div class="post-metadata">

**Author:** ![lducot2](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lducot2/32/102075_2.png) [@lducot2](https://forums.kodeco.com/u/lducot2)\
**Post date:** [March 4, 2021, 1:48am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/7 "2021-03-04T01:48:19Z")

</div>

Just to add — the standard metal project has a few nice macros that you can use for inspiration. They give you this one and I think one or two others:

`#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type`

I always have read macros are evil but seems critical to integrating swift/metal like killer shows. Personally, I had a situation that was driving me insane because metal pointers require an address space qualifier. A macro did the trick to avoid duplicating the struct.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [January 20, 2022, 4:44am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/8 "2022-01-20T04:44:52Z")

</div>

Revisiting this as this seems to work and keeps copyright.

**stdbool.h** :  
[https://opensource.apple.com/source/xnu/xnu-1228/EXTERNAL\_HEADERS/stdbool.h.auto.html](https://opensource.apple.com/source/xnu/xnu-1228/EXTERNAL_HEADERS/stdbool.h.auto.html)

You can include this file in your project and then in **Common.h** do for example:

```auto
#import "stdbool.h"

typedef struct {
  uint width;
  uint height;
  bool alphaTesting;
  bool scissorTesting;
} Params;

```

---

<div class="post-metadata">

**Author:** ![tchelyzt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tchelyzt/32/111572_2.png) [@tchelyzt](https://forums.kodeco.com/u/tchelyzt)\
**Post date:** [January 12, 2024, 11:32pm UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/9 "2024-01-12T23:32:24Z")

</div>

I’ve come upon the same problem. The MSL spec clearly states in table 2.1 that _ **bool** _ is supported and I can send a buffer of _ **bool** _s to the shader, so the bridge is clearly the problem. I imaging bridging is for Obj-C and not for C++, it just usually works, but not for _ **bool** _.

This is surely a major issue for Metal. Is Apple not fixing it? Like the absence of a random number generator as I describe elsewhere.

The _stdbool.h_ solution above works but the problem for me is that I don’t understand what it is doing. What are int and \_Bool unto in this code?

Can anyone explain in simple language what the stdbool.h solution does?

Thanks

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [January 13, 2024, 1:47am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/10 "2024-01-13T01:47:09Z")

</div>

My C is not great, so when I wanted a `bool`, I fumbled about a bit to see how Apple did it. I came across the _stdbool.h_ solution, probably in an old Apple sample code somewhere that I can’t find now.

Here’s a bit of history and explanation behind this:

> <https://stackoverflow.com/questions/74582472/what-does-stdbool-h-do/74582925#74582925>

I probably should have poked around and played with the code a bit more, but I wasn’t sure whether I would break things in future versions. Or maybe that was just how things worked when I first wrote the code ages ago.

You can replace _stdbool.h_ with this code at the top of **Common.h**.

```auto
#ifndef __cplusplus
#define bool _Bool
#endif

```

That SO link explains what `_Bool` is. I don’t know what the compiler is doing in the bridging header (maybe there’s someone out there that can fill us in?), but if you don’t have the `ifndef` to make sure that you’re only compiling C, then it won’t compile. I guess because `bool` is now part of C++, but in C, `_Bool` is the keyword.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [January 13, 2024, 2:58am UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/11 "2024-01-13T02:58:30Z")

</div>

Serendipitously, this article dropped in my feed today:

> **[Is Objective-C BOOL
a boolean type? It depends](https://www.jviotti.com/2024/01/05/is-objective-c-bool-a-boolean-type-it-depends.html?utm_campaign=iOS%2BDev%2BWeekly&utm_medium=email&utm_source=iOS%2BDev%2BWeekly%2BIssue%2B643)**
>
> This article describes how
> the Objective-C BOOL type is differently defined on Apple
> platforms, and how these differences are defined in LLVM

It’s over my head, I’m afraid.

---

<div class="post-metadata">

**Author:** ![tchelyzt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tchelyzt/32/111572_2.png) [@tchelyzt](https://forums.kodeco.com/u/tchelyzt)\
**Post date:** [January 13, 2024, 6:13pm UTC](https://forums.kodeco.com/t/bool-in-a-header-file/132442/12 "2024-01-13T18:13:38Z")

</div>

Thank you. That’s so much simpler.
