docs: update for shared libraries

This commit is contained in:
Andrew Kelley 2019-05-02 11:59:49 -04:00
parent f950ec0c16
commit f8117a0799
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9

View File

@ -8859,20 +8859,21 @@ export fn add(a: i32, b: i32) i32 {
return a + b;
}
{#code_end#}
<p>To make a shared library:</p>
<p>To make a static library:</p>
<pre><code class="shell">$ zig build-lib mathtest.zig
</code></pre>
<p>To make a static library:</p>
<pre><code class="shell">$ zig build-lib mathtest.zig --static
<p>To make a shared library:</p>
<pre><code class="shell">$ zig build-lib mathtest.zig -dynamic
</code></pre>
<p>Here is an example with the {#link|Zig Build System#}:</p>
<p class="file">test.c</p>
<pre><code class="cpp">// This header is generated by zig from mathtest.zig
#include "mathtest.h"
#include &lt;assert.h&gt;
#include &lt;stdio.h&gt;
int main(int argc, char **argv) {
assert(add(42, 1337) == 1379);
int32_t result = add(42, 1337);
printf("%d\n", result);
return 0;
}</code></pre>
<p class="file">build.zig</p>
@ -8882,10 +8883,10 @@ const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));
const exe = b.addCExecutable("test");
exe.addCompileFlags([][]const u8{"-std=c99"});
exe.addSourceFile("test.c");
const exe = b.addExecutable("test", null);
exe.addCSourceFile("test.c", [][]const u8{"-std=c99"});
exe.linkLibrary(lib);
exe.linkSystemLibrary("c");
b.default_step.dependOn(&exe.step);
@ -8896,10 +8897,9 @@ pub fn build(b: *Builder) void {
}
{#code_end#}
<p class="file">terminal</p>
<pre><code class="shell">$ zig build
$ ./test
$ echo $?
0</code></pre>
<pre><code class="shell">$ zig build test
1379
</code></pre>
{#see_also|export#}
{#header_close#}
{#header_open|Mixing Object Files#}