Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:59:51+00:00 2026-06-10T08:59:51+00:00

I’m looking for help getting my Makefile to do what I want it to

  • 0

I’m looking for help getting my Makefile to do what I want it to do.

I have figured out how to add preprocessing code to my C source which will compile only if I’m compiling for debug:

#if DEBUG
  printf("main()\n");

  {
    /* Pauses execution so gdb can attach. */
    int i=9;
    pid_t PID;
    char hostname[256];
    gethostname(hostname, sizeof(hostname));
    printf("PID %d on %s ready for attach.\n", PID=getpid(), hostname);
    fflush(stdout);
    while (i>0) {
      sleep(5);
      i--;
    }
  }
#endif

And I’ve figured out that if I add -DDEBUG=1 to my compile statement, that the above code will be compiled (otherwise it’s not compiled).

Next, I want to pass a flag to my Makefile which will either include, or not include the -D option. Currently, I have two separate compile lines which I comment and uncomment as appropriate. Here is my Makefile (which I inherited from someone and am having a difficult time understanding). See the lines that say CFLAGS?:

SHELL = /bin/sh

prefix       = /home/schwarz/sundials/instdir
exec_prefix  = ${prefix}
includedir   = ${prefix}/include
libdir       = ${exec_prefix}/lib

CPP         = cc -E
CPPFLAGS    =
CC          = cc
# CFLAGS      = -Wall -g
CFLAGS      = -Wall -g -DDEBUG=1
# CFLAGS      = -g -O2
LDFLAGS     =
LIBS        = -lm
MPICC       = /usr/local/mpi/bin/mpicc
MPI_INC_DIR = /usr/local/mpi/bin/../include
MPI_LIB_DIR = /usr/local/mpi/bin/../lib
MPI_LIBS    =
MPI_FLAGS   =

INCLUDES = -I${includedir} -I${MPI_INC_DIR}
LIBRARIES = -lsundials_cvode -lsundials_nvecparallel ${LIBS}
LIBRARIES_BL =

EXAMPLES = FPU          # cvAdvDiff_non_p cvDiurnal_kry_bbd_p cvDiurnal_kry_p


OBJECTS = ${EXAMPLES:=.o}

# -----------------------------------------------------------------------------------------

.SUFFIXES : .o .c

.c.o :
        ${MPICC} ${CPPFLAGS} ${CFLAGS} ${MPI_FLAGS} ${INCLUDES} -c $<

# -----------------------------------------------------------------------------------------

all: ${OBJECTS}
        @for i in ${EXAMPLES} ; do \
          echo "${MPICC} -o $${i} $${i}.o ${MPI_FLAGS} ${CFLAGS} ${LDFLAGS} -L${libdir} ${LIBRARIES} -L${MPI_LIB_DIR} ${MPI_LIBS} ${LIBRARIES_BL}" ; \
          ${MPICC} -o $${i} $${i}.o ${MPI_FLAGS} ${CFLAGS} ${LDFLAGS} -L${libdir} ${LIBRARIES} -L${MPI_LIB_DIR} ${MPI_LIBS} ${LIBRARIES_BL}; \
        done

clean:
        rm -f ${OBJECTS}
        rm -f ${EXAMPLES}

I have searched the web – I swear! – but cannot figure out how to pass a parameter into the makefile. How can I set up my Makefile so that it does different types of compiles of the same code for different compile needs? Even better would be if I could pass a specific value to the Makefile, which would pass a specific value to the compile, which I could use to initialize i in the code. (IOW, I compile as make 8 and the C is compiled as int i=8;). Is any of that even possible in Make?

As an aside, I’ve read about Makefiles and how they work by listing files with labels (colons), followed by their dependencies. But I can’t understand how this Makefile works or why it issues two compile statements. So a brief explanation would also be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:59:53+00:00Added an answer on June 10, 2026 at 8:59 am

    To choose between debug and release versions:

    ifeq($(debug),1)
      CFLAGS      = -Wall -g -DDEBUG=1
    else
      CFLAGS      = -Wall -g
    endif
    

    and then invoke make:

    make debug=1
    

    to compile with DEBUG, or:

    make
    

    to compile without debug.

    For initializing i:

    #include <stdio.h>
    
    #ifndef INIT_I
    # define INIT_I 9
    #endif
    int main() {
      int i = INIT_I;
      return 0;
    }
    

    and makefile:

    ifneq ($(init_i),)
    CFLAGS = "-DINIT_I=$(init_i)"
    endif
    
    all:
      gcc d.c $(CFLAGS) -E
    

    to set initial i:

    make init_i=10
    

    or use default value:

    make
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.