Defining a global MACRO

Discussion in 'C Programming' started by Tagore, Oct 8, 2009.

  1. Tagore

    Tagore Guest

    hi,
    I was working on my first big project in C. Now my project can be
    used for 2 different purposes so some portions of my project needed
    precondition compiling depending on my purpose. so I use
    #ifdef MACRO_NAME
    .....code to purpose-1
    #elif
    .....code for purpose-2
    #endif

    but the problem is that this precondition compiling involve several
    files. so I have to define this macro in all files. Is not there a
    better way to define a global Macro.

    regards,
     
    Tagore, Oct 8, 2009
    #1
    1. Advertisements

  2. Use an include file.
     
    Peter Nilsson, Oct 8, 2009
    #2
    1. Advertisements

  3. Many compilers allow you to specify preprocessor defines on their command
    lines
    e.g.
    gcc -DMACRO_NAME -c foo.c

    Your compiler may allow this.

    If you're working on a big project, you probably want to have something
    automatically
    controlling the building of the project, such as make, Eclipse, or ant.
    These
    tools can make it easier to specify what compiler options will be used for
    all C source
    files.
     
    Morris Keesan, Oct 8, 2009
    #3
  4. what the other posters said is all good stuff. But can you avoid this
    altogether? Can you separate the purpose-1 stuff from the purpose-2
    stuff? Can you put them in different files? Then you build two
    different
    applications

    purpose-1-app
    common-library

    purpose-2-app
    common-library

    No nasty ifdefs anymore!
     
    Nick Keighley, Oct 8, 2009
    #4
  5. Tagore

    Eric Sosman Guest

    #include "file_that_might_or_might_not_define_MACRO_NAME.h"
     
    Eric Sosman, Oct 8, 2009
    #5
  6. El jueves, 8 de octubre de 2009 07:27:09 UTC-5, Eric Sosman escribió:
    I have same problem, don't you find a better solution for this?
     
    adrian.d.arpi.s, Aug 21, 2013
    #6
  7. You're replying to an article that was posted nearl 4 years ago.

    Many compilers let you specify macro values on the command line. gcc is
    typical:

    gcc -DMACRO_NAME foo.c

    You can add this to your Makefile, build script, or whatever you use to
    invoke the compiler.

    I don't know that that's necessarily a *better* solution.
     
    Keith Thompson, Aug 21, 2013
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.