CFLAGS & LDFLAGS documentation somewhere?

CFLAGS and LDFLAGS are a convention that comes from the predefined rules for Makefiles. I will give a brief example to demonstrate how they work. Unfortunately I don’t have a Windows system handy, and so had to make do with my macOS system. Since the example is for explanation instead of something for you to do, this should be ok.

Given hello.c:

#import <stdio.h>

int main() {
	printf("hello");
	return 0;
}

The executable hello can be built by running:

make hello

Make uses its predefined rule to figure out that hello can be built from hello.c and runs:

cc     hello.c   -o hello

The default rules are configured using variables defined in the environment or makefile. I use environment variables, though they are set differently in Windows. To show where the variables show up, I run:

CC=compiler CFLAGS=cflags LDFLAGS=ldflags make hello

Make outputs:

compiler cflags  ldflags  hello.c   -o hello
make: compiler: No such file or directory
make: *** [hello] Error 1

This shows that CFLAGS and LDFLAGS are used as part of the command line for the compiler. (They are split to allow for separate compilation and linking). CFLAGS stands for compiler flags; LDFLAGS for linker flags (the linker is ld).

So, where is the documentation for CFLAGS and LDFLAGS? It is part of the c compiler documentation where command line options are described.

Which c compiler? Run go env and look for CC (c compiler). My system uses clang (clang command line options).

You can set different CFLAGS for different environments using build flags. See https://github.com/mattn/go-sqlite3 (cgo wrapper for sqlite3), especially sqlite3_windows.go, for an example.

Hope this helps.