Invalid argument '-std=c++14' not allowed with 'C'

Trying to build C++ sources using Zig. Getting multiple lines of this:

:1:1: error: invalid argument '-std=c++14' not allowed with 'C'

My zig.build:

const std = @import("std");

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

    const ziggame = b.addExecutable(.{
        .name = "ziggame",
        .target = target,
        .optimize = optimize,
    });
    ziggame.linkSystemLibrary("c++");
    ziggame.linkSystemLibrary("SDL2");
    ziggame.linkSystemLibrary("SDL2_mixer");


    ziggame.addCSourceFiles(.{
        .files = &.{
    "src/agent.cpp",
    // <omitted lines>
    "src/zigconfig.cpp",
    "src/zigconfig.h",
        },
        .flags = &.{
            "-std=c++14",
            "-pedantic",
            "-Wall",
            "-W",
            "-Wno-missing-field-initializers",
        },
    });
    b.installArtifact(ziggame);

}

Any ideas how to get rid of the error?

zig 0.13.0

maybe

ziggame.linkLibCpp();

Hello @faveoled
Welcome to ziggit :slight_smile:

Call linkLibCpp instead of linking to "c++".
You might also need to call linkLibC.
i.e.

    ziggame.linkLibCpp();
    ziggame.linkLibC();
    ziggame.linkSystemLibrary("SDL2");
    ziggame.linkSystemLibrary("SDL2_mixer");

@recombinant @dimdin tried both approaches, same error

My suspicion (supported by trying to invoke zig cc directly with similar arguments) is that this has to do with the .h files you’re including in your source files list: they are most likely being inferred as C (not C++) source files, leading to the error you’re getting, as -std=c++14 only makes sense with C++ source files.

The easiest way to solve this would be to remove the .h files from the files list: usually, header files are meant to be #included from your .cpp source files and not compiled separately. You can use ziggame.addIncludePath(...) if you need to add additional directories to be searched when processing #includes.

An alternative, if you really need to include the header files in the compilation, would be to try adding "-x", "c++" (two separate arguments) to your flags. As long as the Zig build system puts the files after those arguments in the zig cc command line, it will cause the language to be overridden to C++ instead of inferring it from the source file extensions (I haven’t tested it to see if the build system actually constructs the command this way, though).

3 Likes

You’re right, error gone after skipping .h files

1 Like