Visual Studio Linker problems

All,

Happy New Year to all my fellow developers. The holidays are a great time to write code with everyone taking time off, it is really quiet and allows for many uninterrupted hours.

My question is hopefully pretty simple. I wrote a routine in one C++ file and I decided to break it up into some smaller more manageable C++ files. When I copied the variables out of the first one and into the new smaller file (same solution), everything compiles fine but I get a ton of linker errors saying the variables have already been defined in another (the previous) file.

Using Visual Studio 2008. Suggestions?

Thanks,
Bryan
i´m not sure i understood what you are trying to do. do you want to spread a single function over several cpp or h files? as far as i know that is not common practice.
you need to use the extern keyword when defining global variables.

extern [type] [identifier]

header file
1
2
//....
extern int gSolution;


implementation file
 
int gSolution = 10;


this tells the compiler that there is an integer variable somehwere and to look for it when linking.
Last edited on
Thanks for the responses, let me clarify. These are not global variables. I am simply making two separate routines (c++ files) from one to make the code less cumbersome and more modular. In making two routines, I have to take some variables out of the original and move them to the new one.

It would be as if you had a program, say program X that had 4 variables in it where two of the variables did one thing like B=A+4 and D=C+5. Now I want to make another program (Y) and take C & D out of X and put them into Y. Compiles fine but linker says C & D already defined in program X. Of course I'm dealing with a lot more variables resulting in a lot of the same linker errors. See below.

Thanks much.
Bryan

>PageComputeGeoInverse.obj : error LNK2005: "double EllipsoidSemiMinor" (?EllipsoidSemiMinor@@3NA) already defined in Frontier Geodetic.obj
5>PageComputeGeoInverse.obj : error LNK2005: "double EllipsoidAverage" (?EllipsoidAverage@@3NA) already defined in Frontier Geodetic.obj
5>PageComputeGeoInverse.obj : error LNK2005: "double FlateningInverse" (?FlateningInverse@@3NA) already defined in Frontier Geodetic.obj
5>PageComputeGeoInverse.obj : error LNK2005: "double EllipsoidSemiMajor" (?EllipsoidSemiMajor@@3NA) already defined in Frontier Geodetic.obj
5>PageComputeGeoInverse.obj : error LNK2005: "double Flatening" (?Flatening@@3NA) already defined in Frontier Geodetic.obj
5>UITask_ComputeGeoInverse.obj : error LNK2005: "double EllipsoidSemiMinor" (?EllipsoidSemiMinor@@3NA) already defined in Frontier Geodetic.obj
5>UITask_ComputeGeoInverse.obj : error LNK2005: "double EllipsoidAverage" (?EllipsoidAverage@@3NA) already defined in Frontier Geodetic.obj
5>UITask_ComputeGeoInverse.obj : error LNK2005: "double FlateningInverse" (?FlateningInverse@@3NA) already defined in Frontier Geodetic.obj
5>UITask_ComputeGeoInverse.obj : error LNK2005: "double EllipsoidSemiMajor" (?EllipsoidSemiMajor@@3NA) already defined in Frontier Geodetic.obj
5>UITask_ComputeGeoInverse.obj : error LNK2005: "double Flatening" (?Flatening@@3NA) already defined in Frontier Geodetic.obj
5>UITask_GeoInverse.obj : error LNK2005: "double EllipsoidSemiMinor" (?EllipsoidSemiMinor@@3NA) already defined in Frontier Geodetic.obj
5>UITask_GeoInverse.obj : error LNK2005: "double EllipsoidAverage" (?EllipsoidAverage@@3NA) already defined in Frontier Geodetic.obj
5>UITask_GeoInverse.obj : error LNK2005: "double FlateningInverse" (?FlateningInverse@@3NA) already defined in Frontier Geodetic.obj
5>UITask_GeoInverse.obj : error LNK2005: "double EllipsoidSemiMajor" (?EllipsoidSemiMajor@@3NA) already defined in Frontier Geodetic.obj
5>UITask_GeoInverse.obj : error LNK2005: "double Flatening" (?Flatening@@3NA) already defined in Frontier Geodetic.obj
5>CalcGeoInverse.obj : error LNK2005: "double EllipsoidSemiMinor" (?EllipsoidSemiMinor@@3NA) already defined in Frontier Geodetic.obj
5>CalcGeoInverse.obj : error LNK2005: "double EllipsoidAverage" (?EllipsoidAverage@@3NA) already defined in Frontier Geodetic.obj
5>CalcGeoInverse.obj : error LNK2005: "double FlateningInverse" (?FlateningInverse@@3NA) already defined in Frontier Geodetic.obj
5>CalcGeoInverse.obj : error LNK2005: "double EllipsoidSemiMajor" (?EllipsoidSemiMajor@@3NA) already defined in Frontier Geodetic.obj
5>CalcGeoInverse.obj : error LNK2005: "double Flatening" (?Flatening@@3NA) already defined in Frontier Geodetic.obj
5> Creating library c:\Users\Bryan\Documents\Visual Studio 2008\Projects\Frontier Geodetic\Frontier Geodetic\Win32\Debug\Frontier Geodetic.lib and object c:\Users\Bryan\Documents\Visual Studio 2008\Projects\Frontier Geodetic\Frontier Geodetic\Win32\Debug\Frontier Geodetic.exp
5>c:\Users\Bryan\Documents\Visual Studio 2008\Projects\Frontier Geodetic\Frontier Geodetic\Win32\Debug\Frontier Geodetic.dll : fatal error LNK1169: one or more multiply defined symbols found
5>Build log was saved at "file://c:\Users\Bryan\Documents\Visual Studio 2008\Projects\Frontier Geodetic\Frontier Geodetic\Win32\Debug\BuildLog.htm"
5>Frontier Geodetic - 21 error(s), 0 warning(s)
=
but you are passing the variables as function parameters then? like in

--------file 1--------------

void somefunction(int& a)
{

}

---------------file 2----------------
extern void somefunction(int& a)

void otherfunction(int &b)
{
int pepe = 5;
somefunction(pepe);
}


anyway, what pogrady said also goes for functions in separate files- you must use the extern keyword to define in file A the functions that are used in file A but that are not there, for example

extern void somefunction(int& someparameter);
Please post your actual code, so we don't have to guess about your problem.

You should not need to add the extern keyword to your function prototypes.
Topic archived. No new replies allowed.