Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
I have mutiple header files and source files that my main code is using.I have declared my global variables with keyword extern in my header file,and then I have define these variables in my one of source file.Some of my source files are using these global variables.I have added header guard to all of my header file in the following way.

I have added at top of header file basic.h these commands
#ifndef BASIC_H
#define BASIC_H

and at end I have added.
#endif

I am using Visual c++ 2008.When I compile my program I get 6 different errors of unresolved external symbol

Error 4 error LNK2001: unresolved external symbol "int oilvariables" (?oilvariables@@3HA) basic.obj

I have also uploaded my project at mediafire at the following link.
http://www.mediafire.com/?whzjagw3waio9qi[^]
If anyone knows the answer please help me..........
Bundle of thanks in advance

I have also added my header and source files here.
UOV.cpp
#include "basic.h"
#include "matrixb.h"
using namespace std;
int main() {
	cout<<"\nOil Variables\n";
	cin>>oilvariables;
	cout<<"\nVineagar Variables\n";
	cin>>vinegarvariables;
return 0;
}


basic.h
#ifndef BASIC_H
#define BASIC_H

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include<fstream>
#include <direct.h>

extern int oilvariables;
extern int vinegarvariables;
extern int n;//Oilvariables+VinegarVariables
extern int D;  //number of quadratic terms in a public polynomial
#endif


basic.cpp
#include "basic.h"

int n=oilvariables+vinegarvariables;
int D=(n*(n+1))/2;


matrixb.h
#ifndef MATRIXB_H
#define MATRIXB_H

#include "basic.h"
extern int** B_LRS;//Matrix B generated by LRS

#endif


matrixb.cpp
#include "matrixB.h"



using namespace std;
int** B_LRS=0;
Posted
Updated 16-Mar-11 0:18am
v4
Comments
CPallini 16-Mar-11 5:25am    
Could you please post some relevant code here?
smishtiaqhussain 16-Mar-11 6:04am    
I have added the code
Richard MacCutchan 16-Mar-11 5:34am    
Your link links back to this page.
smishtiaqhussain 16-Mar-11 6:19am    
Thanks for ur quick reply.I have checked the link now its working.I have also pasted the code here.

First of all: DO NOT USE GLOBAL VARIABLES... Their "ease of use" just lead to tons of problems. Believe me, I maintained a sofware made by a previous guy in my company. He liked global variables and because of that it took me months to understand some strange bugs.

Even though you still want to use them:

- Make sure the extern keyword is used in the .h
- Make sure a local variable (in your file or function) has not the same name.
- Make sure the type of the variable in the .cpp matches the type of its extern declaration.
- Make sure the .cpp file that contains the variable is part of your project.

If this doesn't solve the problem. Give some code.

------

OK. You defined n and D in basic.cpp but you still need to define oilvariables and vinegarvariables: I don't see them in your code.

------

Your are taking the values from the user, but where the values will be kept? You need to define the variables like the other ones (in basic.cpp for example):
int oilvariables;
int vinegarvariables;
int n = ...;
int D = ...;



I will suggest one more time to not use global variables. You don't need them in your code. Use functions, parameters, and return values from your functions instead.
 
Share this answer
 
v5
Comments
smishtiaqhussain 16-Mar-11 6:03am    
I have added code in updated question
Thanks.
Olivier Levrey 16-Mar-11 6:18am    
I updated my answer.
smishtiaqhussain 16-Mar-11 6:21am    
I am taking oilvariables and vinegarvariables as input from the user.
Olivier Levrey 16-Mar-11 6:26am    
cout doesn't define the variable, it just gives a value to it. See my updated answer.
smishtiaqhussain 16-Mar-11 6:34am    
thanks for ur reply its working now.
But there is still one problem.
In basic.cpp I have defined
int n=oilvariables+vinegarvaribles .But it does add and it keeps value of n=0.Similarly for D.How
I can give value of n=oilvariables+vinegarvaribles.n and D are also global
The "unresolved external symbol" error is a linker error saying that you have referenced a variable or function that is not defined within the object files the linker is fed with. This error can pop up for a number of reasons, but most often it's because you have forgotten to define the variable or function.

In your case you need to define you variables like:
int oilvariables;
int vinegarvariables;
int n;
int D;

somewhere in one of your cpp files.

If you need to have access to global properties, I suggest you use a Singleton Pattern[^] to implement the functionality needed. A singleton class allows you to access one, and only one, instance of a class where you can put your properties, thus simulating 'global' behavior without the drawbacks of true global variables.
 
Share this answer
 
Do not use global variables. Do something like:

C++
using namespace std;
int main()
{
    int oilvariables = 0, vinegarvariables = 0;
    cout << "\nOil Variables\n";
    cin >> oilvariables;
    cout << "\nVineagar Variables\n";
    cin >> vinegarvariables;

    int n = oilvariables + vinegarvariables;
    int D = (n * (n + 1)) / 2;

    //now you can do whatever you want with your 4 LOCAL variables.
    ...

    return 0;
}
 
Share this answer
 
v2
Comments
smishtiaqhussain 16-Mar-11 6:47am    
Thanks a lot for ur help.But one question,as these are local variables,how can I access them in different cpp files.
Olivier Levrey 16-Mar-11 6:50am    
You won't access them. You will give them through function parameters. Or you will modify them by a return value given from a function for example. I suggest you read some tutorials about using functions. For example this one: http://www.cplusplus.com/doc/tutorial/functions/
smishtiaqhussain 16-Mar-11 6:52am    
Thanks a lot.So I will try to use them by functions.
Olivier Levrey 16-Mar-11 7:01am    
I am glad to hear that :)
Good luck.
smishtiaqhussain wrote:
I have define these variables in my one of source file

No you did't. That was the mistake.


smishtiaqhussainRakesh5 wrote:
#include "basic.h"
int n=oilvariables+vinegarvariables;
int D=(n*(n+1))/2;T

Change the above lines to
C++
#include "basic.h"
int oilvariables;
int vinegarvariables;
int n=oilvariables+vinegarvariables;
int D=(n*(n+1))/2;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900