Thread: Included Files and undeclared Identifiers

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    7

    Included Files and undeclared Identifiers

    Ok, this is probably some simple problem to fix, but I cannot figure out what is wrong with it.

    What is happening is that when I put some functions and globally define any variables in my programs (in separate files from my 'main.cpp') and #include them, the compiler/linker are telling me that I haven't declared them yet, but I know I have because they are right there. It doesn't matter where I put them either it always tells me that they are "undeclared".

    I'm using VS .NET 2003 (VC/C++ 7) and this is how i'm doing it:
    Code:
    file: main.cpp**************
    
    int myglobalinteger=0;
    
    #include "myfile.cpp"
    
    void main()
    {
      //blah
      //do something with mydirectxobject
      mydirectxobject->method1();
      myfunction();
    }
    ************************
    
    file: myfile.cpp*************
    
    (some microsoft macro for a typedef) mydirectxobject = NULL;
    void myfunction(int someparam)
    {
      //do something with myglobalinteger
      myglobalinteger+=1
    }
    ************************
    It gives me an error when i try and access any global variable/object that is not in the current file. Have I done something wrong, or is Visual Studio being a pain again?

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Dont include myfile.cpp in main.cpp. Instead place the following in myfile.cpp:

    Code:
    extern int myglobalinteger=0;

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    First off, I'm sorry about that kermi3 I didn't notice the buttons above the textbox, i will use them next time.

    bithub: I can't do that for every variable/object in my program though. Plus there is a program's source that I downloaded off the net and it successfully does what I'm trying to do. And I have examined the code many times and it is no different from mine.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by bithub
    Dont include myfile.cpp in main.cpp. Instead place the following in myfile.cpp:

    Code:
    extern int myglobalinteger=0;
    I believe you shouldn't try to initialize it or you will get linker errors.

    Try setting it up like this:
    Code:
    //main.cpp
    #include "myfile.h"
    
    int myglobalinteger;
    
    int main()
    {
        //blah
      //do something with mydirectxobject
      mydirectxobject->method1();
      myfunction();
    
    }
    Code:
    //myfile.h
    extern object mydirectxobject;
    void myfunction(int someparam);
    Code:
    //myfile.cpp
    extern int myglobalinteger;
    
    object mydirectxobject = NULL;
    void myfunction(int someparam)
    {
      //do something with myglobalinteger
      myglobalinteger+=1
    }
    It is generally bad practice to include .cpp files (the only exception I can think of is inline/template functions, and even then I think you should use a .inl file or include the definitions in the header)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    ok, Thank you all for your input. Yeah, I know including .cpp files in the code is bad practice, but with my code it should have worked, no? I was just trying to get some quick and dirty code done and it was really making me pull my hair out because i couldn't see any reason for it not to work.

    But, VS was being a pain again. First off, I thought it seemed like it was trying to compile each file individually. But i figured out that if i go to the properties of each included source file and change to exclude from build and change to YES. then everything works fine. Thanks though!

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yeah, I know including .cpp files in the code is bad practice, but with my code it should have worked, no?
    No, it shouldn't have, as I will explain below.
    But i figured out that if i go to the properties of each included source file and change to exclude from build and change to YES. then everything works fine.
    That's because they're supposed to be compiled individually. The problem is, the code in the included source files doesn't make sense without the other code that's in main.cpp... so main.cpp will successfully compile (hopefully) with all the contents of the included source files glued in at the top, but the others will not. So in other words, what's happening is:

    main.cpp:
    Code:
    int x;
    void somefunc()
    {
      x = x + 1;
    }
    int main()
    {
       somefunc();
       return 0;
    }
    included.cpp
    Code:
    void somefunc()
    {
       x = x + 1;  //What's x?
    }
    The proper way of solving this problem is, as has been mentioned above:
    -Declare the variables as 'extern' at the top of each .cpp
    -Include the .h file at the top of your .cpp file.
    -Stick the functions' prototypes in a .h file and include that in main.cpp

    Or, you could rename the .cpp files to .h or .inl. But excluding the .cpp's from the build is NOT a good solution at all.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    One other solution is just make a header file with all the extern variables and then include it in each of your cpp files that need them.

    Code:
    myglobals.h
    
    #ifndef MYGLOBALS_H
    #define MYGLOBALS_H
    
    extern variable1;
    extern variable2;
    
    #endif
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    That wouldn't be a good solution. The .cpp files would still be compiled, and if they're also being included in main.cpp, then there's no need for them to be compiled; besides, you might even get redefinition errors from your linker, since the very same functions are defined both in main.cpp and in x.cpp.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    All I was suggesting is that you put the externs in a header file. As long as the globals are defined only once i don't see how it would be any different then placing the extern statemements manually in each file? Maybe i am not fully understanding the problem.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    As long as the globals are defined only once i don't see how it would be any different then placing the extern statemements manually in each file?
    I'm not talking about the externs. What I'm referring to is the fact that both the main.cpp with the included functions inlined and the other .cpp files are compiled. Thus, two of the same function are compiled; and if the linker isn't smart enough to exclude the other .cpp files' compiled object files, then you'll either end up with linker errors from function redefinition (although that should probably be a compiler error), or else you'll end up with duplicate code bloating your .exe file.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Including kernel header files
    By carmaa in forum C Programming
    Replies: 0
    Last Post: 02-03-2008, 03:43 PM
  3. Deleting files that are under use...
    By Yuri in forum Windows Programming
    Replies: 10
    Last Post: 10-31-2005, 10:48 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. g++ error: Macro names must be identifiers.
    By eccles in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2005, 03:28 AM