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

    OBJECTIVE
    {
    // 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
    all.

    a^2
     
    aaragon, Feb 11, 2007
    #1
    1. Advertising

  2. aaragon

    red floyd Guest

    aaragon wrote:
    > 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_);


    Delete the semicolon.

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

    > // file main.cpp
    >
    > OBJECTIVE
    > {
    > // 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
    > all.



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

  3. aaragon

    red floyd Guest

    red floyd wrote:
    > aaragon wrote:
    >> 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_);

    >
    > Delete the semicolon.
    >
    > However, in general, this is not a good idea.
    >
    >> // file main.cpp
    >>
    >> OBJECTIVE
    >> {
    >> // definition for objective
    >> }
    >>


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

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

    aaragon Guest

    On Feb 11, 12:49 pm, red floyd <> wrote:
    > aaragon wrote:
    > > 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_);

    >
    > Delete the semicolon.
    >
    > However, in general, this is not a good idea.


    why not?

    >
    > > // file main.cpp

    >
    > > OBJECTIVE
    > > {
    > > // 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
    > > all.

    >
    > did you #include "file.h"
     
    aaragon, Feb 11, 2007
    #4
  5. aaragon

    aaragon Guest

    On Feb 11, 12:53 pm, "aaragon" <> wrote:
    > On Feb 11, 12:49 pm, red floyd <> wrote:
    >
    > > aaragon wrote:
    > > > 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_);

    >
    > > Delete the semicolon.

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

    >
    > why not?
    >
    >
    >
    > > > // file main.cpp

    >
    > > > OBJECTIVE
    > > > {
    > > > // 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
    > > > all.

    >
    > > did you #include "file.h"


    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
    user:

    // file.h
    class Objective
    {
    public:

    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
    #5
  6. aaragon wrote:
    > On Feb 11, 12:53 pm, "aaragon" <> wrote:
    >
    >>On Feb 11, 12:49 pm, red floyd <> wrote:
    >>
    >>
    >>>aaragon wrote:
    >>>
    >>>>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_);

    >>
    >>>Delete the semicolon.

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

    >>
    >>why not?
    >>
    >>
    >>
    >>
    >>>>// file main.cpp

    >>
    >>>>OBJECTIVE
    >>>>{
    >>>> // 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
    >>>>all.

    >>
    >>>did you #include "file.h"

    >
    >
    > 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
    > user:
    >
    > // file.h
    > class Objective
    > {
    > public:
    >
    > 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?
    >


    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
     
    John Harrison, Feb 11, 2007
    #6
  7. aaragon

    Chris Theis Guest

    "aaragon" <> wrote in message
    news:...
    > On Feb 11, 12:53 pm, "aaragon" <> wrote:
    >> On Feb 11, 12:49 pm, red floyd <> wrote:
    >>
    >> > aaragon wrote:
    >> > > 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_);

    >>
    >> > Delete the semicolon.

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

    >>
    >> why not?


    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
    (http://www.parashift.com/c -faq-lite/inline-functions.html#faq-9.5) -
    "Macros are bad for your health ;-)"

    Cheers
    Chris
     
    Chris Theis, Feb 12, 2007
    #7
  8. aaragon

    aaragon Guest

    On Feb 12, 7:25 am, "Chris Theis" <>
    wrote:
    > "aaragon" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > On Feb 11, 12:53 pm, "aaragon" <> wrote:
    > >> On Feb 11, 12:49 pm, red floyd <> wrote:

    >
    > >> > aaragon wrote:
    > >> > > 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_);

    >
    > >> > Delete the semicolon.

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

    >
    > >> why not?

    >
    > 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
    > (http://www.parashift.com/c -faq-lite/inline-functions.html#faq-9.5) -
    > "Macros are bad for your health ;-)"
    >
    > Cheers
    > Chris


    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.

    a^2
     
    aaragon, Feb 13, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spamfurnace
    Replies:
    1
    Views:
    2,669
    Scott M.
    May 24, 2004
  2. Patrick Olurotimi Ige
    Replies:
    7
    Views:
    6,045
    Patrick Olurotimi Ige
    Jun 15, 2005
  3. Replies:
    80
    Views:
    2,522
    Stephen J. Bevan
    Nov 7, 2003
  4. Replies:
    1
    Views:
    492
    Marco Antoniotti
    Oct 7, 2003
  5. Jurjen de Groot
    Replies:
    0
    Views:
    332
    Jurjen de Groot
    May 19, 2008
Loading...

Share This Page