| ... | @@ -9914,7 +9914,6 @@ lib.addCSourceFile("src/lib.c", &[_][]const u8{ | ... | @@ -9914,7 +9914,6 @@ lib.addCSourceFile("src/lib.c", &[_][]const u8{ |
| 9914 | </ul> | 9914 | </ul> |
| 9915 | {#see_also|Primitive Types#} | 9915 | {#see_also|Primitive Types#} |
| 9916 | {#header_close#} | 9916 | {#header_close#} |
| 9917 | | | |
| 9918 | {#header_open|Import from C Header File#} | 9917 | {#header_open|Import from C Header File#} |
| 9919 | <p> | 9918 | <p> |
| 9920 | The {#syntax#}@cImport{#endsyntax#} builtin function can be used | 9919 | The {#syntax#}@cImport{#endsyntax#} builtin function can be used |
| ... | @@ -9954,6 +9953,165 @@ const c = @cImport({ | ... | @@ -9954,6 +9953,165 @@ const c = @cImport({ |
| 9954 | {#see_also|@cImport|@cInclude|@cDefine|@cUndef|@import#} | 9953 | {#see_also|@cImport|@cInclude|@cDefine|@cUndef|@import#} |
| 9955 | {#header_close#} | 9954 | {#header_close#} |
| 9956 | | 9955 | |
| | 9956 | {#header_open|C Translation CLI#} |
| | 9957 | Zig's C translation capability is available as a CLI tool via <code class="shell">zig translate-c</code>. |
| | 9958 | It requires a single filename as an argument. It may also take a set of optional flags that are |
| | 9959 | forwarded to clang. It writes the translated file to stdout. |
| | 9960 | {#header_open|Command line flags#} |
| | 9961 | <ul> |
| | 9962 | <li> |
| | 9963 | <code class="shell">-I</code>: |
| | 9964 | Specify a search directory for include files. May be used multiple times. Equivalent to |
| | 9965 | <a href="https://releases.llvm.org/12.0.0/tools/clang/docs/ClangCommandLineReference.html#cmdoption-clang-i-dir"> |
| | 9966 | clang's <code>-I</code> flag</a>. The current directory is <em>not</em> included by default; |
| | 9967 | use <code>-I.</code> to include it. |
| | 9968 | </li> |
| | 9969 | <li> |
| | 9970 | <code class="shell">-D</code>: Define a preprocessor macro. Equivalent to |
| | 9971 | <a href="https://releases.llvm.org/12.0.0/tools/clang/docs/ClangCommandLineReference.html#cmdoption-clang-d-macro"> |
| | 9972 | clang's <code>-D</code> flag</a>. |
| | 9973 | </li> |
| | 9974 | <li> |
| | 9975 | <code class="shell">-cflags [flags] --</code>: Pass arbitrary additional |
| | 9976 | <a href="https://releases.llvm.org/12.0.0/tools/clang/docs/ClangCommandLineReference.html">command line |
| | 9977 | flags</a> to clang. Note: the list of flags must end with <code>--</code> |
| | 9978 | </li> |
| | 9979 | <li> |
| | 9980 | <code class="shell">-target</code>: The {#link|target triple|Targets#} for the translated Zig code. |
| | 9981 | If no target is specified, the current host target will be used. |
| | 9982 | </li> |
| | 9983 | </ul> |
| | 9984 | {#header_close#} |
| | 9985 | {#header_open|Using -target and -cflags#} |
| | 9986 | <p> |
| | 9987 | <strong>Important!</strong> When translating C code with <code class="shell">zig translate-c</code>, |
| | 9988 | you <strong>must</strong> use the same <code>-target</code> triple that you will use when compiling |
| | 9989 | the translated code. In addition, you <strong>must</strong> ensure that the <code>-cflags</code> used, |
| | 9990 | if any, match the cflags used by code on the target system. Using the incorrect <code>-target</code> |
| | 9991 | or <code>-cflags</code> could result in clang or Zig parse failures, or subtle ABI incompatibilities |
| | 9992 | when linking with C code. |
| | 9993 | </p> |
| | 9994 | <p class="file">varytarget.h</p> |
| | 9995 | <pre><code class="c">long FOO = __LONG_MAX__;</code></pre> |
| | 9996 | <pre><code class="shell">$ zig translate-c -target <strong>thumb-freestanding-gnueabihf</strong> varytarget.h|grep FOO |
| | 9997 | pub export var FOO: c_long = <strong>2147483647</strong>;</code></pre> |
| | 9998 | <pre><code class="shell">$ zig translate-c -target <strong>x86_64-macos-gnu</strong> varytarget.h|grep FOO |
| | 9999 | pub export var FOO: c_long = <strong>9223372036854775807</strong>;</code></pre> |
| | 10000 | <p class="file">varycflags.h</p> |
| | 10001 | <pre><code class="c">enum FOO { BAR }; |
| | 10002 | int do_something(enum FOO foo);</code></pre> |
| | 10003 | <pre><code class="shell">$ zig translate-c varycflags.h|grep -B1 do_something |
| | 10004 | pub const enum_FOO = <strong>c_uint</strong>; |
| | 10005 | pub extern fn do_something(foo: enum_FOO) c_int;</code></pre> |
| | 10006 | <pre><code class="shell">$ zig translate-c <strong>-cflags -fshort-enums --</strong> varycflags.h|grep -B1 do_something |
| | 10007 | pub const enum_FOO = <strong>u8</strong>; |
| | 10008 | pub extern fn do_something(foo: enum_FOO) c_int;</code></pre> |
| | 10009 | {#header_close#} |
| | 10010 | {#header_open|@cImport vs translate-c#} |
| | 10011 | <p>{#syntax#}@cImport{#endsyntax#} and <code class="shell">zig translate-c</code> use the same underlying |
| | 10012 | C translation functionality, so on a technical level they are equivalent. In practice, |
| | 10013 | {#syntax#}@cImport{#endsyntax#} is useful as a way to quickly and easily access numeric constants, typedefs, |
| | 10014 | and record types without needing any extra setup. If you need to pass {#link|cflags|Using -target and -cflags#} |
| | 10015 | to clang, or if you would like to edit the translated code, it is recommended to use |
| | 10016 | <code class="shell">zig translate-c</code> and save the results to a file. Common reasons for editing |
| | 10017 | the generated code include: changing {#syntax#}anytype{#endsyntax#} parameters in function-like macros to more |
| | 10018 | specific types; changing {#syntax#}[*c]T{#endsyntax#} pointers to {#syntax#}[*]T{#endsyntax#} or |
| | 10019 | {#syntax#}*T{#endsyntax#} pointers for improved type safety; and |
| | 10020 | {#link|enabling or disabling runtime safety|@setRuntimeSafety#} within specific functions. |
| | 10021 | </p> |
| | 10022 | {#header_close#} |
| | 10023 | {#see_also|Targets|C Type Primitives|Pointers|C Pointers|Import from C Header File|@cInclude|@cImport|@setRuntimeSafety#} |
| | 10024 | {#header_close#} |
| | 10025 | {#header_open|C Translation Caching#} |
| | 10026 | <p> |
| | 10027 | The C translation feature (whether used via <code class="shell">zig translate-c</code> or |
| | 10028 | {#syntax#}@cImport{#endsyntax#}) integrates with the Zig caching system. Subsequent runs with |
| | 10029 | the same source file, target, and cflags will use the cache instead of repeatedly translating |
| | 10030 | the same code. |
| | 10031 | </p> |
| | 10032 | <p> |
| | 10033 | To see where the cached files are stored when compiling code that uses {#syntax#}@cImport{#endsyntax#}, |
| | 10034 | use the <code class="shell">--verbose-cimport</code> flag: |
| | 10035 | </p> |
| | 10036 | {#code_begin|exe|verbose#} |
| | 10037 | {#link_libc#} |
| | 10038 | {#code_verbose_cimport#} |
| | 10039 | const c = @cImport({ |
| | 10040 | @cDefine("_NO_CRT_STDIO_INLINE", "1"); |
| | 10041 | @cInclude("stdio.h"); |
| | 10042 | }); |
| | 10043 | pub fn main() void { |
| | 10044 | _ = c; |
| | 10045 | } |
| | 10046 | {#code_end#} |
| | 10047 | <p> |
| | 10048 | <code class="shell">cimport.h</code> contains the file to translate (constructed from calls to |
| | 10049 | {#syntax#}@cInclude{#endsyntax#}, {#syntax#}@cDefine{#endsyntax#}, and {#syntax#}@cUndef{#endsyntax#}), |
| | 10050 | <code class="shell">cimport.h.d</code> is the list of file dependencies, and |
| | 10051 | <code class="shell">cimport.zig</code> contains the translated output. |
| | 10052 | </p> |
| | 10053 | {#see_also|Import from C Header File|C Translation CLI|@cInclude|@cImport#} |
| | 10054 | {#header_close#} |
| | 10055 | {#header_open|Translation failures#} |
| | 10056 | <p> |
| | 10057 | Some C constructs cannot be translated to Zig - for example, <em>goto</em>, |
| | 10058 | structs with bitfields, and token-pasting macros. Zig employs <em>demotion</em> to allow translation |
| | 10059 | to continue in the face of non-translateable entities. |
| | 10060 | </p> |
| | 10061 | <p> |
| | 10062 | Demotion comes in three varieties - {#link|opaque#}, <em>extern</em>, and |
| | 10063 | {#syntax#}@compileError{#endsyntax#}. |
| | 10064 | |
| | 10065 | C structs and unions that cannot be translated correctly will be translated as {#syntax#}opaque{}{#endsyntax#}. |
| | 10066 | Functions that contain opaque types or code constructs that cannot be translated will be demoted |
| | 10067 | to {#syntax#}extern{#endsyntax#} declarations. |
| | 10068 | |
| | 10069 | Thus, non-translateable types can still be used as pointers, and non-translateable functions |
| | 10070 | can be called so long as the linker is aware of the compiled function. |
| | 10071 | </p> |
| | 10072 | <p> |
| | 10073 | {#syntax#}@compileError{#endsyntax#} is used when top-level definitions (global variables, |
| | 10074 | function prototypes, macros) cannot be translated or demoted. Since Zig uses lazy analysis for |
| | 10075 | top-level declarations, untranslateable entities will not cause a compile error in your code unless |
| | 10076 | you actually use them. |
| | 10077 | </p> |
| | 10078 | {#see_also|opaque|extern|@compileError#} |
| | 10079 | {#header_close#} |
| | 10080 | {#header_open|C Macros#} |
| | 10081 | <p> |
| | 10082 | C Translation makes a best-effort attempt to translate function-like macros into equivalent |
| | 10083 | Zig functions. Since C macros operate at the level of lexical tokens, not all C macros |
| | 10084 | can be translated to Zig. Macros that cannot be translated will be be demoted to |
| | 10085 | {#syntax#}@compileError{#endsyntax#}. Note that C code which <em>uses</em> macros will be |
| | 10086 | translated without any additional issues (since Zig operates on the pre-processed source |
| | 10087 | with macros expanded). It is merely the macros themselves which may not be translateable to |
| | 10088 | Zig. |
| | 10089 | </p> |
| | 10090 | <p>Consider the following example:</p> |
| | 10091 | <p class="file">macro.c</p> |
| | 10092 | <pre><code class="c">#define MAKELOCAL(NAME, INIT) int NAME = INIT |
| | 10093 | int foo(void) { |
| | 10094 | MAKELOCAL(a, 1); |
| | 10095 | MAKELOCAL(b, 2); |
| | 10096 | return a + b; |
| | 10097 | }</code></pre> |
| | 10098 | <pre><code class="shell">$ zig translate-c macro.c > macro.zig |
| | 10099 | </code></pre> |
| | 10100 | <p class="file">macro.zig</p> |
| | 10101 | <pre>{#syntax#}pub export fn foo() c_int { |
| | 10102 | var a: c_int = 1; |
| | 10103 | var b: c_int = 2; |
| | 10104 | return a + b; |
| | 10105 | } |
| | 10106 | pub const MAKELOCAL = @compileError("unable to translate C expr: unexpected token .Equal"); // macro.c:1:9{#endsyntax#}</pre> |
| | 10107 | <p>Note that {#syntax#}foo{#endsyntax#} was translated correctly despite using a non-translateable |
| | 10108 | macro. {#syntax#}MAKELOCAL{#endsyntax#} was demoted to {#syntax#}@compileError{#endsyntax#} since |
| | 10109 | it cannot be expressed as a Zig function; this simply means that you cannot directly use |
| | 10110 | {#syntax#}MAKELOCAL{#endsyntax#} from Zig. |
| | 10111 | </p> |
| | 10112 | {#see_also|@compileError#} |
| | 10113 | {#header_close#} |
| | 10114 | |
| 9957 | {#header_open|C Pointers#} | 10115 | {#header_open|C Pointers#} |
| 9958 | <p> | 10116 | <p> |
| 9959 | This type is to be avoided whenever possible. The only valid reason for using a C pointer is in | 10117 | This type is to be avoided whenever possible. The only valid reason for using a C pointer is in |