2

I have a set of C programs that are generally compiled using gcc in a makefile, but OSX Mavericks now uses clang. What is a good way to test for existing compilers in a makefile and use the appropriate one? Moreover, I would also like to use architecture-dependent optimization flags (e.g., for gcc on Linux, I would use -O3, but on OSX I would use -fast; for clang on OSX I would use -Ofast).

Kindest thanks,

Ryan

Ryan
  • 21
  • 2
  • 1
    The make variable `CXX` is the appropriate C++ compiler, and `CC` is the C compiler. See `info make Implicit Implicit` for a list of such variables. – rici Jul 03 '14 at 20:59

1 Answers1

6

If you are compiling on MacOS X and Linux then you can put the following in your makefile:

UNAME := $(shell uname)

ifeq ($(UNAME), Darwin)
CFLAGS = -Ofast
endif

ifeq ($(UNAME), Linux)
CFLAGS = -O3
endif

# etc
koan
  • 3,596
  • 2
  • 25
  • 35
  • I think `CFLAGS += ...` might be better. – rici Jul 03 '14 at 21:00
  • This is very helpful! I suppose what I am missing is how to test the compiler. I would like to do something similar to what is done in this post in cmake: http://stackoverflow.com/questions/10046114/in-cmake-how-can-i-test-if-the-compiler-is-clang. That is, does make have an analogous variable to CMAKE_CC_COMPILER_ID? I can't seem to find one. Thanks! – Ryan Jul 09 '14 at 21:37
  • You could define the compiler to use yourself. Or you can use clang on both OSX and Linux. – koan Jul 10 '14 at 09:09
  • Ok, thanks. That is easy for me to do, but for software that is fairly widely distributed, I would like the make file to compile on both Linux and OSX without the user having to update the makefile. This was easy when gcc was essentially universal. But that changed when OSX Mavericks switched to clang. Many Linux systems (particularly older distributions) do not seem to have clang installed. – Ryan Jul 10 '14 at 18:23