golang/go · error · PackageError
Objective-C source files not allowed when not using cgo or S
Error message
Objective-C source files not allowed when not using cgo or SWIG: %s
What it means
A Go package contains Objective-C source files (.m) but neither cgo nor SWIG is active. Like the C++ check, this restriction applies to all compiler toolchains. The check is len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig(). Objective-C files are typically used for macOS/iOS framework integration.
Source
Thrown at src/cmd/go/internal/load/pkg.go:2115
// 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
}
func (e *EmbedError) Error() string {
return fmt.Sprintf("pattern %s: %v", e.Pattern, e.Err)
}
View on GitHub (pinned to b6b368adc5)
Solutions
- Enable cgo and add a proper // #cgo preamble in a .go file that wraps the Objective-C code.
- Ensure CGO_ENABLED=1, especially on macOS (go env CGO_ENABLED).
- Remove .m files if Objective-C integration is not needed for the current build target.
- Consider SWIG for complex Objective-C framework bridging.
Example fix
// before — .m file present but no cgo // mypackage/ // main.go // delegate.m // after — add cgo wrapping ObjC code // mypackage/main.go package main /* #cgo darwin LDFLAGS: -framework Foundation #import <Foundation/Foundation.h> #include "delegate.h" */ import "C" // mypackage/delegate.m remains
Defensive patterns
Strategy: validation
Validate before calling
// Check that Objective-C files are accompanied by cgo or SWIG before building.
func checkObjcCgoRequirement(pkgDir string) error {
entries, _ := os.ReadDir(pkgDir)
var hasM, hasCgo, hasSwig bool
for _, e := range entries {
name := e.Name()
if strings.HasSuffix(name, ".m") {
hasM = 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 hasM && !hasCgo && !hasSwig {
return fmt.Errorf("Objective-C files present without cgo or SWIG")
}
return nil
} Prevention
- Set up cgo with #cgo LDFLAGS for macOS frameworks whenever adding .m files.
- Ensure CGO_ENABLED=1 on macOS when building with Objective-C.
- Remove .m files if macOS framework integration is not needed for the target platform.
- Use #import for framework headers in the cgo preamble.
When it happens
Trigger: Adding .m files to a package directory without setting up cgo or SWIG integration. Having leftover Objective-C files from a previous macOS-specific build configuration. Cross-compiling away from macOS while .m files remain in the source tree.
Common situations: Integrating macOS frameworks (Foundation, UIKit, AppKit) via Objective-C without proper cgo setup. Accidental inclusion of .m files in a cross-platform package. Removing cgo integration but forgetting to delete .m files. Third-party macOS bindings that ship .m files.
Related errors
- C source files not allowed when not using cgo or SWIG: %s
- C++ source files not allowed when not using cgo or SWIG: %s
- no Go source files
- reading %s: %v
- missing =<value> in <pattern>=<value>
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/f46b9f75ac882cbb.
Report an issue: GitHub.