Skip to content

Segfault when building with self-hosted x86_64 backend #25997

@plzfgme

Description

@plzfgme

Zig Version

0.15.2

Steps to Reproduce and Observed Behavior

The build failed silently.

Image

dmesg shows:
Image

lldb:
Image

build.zig

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const hello = b.addObject(.{
        .name = "hello",
        .root_module = b.createModule(.{
            .target = target,
            .optimize = optimize,
            .link_libcpp = true,
        }),
    });

    hello.root_module.addCSourceFiles(.{
        .files = &.{
            "foo.cpp",
            "bar.cpp",
        },
    });
    hello.root_module.addIncludePath(.{
        .cwd_relative = ".",
    });

    const exe = b.addExecutable(.{
        .name = "lca",
        .root_module = b.createModule(.{
            .root_source_file = b.path("main.zig"),
            .target = target,
            .optimize = optimize,
            .link_libcpp = true,
            .imports = &.{},
        }),
        // .use_llvm = true,
    });

    exe.root_module.addObject(hello);
    exe.root_module.addIncludePath(.{
        .cwd_relative = ".",
    });
    b.installArtifact(exe);
}

foo.cpp

#include "foo.h"
#include <string>
#include <vector>

void foo() { std::vector<std::string> keys{"xxx", "yyy"}; }

foo.h

#ifndef _FOO_H_
#define _FOO_H_

#ifdef __cplusplus
extern "C" {
#endif

void foo();

#ifdef __cplusplus
}
#endif

#endif

bar.cpp (similar to foo.cpp)

#include "bar.h"
#include <string>
#include <vector>

void bar() { std::vector<std::string> keys{"xxx", "yyy"}; }

bar.h

#ifndef _BAR_H_
#define _BAR_H_

#ifdef __cplusplus
extern "C" {
#endif

void bar();

#ifdef __cplusplus
}
#endif

#endif

main.zig

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, World!\n", .{});
}

Using .use_llvm=true or removing one of foo.cpp and bar.cpp from build.zig can fix this.

Expected Behavior

No error

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions