Thread: Conditional makefile: help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    Conditional makefile: help

    Hello everybody!
    I hope somebody could help me to solve this iussue: I should create a makefile which, according to the gcc version installed on the machine where the make will be run, will set or not the flag "-fno-stack-protector" (for gcc versions < 4.1, this flag doesn't exist since SSP is not present in gcc and i would get an error).
    How could I do such a makefile?
    Thanks in advance!
    Bye!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Collect the output of `gcc -v`, then use something to extract the version number.
    Use that as the basis of your decision.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by Salem View Post
    Collect the output of `gcc -v`, then use something to extract the version number.
    Use that as the basis of your decision.
    hello! 1st of all, thanks for answering!
    my problem is exactly how to parse the result of gcc -v and decide for the flags in a makefile... I am not really experienced with makefiles... Any hints?
    Thanks again!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by smoking81 View Post
    hello! 1st of all, thanks for answering!
    my problem is exactly how to parse the result of gcc -v and decide for the flags in a makefile... I am not really experienced with makefiles... Any hints?
    Thanks again!
    The "gcc --version" command sucks, because the format of its output changes from version to version. Instead, check to see if gcc actually supports the option:

    Code:
    -include .cflags
    CFLAGS  = $(STACK_FLAGS) -W -Wall
    
    all:
            echo My CFLAGS is $(CFLAGS)
    
    .cflags:
            gcc -c -fno-stack-protector check.c
            echo "STACK_FLAGS=-fno-stack-protector" > .cflags
    We trigger a failure-protected inclusion of .cflags. This file has a rule which calls gcc on a dummy file with the flag to be checked. If this call succeeds, the next command in the .cflags rule runs, which creates a mini-makefile containing a variable STACK_FLAGS with the appropriate option. If, on the other hand, gcc does not support the option, processing of the .cflags rule aborts and the .cflags file does not get created, thus the flag does not get set.

    I included an "all" target so you can test if it works.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is a piece of version checking code for gcc in this:
    http://lxr.xensource.com/lxr/source/Config.mk

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  2. Conditional compilation for object files in Makefile?
    By jutirain in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2007, 06:23 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. Need help with Makefile
    By xshapirox in forum C++ Programming
    Replies: 14
    Last Post: 09-28-2004, 03:32 PM