Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile doesn't respect externally supplied variables #10

Closed
yurivict opened this issue Nov 4, 2017 · 3 comments
Closed

Makefile doesn't respect externally supplied variables #10

yurivict opened this issue Nov 4, 2017 · 3 comments
Labels

Comments

@yurivict
Copy link

yurivict commented Nov 4, 2017

Makefile should keep CFLAGS/CXXFLAGS/LDFLAGS, because build frameworks can supply their own optimization variables.

Also, LDFLAGS doesn't need -O3. Optimization only occurs during compilation.

It should be something like this:
CFLAGS?=-g -O3 ...

@lucasb-eyer
Copy link
Owner

Thanks for your feedback! This issue is about two things:

1. Use ?= instead of = for *FLAGS.

The original idea was to just have people drop the .h/.c file (and license 👮‍♂️) into their project and use their build-system, the Makefile here only serving as an example and to build the tests, benchmarks, and examples.

What I'm afraid of going the ?= route is that, for example, I need the -I. flag and some others, and I'm confident naive integration will fail or produce slower binaries. And if naive integration using the Makefile fails, one might aswell go ahead and put the .c/.h files in their project, as was intended.

If you still disagree, could you give me a concrete example of why one wants to integrate the full Makefile as opposed to just dropping the couple files in their project?

2. Don't add -O3 to LDFLAGS

You are not completely right here. When using link-time optimization -flto, it is highly recommended to pass the same flags to the linker, see the GCC help to -flto which I'm quoting here:

To use the link-time optimizer, -flto and optimization options should be specified at compile time and during the final link. It is recommended that you compile all the files participating in the same link with the same options and also specify those options at link time. For example:

gcc -c -O2 -flto foo.c
gcc -c -O2 -flto bar.c
gcc -o myprog -flto -O2 foo.o bar.o

I am not actually using -flto because it can cause problems with some CUDA installations not supporting it in their overwritten GCC version and this error can be quite subtle to figure out, but I do recommend it in the comment. I will make this reason for adding the same -O to the linker flags explicit in the comment, too.

@yurivict
Copy link
Author

yurivict commented Nov 4, 2017

Use ?= instead of = for *FLAGS

You should first keep CFLAGS when supplied by the caller (?=), assuming it has all optimization options. Then add -I flags that are needed with +=.

I am not actually using -flto

Right, without -flto -O3 doesn't do anything during linking. Both should be enabled together.

@lucasb-eyer
Copy link
Owner

You are absolutely correct. For some reason, += didn't cross my mind. I have fixed this, and tested it myself with and without env CFLAGS=-O0 CXXFLAGS=-O0 make and it works as you'd expect now. Please see if you agree with the way it is now.

I made -fPIC an unconditional flag as without it, ar can't build a static lib, which I do build unconditionally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants