Click to See Complete Forum and Search --> : Declaring a global variable


spetsacdc
March 2nd, 2008, 01:44 AM
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)':project_4.cpp:(.text+0xd4): undefined reference to `findnumber()'
collect2: ld returned 1 exit status


Any ideas?


Thanks

laserlight
March 2nd, 2008, 01:47 AM
Why do you need a global variable?

Paul McKenzie
March 2nd, 2008, 01:48 AM
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

spetsacdc
March 2nd, 2008, 10:34 AM
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

Doron Moraz
March 2nd, 2008, 12:32 PM
Take a look at the following link:

http://www.cs.bris.ac.uk/Teaching/Resources/COMSM1401/pm_lectures/Paul/exercises/compile_make_intro.html

Regards
Doron Moraz

spetsacdc
March 2nd, 2008, 12:54 PM
ok thanks