Skip to content

Instantly share code, notes, and snippets.

@JackyCZJ
Last active November 29, 2022 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackyCZJ/f2284567685596dd34ecc468626b1a73 to your computer and use it in GitHub Desktop.
Save JackyCZJ/f2284567685596dd34ecc468626b1a73 to your computer and use it in GitHub Desktop.
How to cross compile an openwrt CGO program
TARGET= GreatCGOProgram
OPENWRT_SDK=/home/user/openwrt
STAGING_DIR=$(OPENWRT_SDK)/staging_dir
CC=$(BUILD_DIR)/$(OPENWRT_SDK)/staging_dir/$(toolchain_dir)/bin/$(ARCH)-openwrt-linux-gcc
# example:
# toolchain-aarch64_cortex-a53+crypto_gcc-8.4.0_musl
# aarch64-openwrt-linux-gcc
CGO_CFLAGS=-I$(STAGING_DIR)/target-$(ARCH)_[musl|glib]/usr/include
CGO_LDFLAGS=-L$(STAGING_DIR)/target-$(ARCH)_[musl|glib]/usr/lib -lubox -lblobmsg_json -lubus -luci -ljson-c
#example
# target-aarch64_cortex-a53+crypto_musl
all: clean $(TARGET)
$(TARGET):
STAGING_DIR="$(STAGING_DIR)" CC="$(CC)" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_ENABLED=1 GOOS=linux GOARCH=$(ARCH) \
go build -o $(TARGET) main.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment