![]() The source for this example can be found here. Being able to easily build native binaries for almost any platform is a huge strength. I use modified versions of this Makefile for all my various Go projects. It nicely formats and outputs the comments in the Makefile. #Makefile for windows codeI found this code around the internet somewhere and I've been using it ever since. phony is used to force the specified targets to always execute, regardless of whether the output already exists.PHONY: all test cleanĪnd finally, some grep magic to create a help target. all is the default for when you just run make and I want it to run the tests, then build everything. #Makefile for windows windowsbuild: windows linux darwin # Build version: $(VERSION) I like echoing the version so I can easily see what I've built. Next step is to add a target to build for all three operating systems. cmd/service/main.goĮnv GOOS=darwin GOARCH=amd64 go build -i -v -o $(DARWIN) -ldflags="-s -w -X main.version=$(VERSION)". cmd/service/main.goĮnv GOOS=linux GOARCH=amd64 go build -i -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)". windows: $(WINDOWS) # Build for Windowsĭarwin: $(DARWIN) # Build for Darwin (macOS)Įnv GOOS=windows GOARCH=amd64 go build -i -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)". The other flags aren't as important to discuss here, but are for verbosity and optimization. This is extremely useful for auto-versioning binaries. ![]() Setting main.version=$(VERSION) will set a version variable in the main package to the value of VERSION. Setting GOOS and GOARCH before calling go build causes the compiler to build for the specified environment. ![]() The first three targets ( windows| linux| darwin) are just for convenience so I can easily type make linux and build for Linux. Next up, I created the actual build targets. You can also use the normal command prompt and run vsvars32.bat (c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools for VS2008). VERSION=$(shell git describe -tags -always -long -dirty) If you have Visual Studio, run the Visual Studio Command prompt from the Start menu, change to the directory containing Makefile.win and type this: nmake -f Makefile.win. ![]() VERSION is a version string derived from the latest git tag and commit hash, something like v1.1.1-8-g99740b5. WINDOWS, LINUX, and DARWIN are the actual names of the binaries to be created, based off of EXECUTABLE. EXECUTABLE is the name of the program to build. I made a Makefile to take advantage of this.įirst I started with a few variables. All that is required is to change the GOOS and GOARCH env variables to the desired OS and architecture. #Makefile for windows how toMake gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. On a single build machine, you can build binaries for Windows, macOS, and many flavors of Linux. Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files. You can build a binary for just about any system without much hassle. Something that Go does very well is multi-platform support. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |