Thread: How to execute specific targets based on a gcc flag(-D) in the Makefile

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    228

    How to execute specific targets based on a gcc flag(-D) in the Makefile

    So my goal as of now is to be able to compile my program under a flag(-D) if and only the user does make -D. If the user simply does make, then compile the program without the -D flag. The problem with this is that the macro MAKEFLAGS does not have the -D flag defined anywhere in it because this is a gcc preprocessor flag. So what can I do?

    Here is how my Makefile looks

    Code:
    
    TARGET = ThreadSync
    
    GCC = gcc
    
    CFLAGS = -Wall -Wextra -g -std=gnu99 -pthread
    
    SYNCTHREAD = -D PTHREAD_SYNC 
    
    all: $(TARGET)
    
    ifneq (,$(findstring D,$(MAKEFLAGS)))
    $(TARGET): 
        $(GCC) $(CFLAGS) [email protected] -o $@ $(SYNCTHREAD)
    else
    $(TARGET): 
        $(GCC) $(CFLAGS) [email protected] -o $@
    endif
    
    .PHONY: clean
    
    clean:
        rm -f $(TARGET)
    Last edited by deathslice; 10-05-2016 at 12:17 PM.

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    228
    oops, can a moderator change the title of this thread to "How to execute specific targets based on a gcc flag(-D) in the Makefile". Don't know what happen there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-19-2013, 03:51 AM
  2. TurboC based, Graphics based program with weird error!
    By sidx64 in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2012, 12:48 PM
  3. Makefile for similar targets
    By King Mir in forum Tech Board
    Replies: 6
    Last Post: 11-26-2011, 07:11 AM
  4. make incorrectly remaking targets
    By jarro_2783 in forum C Programming
    Replies: 4
    Last Post: 05-26-2006, 12:10 AM
  5. Replies: 2
    Last Post: 01-02-2002, 01:42 PM

Tags for this Thread