Defining a global MACRO

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

  1. Tagore

    Tagore Guest

    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
    .....code for purpose-2

    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.

    Tagore, Oct 8, 2009
    1. Advertisements

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

  3. Many compilers allow you to specify preprocessor defines on their command
    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
    controlling the building of the project, such as make, Eclipse, or ant.
    tools can make it easier to specify what compiler options will be used for
    all C source
    Morris Keesan, Oct 8, 2009
  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



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

    Eric Sosman Guest

    #include "file_that_might_or_might_not_define_MACRO_NAME.h"
    Eric Sosman, Oct 8, 2009
  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
  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

    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
    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.