golang/go · error · PackageError

C++ source files not allowed when not using cgo or SWIG: %s

Error message

C++ source files not allowed when not using cgo or SWIG: %s

What it means

A Go package contains C++ source files (.cc, .cpp, .cxx) but neither cgo nor SWIG is active. Unlike the C-file check which only applies to the gc compiler, this restriction applies to all toolchains (both gc and gccgo). The check is len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig().

Source

Thrown at src/cmd/go/internal/load/pkg.go:2111

		p.MFiles = nil
		p.SwigFiles = nil
		p.SwigCXXFiles = nil
		// Note that SFiles are okay (they go to the Go assembler)
		// and HFiles are okay (they might be used by the SFiles).
		// Also Sysofiles are okay (they might not contain object
		// code; see issue #16050).
	}

	// The gc toolchain only permits C source files with cgo or SWIG.
	if len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == "gc" {
		setError(fmt.Errorf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")))
		return
	}

	// C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,
	// regardless of toolchain.
	if len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
		setError(fmt.Errorf("C++ source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CXXFiles, " ")))
		return
	}
	if len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
		setError(fmt.Errorf("Objective-C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.MFiles, " ")))
		return
	}
	if len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {
		setError(fmt.Errorf("Fortran source files not allowed when not using cgo or SWIG: %s", strings.Join(p.FFiles, " ")))
		return
	}
}

// An EmbedError indicates a problem with a go:embed directive.
type EmbedError struct {
	Pattern string
	Err     error
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Enable cgo and add a // #cgo CXXFLAGS preamble in a .go file that wraps the C++ code with extern "C".
  2. Ensure CGO_ENABLED=1.
  3. Remove the C++ files if they are not needed for the build.
  4. Use SWIG (.swigcxx file) for complex C++ class integration.

Example fix

// before — .cpp file present but no cgo
// mypackage/
//   main.go
//   helper.cpp

// after — add cgo with C++ support
// mypackage/main.go
package main

/*
#cgo CXXFLAGS: -std=c++11
#cgo LDFLAGS: -lstdc++
#include "helper.h"
*/
import "C"
// mypackage/helper.cpp with extern "C" wrapper remains
Defensive patterns

Strategy: validation

Validate before calling

// Check that C++ files are accompanied by cgo or SWIG before building.
func checkCxxCgoRequirement(pkgDir string) error {
    entries, _ := os.ReadDir(pkgDir)
    var hasCxx, hasCgo, hasSwig bool
    for _, e := range entries {
        name := e.Name()
        for _, suffix := range []string{".cc", ".cpp", ".cxx"} {
            if strings.HasSuffix(name, suffix) {
                hasCxx = true
            }
        }
        if strings.HasSuffix(name, ".swig") || strings.HasSuffix(name, ".swigcxx") {
            hasSwig = true
        }
        if strings.HasSuffix(name, ".go") {
            data, _ := os.ReadFile(filepath.Join(pkgDir, name))
            if bytes.Contains(data, []byte("import \"C\"")) {
                hasCgo = true
            }
        }
    }
    if hasCxx && !hasCgo && !hasSwig {
        return fmt.Errorf("C++ files present without cgo or SWIG")
    }
    return nil
}

Prevention

When it happens

Trigger: Adding .cpp or .cc files to a package directory without cgo or SWIG integration. Having leftover C++ files after removing cgo integration. Building with CGO_ENABLED=0 while C++ files remain.

Common situations: Mixing C++ and Go without proper cgo setup. Accidental inclusion of C++ files in a Go package directory. Disabling cgo while C++ files remain. Third-party dependencies that ship C++ files without proper cgo annotations.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/6018e00f4c681326. Report an issue: GitHub.