Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Where to declare global variables?

Where to declare global variables?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KaKa
    wrote on last edited by
    #1

    I have a C++ project with multiple .h and .cpp files and with a .cpp file containing the int main() function. Where do I declare global variables such that they can be accessed by the methods in the various .cpp files? I declared the variable before the main() function but there is an "undeclared variable" error. I tried creating a .h file to store the global variable and added the line: #include "globals.h" in the necessary .h files but there were all sorts of link errors.

    J A M 3 Replies Last reply
    0
    • K KaKa

      I have a C++ project with multiple .h and .cpp files and with a .cpp file containing the int main() function. Where do I declare global variables such that they can be accessed by the methods in the various .cpp files? I declared the variable before the main() function but there is an "undeclared variable" error. I tried creating a .h file to store the global variable and added the line: #include "globals.h" in the necessary .h files but there were all sorts of link errors.

      J Offline
      J Offline
      john5632
      wrote on last edited by
      #2

      Declare in cpp file where it first time used.And for another use use make it extern

      1 Reply Last reply
      0
      • K KaKa

        I have a C++ project with multiple .h and .cpp files and with a .cpp file containing the int main() function. Where do I declare global variables such that they can be accessed by the methods in the various .cpp files? I declared the variable before the main() function but there is an "undeclared variable" error. I tried creating a .h file to store the global variable and added the line: #include "globals.h" in the necessary .h files but there were all sorts of link errors.

        A Offline
        A Offline
        Arman S
        wrote on last edited by
        #3

        The terms declare and define need to be clarified. A [global] variable should be defined once but may be declared many times. The extern keyword garrantees that a variable is declared but not defined. You could define your global variables somewhere in a source (.cpp) file and then extern (declare) them in a header file (e.g globals.h). So you only need to include that header in any file to access the globals.

        -- ===== Arman

        1 Reply Last reply
        0
        • K KaKa

          I have a C++ project with multiple .h and .cpp files and with a .cpp file containing the int main() function. Where do I declare global variables such that they can be accessed by the methods in the various .cpp files? I declared the variable before the main() function but there is an "undeclared variable" error. I tried creating a .h file to store the global variable and added the line: #include "globals.h" in the necessary .h files but there were all sorts of link errors.

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          See the FAQ 6.2 How do I share a global variable among my .CPP files?[^]

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups