Skip to content

Commit 164b1a5

Browse files
committed
cgo
1 parent fda3208 commit 164b1a5

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ gocar clean
108108
### 编译构建
109109

110110

111-
**`gocar build [--release] [--target <os>/<arch>] [--help]`**
111+
**`gocar build [--release] [--target <os>/<arch>] [--with-cgo] [--help]`**
112112

113113
构建可执行文件:
114114
- `gocar build` ` 构建 Debug 版本(默认)
115115
- `gocar build --release` 构建 Release 版本(启用CGO_ENABLED=0,ldflags="-s -w" 和 trimpath)
116116
- `gocar build --target <os>/<arch>` 交叉编译到指定平台
117117
- `gocar build --release --target <os>/<arch>` 以 Release 模式交叉编译到指定平台
118+
- `gocar build --with-cgo` 强制启用 CGO(设置 CGO_ENABLED=1)
118119
- `gocar build --help` 显示帮助信息
119120

120121
构建行为:
@@ -125,6 +126,8 @@ gocar clean
125126
| -- release | `CGO_ENABLED=0 go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
126127
| -- target| `GOOS=<os> GOARCH=<arch> go build -o bin/<os>/<arch>/<appName> ./main.go` |
127128
| -- release -- target | `CGO_ENABLED=0 GOOS=<os> GOARCH=<arch> go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
129+
| -- with-cgo | `CGO_ENABLED=1 go build -o bin/<os>/<arch>/<appName> ./main.go` |
130+
| -- release -- with-cgo | `CGO_ENABLED=1 go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
128131

129132
示例:
130133
```bash
@@ -140,6 +143,12 @@ gocar build --target linux/amd64
140143
# Release 模式交叉编译到 Windows AMD64(启用CGO_ENABLED=0,ldflags="-s -w" 和 trimpath)
141144
gocar build --release --target windows/amd64
142145

146+
# 强制启用 CGO 构建
147+
gocar build --with-cgo
148+
149+
# Release 模式下启用 CGO 构建
150+
gocar build --release --with-cgo
151+
143152
# 显示帮助信息
144153
gocar build --help
145154
```

README_en.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,27 @@ Directory structure for **project mode**:
110110
111111
### Build / Compile
112112

113-
**`gocar build [--release] [--target <os>/<arch>] [--help]`**
113+
**`gocar build [--release] [--target <os>/<arch>] [--with-cgo] [--help]`**
114114

115115
Build the executable:
116116

117117
- `gocar build` builds a Debug binary (default)
118118
- `gocar build --release` builds a Release binary (enables `CGO_ENABLED=0`, `ldflags="-s -w"`, and `-trimpath`)
119119
- `gocar build --target <os>/<arch>` cross-compiles for the specified platform
120120
- `gocar build --release --target <os>/<arch>` cross-compiles in Release mode for the specified platform
121+
- `gocar build --with-cgo` forces CGO to be enabled (sets `CGO_ENABLED=1`)
121122
- `gocar build --help` shows help information
122123

123124
Build behavior:
124125

125-
| Mode | Equivalent command |
126-
| ------------------ | ------------------------------------------------------------ |
127-
| debug (default) | `go build -o bin/<os>/<arch>/<appName> ./main.go` |
128-
| --release | `CGO_ENABLED=0 go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
129-
| --target | `GOOS=<os> GOARCH=<arch> go build -o bin/<os>/<arch>/<appName> ./main.go` |
130-
| --release --target | `CGO_ENABLED=0 GOOS=<os> GOARCH=<arch> go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
126+
| Mode | Equivalent command |
127+
| ----------------------- | ------------------------------------------------------------ |
128+
| debug (default) | `go build -o bin/<os>/<arch>/<appName> ./main.go` |
129+
| --release | `CGO_ENABLED=0 go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
130+
| --target | `GOOS=<os> GOARCH=<arch> go build -o bin/<os>/<arch>/<appName> ./main.go` |
131+
| --release --target | `CGO_ENABLED=0 GOOS=<os> GOARCH=<arch> go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
132+
| --with-cgo | `CGO_ENABLED=1 go build -o bin/<os>/<arch>/<appName> ./main.go` |
133+
| --release --with-cgo | `CGO_ENABLED=1 go build -ldflags="-s -w" -trimpath -o bin/<os>/<arch>/<appName> ./main.go` |
131134

132135
Examples:
133136

@@ -144,6 +147,12 @@ gocar build --target linux/amd64
144147
# Release cross-compile for Windows AMD64 (enables CGO_ENABLED=0, ldflags="-s -w" and trimpath)
145148
gocar build --release --target windows/amd64
146149
150+
# Force enable CGO
151+
gocar build --with-cgo
152+
153+
# Release build with CGO enabled
154+
gocar build --release --with-cgo
155+
147156
# Show help
148157
gocar build --help
149158
```

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,19 +479,16 @@ EXAMPLES:
479479
gocar build Build for current platform (debug)
480480
gocar build --release Build for current platform (release)
481481
gocar build --target linux/amd64 Cross-compile for Linux AMD64
482-
gocar build --target windows/amd64 Cross-compile for Windows AMD64
483482
gocar build --release --target linux/arm64 Cross-compile for Linux ARM (release)
484483
gocar build --with-cgo Build with CGO enabled
485484
gocar build --release --with-cgo Build in release mode with CGO enabled
486485
487486
COMMON TARGETS:
488487
linux/amd64 Linux AMD 64-bit
489488
linux/arm64 Linux ARM 64-bit
490-
linux/arm Linux ARM 32-bit
491489
darwin/amd64 macOS Intel
492490
darwin/arm64 macOS Apple Silicon
493491
windows/amd64 Windows 64-bit
494-
windows/386 Windows 32-bit
495492
`
496493
fmt.Print(help)
497494
}

0 commit comments

Comments
 (0)