Hey,

I have a program that has 5 files, 2 header files called:

console.h
screen.h

and 3 source files called:

a2main.cpp
screen.cpp
console.c    //not a typo

When I copy the main from my a2main.cpp file to my screen.cpp my program compiles without a problem. But when I try and compile it using the external main I get the following errors.

screen.obj : error LNK2005: _line already defined in a2main.obj
screen.obj : error LNK2005: _cursor_pos already defined in a2main.obj
screen.obj : error LNK2005: _col already defined in a2main.obj
a2main.exe : fatal error LNK1169: one or more multiply defined symbols found

Could someone please explain why I am getting these errors.
Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were declared in console.c then redeclared in screen.cpp.

I have used:

extern "C" {
   #include "console.h" 
}

in both my a2main.cpp and screen.cpp as the objects and functions in them are called by the main.

Thanks.

Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were declared in console.c then redeclared in screen.cpp.
.

The above should be:
Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were defined in console.c then redefined in screen.cpp, but declared in console.h

The most common reason for those duplicate declaration errors is declaring variables in header files without the extern keyword. extern tells the compiler that the object is actually declared in some other *.cpp or *.c file.

Here is an example of how you must declare variables in header files

// consol.h
#ifdef _cplusplus
extern "C" {
#endif
extern int _line;
extern int _cursor_pos;
extern int _col;
#ifdef _cplusplus
};
#endif

Now, in one and only one *.cpp file you have to declare them without the extern keyword

//console.c 
#ifdef _cplusplus
extern "C" {
#endif
int _line = 0;
int _cursor_pos = 0;
int _col = 0;
#ifdef _cplusplus
};
#endif

>>Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were defined in console.c then redefined in screen.cpp,

You can't declare the same variables in two or more *.cpp files. Declare them in only one *.cpp file and use the extern keyword to declare them on other files. See example in my previous post.

Some compilers will discard unused variables during the optimization process, but apparently yours does not. In the case of your program I doubt any compiler would discard them because it doesn't know until link time whether the variables are actually used or not.

Why is it then that I'm able to compile the program when my main is a part of the screen.cpp file instead of the main.cpp?

you would have to post all that code -- it shouldn't matter what *.cpp file contains main().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.