Fixing incorrect extern declaration

This is a discussion on Fixing incorrect extern declaration within the C++ Programming forums, part of the General Programming Boards category; I'm rephrasing my question on a thread I started earlier since it didn't really help me solve the problem I ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Fixing incorrect extern declaration

    I'm rephrasing my question on a thread I started earlier since it didn't really help me solve the problem I had.
    (The particular thread is:
    Compiles on gcc 3.3 but not on gcc 4.0.3)

    I have an external variable declared in "Globals.h"
    Code:
    extern float PROB_SMOOTH;
    There are tons of other source files that reference this variable but do not declare it IN ANYWAY internally.
    For example, a file "NTables.h" has a bunch of code, then in the middle pops out a reference to PROB_SMOOTH without a declaration of PROB_SMOOTH anywhere in "NTables.h".
    Also, "Globals.h" does not exist in the header include hierarchy of "NTables.h".

    I wish to go about fixing this by declaring
    Code:
    extern float PROB_SMOOTH;
    in all the source files that reference PROB_SMOOTH.

    I know it will compile. I've tried it.

    But, someone, if I'm doing something wrong here, please let me know.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    So you're asking whether or not that's the best way to solve the problem?

    I'd probably create a new header file and include that in every source file that uses PROB_SMOOTH, or if you already have such a header file, put it in there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks. That also solved the problem, so I guess my method would have fouled up the source in some way.

    Do you know by any chance of a link that explains why this particular sort of coding will compile in gcc 3.33 but not in gcc 4.0.3?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Do you know by any chance of a link that explains why this particular sort of coding will compile in gcc 3.33 but not in gcc 4.0.3?
    No. But:
    On one computer, an OS X, I have gcc 3.3 installed.
    This is the second thread this week about someone having trouble with OS X's gcc compiler. Oh, never mind, the other thread was yours as well. http://cboard.cprogramming.com/searc...earchid=607635

    So for gcc 4.0.3 (linux) you need the extern declarations but not for gcc 3.3 (mac)? That's odd for sure. Are your warning levels different at all?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Yes. It's code I got off the Internet, and in Mac OS 10.3, all you have to do is unzip the thing, run make, and you're ready to use it.

    But on Ubuntu, gcc 4.0.3, I've had to
    1. use "this->" scope specifiers so that the compiler could find some variables
    2. either include a header file where the declaration was made or declare it as extern myself.

    For the warnings I got in (1), the warning messages said I could use the flag "-fpermissive" to make it compile, but it discouraged me from doing so. Well, I tried it, didn't work.

    With the PROB_SMOOTH variable, it just couldn't find it, only natural considering it was never included. The strange thing is it compiled on OS X.

    Anyway, I've "fixed" everything and it's compiling now, so I'm sort of satisfied, but I'd be happier if I new why it compiled on the Mac in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21