#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13

    Strange problem with constants


    Hi,

    I'm having a problem in linking that I don't understand. I'm working with mingw with gcc 3.2 under Windows Xp. My project has 4 files:
    cons.h
    usrcon.h (includes cons.h)
    usrcon.cpp (includes usrcon.h)
    main.cpp (include usrcon.h)

    in cons.h I have a
    #ifndef A
    #def A 1

    namespace FS{
    const short int FOO = 23;
    const char *FAA = "Hola";
    }
    The problem is that when I compile the linker says that FS::FAA is defined twice. It seems to me that the #ifndef directive should avoid this, shouldn't it?
    Any idea? What surprises me more is that FSI::FOO does not raise any linking error.
    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    #def A 1

    That isn't a legal statement on my compiler. There is no preprocessor directive #def, and if you meant:

    #define A 1

    do you really want to replace the letter A with the symbol 1 in your code(e.g. your variable FAA). I'm not sure what you're trying to do, but if you're trying to keep a header file from being included twice, then do this:

    #ifndef CONS_H
    #define CONS_H


    namespace FS{
    const short int FOO = 23;
    const char *FAA = "Hola";
    }

    #endif

    If you use the header file name as the identifier, then it will be unique in your program, and that is something you want to make sure of.
    Last edited by 7stud; August 13th, 2003 at 04:32 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Oh, of course it was #define CONS_H in my real case, (the #def was an error,ups; the A a simplification, maybe too much of a simplification). Anyway, I there is no difference between what you say and what I have in the real case:
    #ifndef CONSTANTS_H
    #define CONSTANTS_H
    namespace FSI{
    const unsigned char CONP = 51;
    const unsigned char CONA = 52;
    const unsigned char CONR = 53;

    const unsigned char CST = 61;
    const unsigned char NUSR = 62;

    const unsigned char MSGI = 71;
    const unsigned char MSGP = 72;

    const unsigned char PING = 81;
    const unsigned char LUSR = 91;

    const char CP[13] = "<CP TP=\"\"/>";
    const char USR[38] = "<USR ID=\"\" N=\"\" ST=\"\" MXM=\"\" MXE=\"\"/>";
    const char CUSR[19] = "<USR ID=\"\" NST=\"\"/>";
    const char MSG[30] = "<MSG RID=\"\" EID=\"\" T=\"\" D=\"\"/>";
    const char CON[17] = "<CON N=\"\" PWD=\"\">";

    }
    #endif

    Many thanks but I think that does not solve my problem, :-(.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    6
    Rep Power
    0
    I tried to reproduce your error with the situation as you decribed.
    My usrcon.cpp and usrcon.h files are empty but contains the includes, the cons.h file contains your code. In the main.cpp I have a simple main() function.
    I don't get the error here. I have gcc version 3.2.3 (MinGW) under Win2000. Do you have tested this like me and do you get the error in that case?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    no, I've not tested that. Good idea. I'll do so and let you know. Thanks.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Hi,

    Well, problem solved. I'm not sure what happened but it now works. The only thing I've changed is some of the sizes in the arrays that where wrong. Could it have been that? No idea.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    6
    Rep Power
    0
    I've noticed that too, I don't think that was the problem, maybe the compiler?
    When you changed that file the source was compiled again...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Yes, I asked for a full rebuild. And it seems to work perfectly now!!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "no, I've not tested that."

    lol.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    lol, why?
    Why trying simple things when you're sure you're right ;-)?

IMN logo majestic logo threadwatch logo seochat tools logo