CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2008
    Posts
    17

    Question Declaring a global variable

    Hello, I need to make a global variable for a number. I have a function that calculates this number. This function has its own .h and .cpp files.

    In my main program .cpp file I have:

    #include "findnumber.h"
    double theglobalvar = findnumber();
    int main(){ .... etc ....

    My problem is I don't know how to use the function to declare my global variable to be the return value of the function.

    It doesn't like me calling the findnumber function outside of main. The compiler says:

    In function `__static_initialization_and_destruction_0(int, int)'roject_4.cpp.text+0xd4): undefined reference to `findnumber()'
    collect2: ld returned 1 exit status


    Any ideas?


    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Declaring a global variable

    Why do you need a global variable?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Declaring a global variable

    Quote Originally Posted by spetsacdc
    It doesn't like me calling the findnumber function outside of main. The compiler says:
    That is not a compiler error. That is a linker error. The error is telling you that the "findnumber" function was called, but it doesn't exist anywhere.

    So where is the "findnumber" function? Declaring it is not enough, it has to actually exist if you're going to call that function in your program.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Mar 2008
    Posts
    17

    Re: Declaring a global variable

    Oh ok sorry stupid mistake, I forgot to add findnumber.cpp to the end of my compile statement....

    g++ -o program2 program2.cpp findnumber.cpp


    Oh, and what does a makefile do? I think it is something to compile programs that have many different .h and .cpp source files, but I don't see why it is needed since that line above does it.


    Thanks

  5. #5
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: Declaring a global variable

    Take a look at the following link:

    http://www.cs.bris.ac.uk/Teaching/Re...ake_intro.html

    Regards
    Doron Moraz

  6. #6
    Join Date
    Mar 2008
    Posts
    17

    Re: Declaring a global variable

    ok thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured