CFLAGS & LDFLAGS documentation somewhere?

Hello all,
I want to modify an existing package witch is a cgo binding around a C library.
it’s nearly done and already working, but I want to make it cross-platform and want to change things (or at least understand well) in the starting CFLAGS and LDFLAGS. (I’m on Windows actually)
I spend most of my sunday evening searching for documentation about those flags, but found nearly nothing…!!

I found several places with little - but useful - information, like here :
http://stackoverflow.com/questions/519342/what-is-the-difference-between-i-and-l-in-makefile
but also found many places where others search for this doc like me, and the answers where really not encouraging…
http://stackoverflow.com/questions/16044020/gcc-and-linking-environment-variables-and-flags

I understand that it’s not really a cgo problem, but since cgo use those flags, why is there not some documentation or a link to it.
so, does someone can give me a link to a possible documentation about those flags…?
thanks a lot.
ffred

* really sorry, but the forum doesn’t want me to add links in my post… :disappointed:
[Edit] ok, its working now. i edited the links…

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.

Thanks a lot @nathankerr for your reply. I will look at the examples tonight…

I’m using TDM-GCC, so MinGW-w64.
I understand that it should be in compiler documentation and I search this way too. but if you look at my second link, it seems that those flags are “originated from Unix OS family” and no-one on this link was able to give a link to a complete documentation.

TDM-GCC is based on GCC 5.1.0 (assuming that is the latest version), so the gcc 5.1.0 documentation is what you are looking for.

thanks, but already looked there…
I found a few tags I recognized for having already seen them on CFLAGS or LDFLAGS in example packages, but except for that, I don’t know what I’m looking for and there’s no section about “CFLAGS” or “LDFLAGS”, so too complicated to find something useful in here. I found more informations on places like my first link and on package examples.
this one for example was instructive :
https://github.com/karalabe/gousb/tree/master/usb
(look in “usb.go”)
ffred

There is no section for CFLAGS or LDFLAGS because these are not concepts gcc cares about. The contents of these variables are just the command line options for the compiler (which I linked to). Compilers are complicated things, so they have lots of options.

usb.go is a nice example of setting those variables, but does not explain what the settings do.

I usually figure out what I am looking for by reading the errors from the compiler and then looking for something to help me out. For example, when a header file is not found by the compiler, but I know where it is I scan through the page I linked to and find these options specify directories to search for header files, for libraries and for parts of the compiler. The first flag on that page solves that problem.

If, on the other hand, you are looking to understand flags that are used, then searching through the complete list of flags in Option Summary using the browser’s search feature will get me to the page explaining that option.

I suppose the cgo docs could explain that CFLAGS, etc are command line options to the c compiler. I think those docs assume an audience that is already familiar with building c, and therefore associated conventions. Thats the background I was trying to give you with my example above.

Do you have specific flags you are trying to understand or something you are trying to do?

2 Likes

ok, thanks a lot… will do more try and search with that doc.
not on my computer here, so maybe just that simple example for witch I didn’t find info :
// #cgo amd64 386 CFLAGS: -DX86=1
and here, I don’t know what is this DX86 one… I found other uses witch make me think it could be related with optimization or compilation mode, but nothing clear.

-DX86=1 defines a macro, X86, as 1.

2 Likes

ok, thanks. you’re right, I need to search more in GCC doc.
so, just look rapidly here : https://gcc.gnu.org/onlinedocs/
I can download a PDF documentation an it’s easier to do searches across the whole doc.
thanks again. :slight_smile:
will try to read some parts of this doc, and will do try & search to find what I need.
ffred

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.