Comments (6)
More minimal foo.h
:
#define bool _Bool
#define true 1
#define false 0
The last 3 lines of zig translate-c foo.h
:
pub const @"bool" = bool;
pub const @"true" = @as(c_int, 1);
pub const @"false" = @as(c_int, 0);
from zig.
I have a pretty janky workaround:
@cImport({
@cDefine("true", "(_Bool)1");
@cDefine("false", "(_Bool)0");
@cInclude("stdbool.h");
@cUndef("true");
@cUndef("false");
@cDefine("true", "(_Bool)1");
@cDefine("false", "(_Bool)0");
// other @cInclude()s that use stdbool.h...
});
A couple of points of explanation:
- We must use
(_Bool)1
, not(bool)1
. The latter confuses zig and it just doesn't understand what the 1 is doing there - We must pre-define as well as post-define
true
andfalse
. If we only define it before,stdbool.h
will override, and if we only define it after, zig will use the first definition (fromstdbool.h
) to generate its own idea of whattrue
andfalse
are. - We could also just replicate
stdbool.h
but with altered definitions oftrue
andfalse
if we were so inclined, it's not that long of a file.
from zig.
Thanks for the recatogrization, I wasn't sure if it was a bug or proposal.
I don't know how the tags work, so maybe this is correct anyways, but I didn't invoke zig translate-c
at any point, this is just from @cImport()
and @cInclude()
from zig.
I've uncovered even weirder behaviour. In stdbool.h
, we have bool
defined by #define bool _Bool
, so I figured if I overrode this by putting
@cInclude("stdbool.h");
@cUndef("bool");
@cDefine("bool", "int");
To the top of the @cImport()
statement i would force zig to define @"bool" = c_int
so that the example works,
It doesn't work. Still defines @"bool" = bool
. However, wherever bool
is used as part of a struct, it now uses c_int
.
Totally unsure what's going on now. Is mapping C bool
to zig bool
hard-coded? Is re-#define
ing things not supported?
from zig.
Okay, it seems indeed that this is a general thing. any time you have
const c = @cImport({
@cDefine("foo", "1");
@cUndef("foo");
@CDefine("foo", "2");
});
It's going to correctly apply the second #define
in any C code @cInclude
d later on (replacing foo
with 2
), but if you access that as a variable from zig, it will go with the first definition (c.foo == 1
).
I can't tell how much of this is intentional design vs. bugs. #define
s don't actually have the same semantics as const
declarations, but my intuition is that whatever the last #define
declaration is, that one should determine how it shows up in the zig code, since that's how it would show up in C code with the same set of #include
s and #define
s
from zig.
It generates
pub const @"true" = @import("std").zig.c_translation.cast(bool, @as(c_int, 1));
pub const @"false" = @import("std").zig.c_translation.cast(bool, @as(c_int, 0));
Which is kind of goofy, but does work.
from zig.
Related Issues (20)
- `error: TODO: implement @ptrCast between slices changing the length` HOT 1
- Compiler allows initialisation of enum types with invalid enum Values HOT 1
- Runtime safety check added in comptime block HOT 2
- Cannot `@cImport` `windows.h` when targetting `msvc` abi HOT 6
- Errdefer payload type doesn't match function signature
- `callconv(.Naked)` functions with parameters cause silent LLVM crash when compiling for PowerPC
- default local cache directory to `.zig-cache` instead of `zig-cache` HOT 4
- std.debug.bufPrintZ padding on the wrong side of output
- observed error.Unexpected NTSTATUS=0xc0000056 (DELETE_PENDING) in makeOpenDirAccessMaskW
- add `math.approxEqUlp` for numerical precision testing
- Missing error for runtime `@ptrFromInt` to comptime-only type HOT 2
- ReleaseSafe panic error trace points at line 0 when freeing an undefined pointer HOT 4
- Zig does not report errors or sliently fails on `.S` / `assembler_with_cpp` files
- Can't link on PPC64LE HOT 1
- ErrorUnion typeInfo debug print causes GenericPoison HOT 3
- Support `wasm32-emscripten` libc HOT 1
- Unsure how to handle duplicate symbol linker error
- SIGTRAP When attempting to write to a runtime index in comptime variable HOT 1
- Undefined struct fields are initialized to 0 instead of 0xAA in debug mode (with llvm backend)
- zig fetch -h should include examples
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zig.