can I make #define macros visible in another file?

Discussion in 'C++' started by aaragon, Feb 11, 2007.

  1. aaragon

    aaragon Guest

    Hi everyone, I have a simple question. I'm trying to make a macro in
    one file so I can use it in main.cpp. The idea is that I the user of
    my code has simple to type the macro definition to replace a more
    complicated code. Something like the following:

    // file.h
    #define OBJECTIVE template <class T> \
    void operator()(T& chrom_,double* fitness_);

    // file main.cpp

    // definition for objective

    I've been trying to do this but it doesn't work. It seems that the
    macros can only be used in the same file? Any suggestions? Thank you

    aaragon, Feb 11, 2007
    1. Advertisements

  2. aaragon

    red floyd Guest

    Delete the semicolon.

    However, in general, this is not a good idea.

    did you #include "file.h"
    red floyd, Feb 11, 2007
    1. Advertisements

  3. aaragon

    red floyd Guest

    Let me clarify my "not a good idea". I'm not talking about killing the
    semicolon. I'm saying that this sort of usage is in general not a good

    What problem are you trying to solve?
    red floyd, Feb 11, 2007
  4. aaragon

    aaragon Guest

    why not?
    aaragon, Feb 11, 2007
  5. aaragon

    aaragon Guest

    Well, I'm writing a library and I want it user-friendly for people
    who're gonna use it. This library uses a functor that must be written
    by the user. Therefore, instead of allowing the user to write
    completely the code for the functor, I just wrote the functor and the
    declaration of operator in a .h file that will be included by the

    // file.h
    class Objective

    template <class T>
    void operator()(T& chrom_,double* fitness_);


    #define OBJECTIVE template <class T> \
    void Objective::eek:perator()(T& chrom_,double* fitness_)

    Therefore, the user of the library has to type OBJECTIVE in main.cpp
    to write the templated syntax for operator(). The way I use the macro
    here is just for string replacement so I thought it was a good idea.
    Do you have a better suggestion?
    aaragon, Feb 11, 2007
  6. Macros can of course be defined in one file and used in another.
    Standard header files define several macros.

    I suspect you are running into another problem. Templates must be fully
    defined in header files. You cannot get you user to write a templated
    method in a cpp file and expect it to link with your library code.
    You're going to have to find some other solution.

    John Harrison, Feb 11, 2007
  7. aaragon

    Chris Theis Guest

    Apart from the problem with the templates that John already pointed out,
    define macros can be the source of hours of tiresome "debugging", where
    there is actually no bug. The issue is that the preprocessor might modify
    your code in a way that you do not intend and this can lead for example to
    confusing error messages that have absolutely nothing to do with the actual
    problem that you're facing. I just had an example of old code which fixed
    the non compliance of for statements of an old compiler with such a define.
    Upgrading to a newer version of the compiler where I used "for" in a #pragma
    got me this "for" replaced by the define and this resulted in an absolutely
    nonsencial error message, which took me quite some time to figure out.

    Thus, I'd consider trading transparency to the library user for some more
    coding on their part. But I can also understand that you're trying to make
    things as nice as possible. Just keep the FAQ in mind
    ( -
    "Macros are bad for your health ;-)"

    Chris Theis, Feb 12, 2007
  8. aaragon

    aaragon Guest

    I understand your point and now I know that if I run into problems,
    the macros I defined are the first place to look at. However, I'm not
    using macros as functions but only as pure text replacements. I got my
    code working and it seems do the right thing without any problems. I
    just want the user to type OBJECTIVE instead of the more complicated
    and error-prone

    template <class T>
    void operator()(T& chrom_,double* fitness_)

    Thanks for all your advice.

    aaragon, Feb 13, 2007
    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.