Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Multiple difinitions of global variables

Reply
Thread Tools

Re: Multiple difinitions of global variables

 
 
Jack Klein
Guest
Posts: n/a
 
      08-21-2003
On 20 Aug 2003 16:17:17 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Morning
River) wrote in comp.lang.c++:

> I am working on a project to port some C++ code from UNIX to Windows and
> having some problems to get them built on windows. One problem is about the
> multiple difinitions of global variables. In UNIX, most compilers support
> the multiple difinitions.


The code is wrong and the compilers that accept it are wrong. C++ has
a "one definition" rule. C has the same requirement, although it is
worded differently.

> While on windows, I got the following errors from Visual C++ 6.0:
> HeaderA.obj : error LNK2005: "int length" (?length@@3HA) already defined in
> vctest.obj
> Debug/AnotherVCTest.exe : fatal error LNK1169: one or more multiply defined
> symbols found
> Error executing link.exe.
>
> Can anyone tell me how to get around this problem?


Fix the code. Make it legal and correct C++ and it will compile
everywhere.

> Code:
> file HeaderA.h:
> #ifndef _HEADER_A
> #define _HEADER_A


Fix this as well, this code illegally invades the implementation's
namespace. All symbols beginning with an underscore followed by an
upper-case letter, or containing two consecutive underscores anywhere
within them, are reserved for the implementation and not to be defined
in user programs.

> int length;
>
> int getLength();
> void setLength(int len);
>
> #endif
>
> file HeaderA.cpp:
> #include "HeaderA.h"
>
> void setLength(int len) {
> length = len;
> }
>
> int getLength() {
> return length;
> }
>
> file vctest.cpp:
> // vctest.cpp : Defines the entry point for the console application.
> //
> #include "HeaderA.h"
> #include <stdio.h>
>
> int main(int argc, char* argv[])
> {
>
> printf("Hello World!\n");
> printf("len=%d\n", length);
> setLength(10);
> printf("len = %d\n", getLength());
> printf("len = %d\n", length);
> return 0;
> }
>
> Thanks,
>
> James


You are not going to get Visual C++, or a good many other compilers,
to accept this code as written.

Put external declarations for these variables in a header file.
Include that header in all source files that reference them. In one
and only one source file, write the definitions for these variables.
Include the header that declares them in the source file that defines
them.

Or even better, redesign the code to eliminate the global variables.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Re: Multiple difinitions of global variables Erik C++ 1 08-21-2003 04:59 PM



Advertisments