Thread: make variables visible on multiple files

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    7

    make variables visible on multiple files

    How can I make a variable visible on different C++ source files? In other words, can I declare a variable with a scope that's wider than the "file" level? I understand it's a basic question but both my book and a friend of mine that used to be a professional c programmer weren't able to give me an answer, and I didn't find anything on the FAQ. I am writing something with QT 3 , and I need to pass some variables contained into a machine-generated ui source file (qtballit.ui.h) to another one (trajectory.ui.h) . 10x in advance!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Declare the variable in one file globally. In the other file declare the same variable globally as well but put the extern keyword specifier in front of everything:

    file1:
    Code:
    int myvar;
    file2:
    Code:
    extern int myvar;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    In the cpp file declare it normally. In the header file (which you include in all other files that wants to access it) you declare it using extern (see above post).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can I declare a variable with a scope that's wider than the "file" level?
    No, but you can include declarations in each file that you intend to use the variable and define it in a single location so that the linker continues to like you.

    >a friend of mine that used to be a professional c programmer weren't able to give me an answer
    That's odd. That's very odd. I would expect a professional to be familar with handling multiple file programs.
    My best code is written with the delete key.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Pfft. Who needs multiple files? It just takes me ten minutes to find the missing semicolon in my hundred-thousand lines of code.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  3. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. variables across files
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 07-23-2002, 10:58 AM