Re: Multiple difinitions of global variables

Discussion in 'C++' started by Jack Klein, Aug 21, 2003.

  1. Jack Klein

    Jack Klein Guest

    On 20 Aug 2003 16:17:17 -0700, (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
     
    Jack Klein, Aug 21, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne
    Replies:
    2
    Views:
    500
    Wayne
    Nov 11, 2003
  2. Erik
    Replies:
    1
    Views:
    474
  3. jubelbrus
    Replies:
    5
    Views:
    645
    JohnQ
    Jul 20, 2007
  4. mark4asp
    Replies:
    1
    Views:
    236
  5. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    241
    Tony Archer
    Nov 25, 2003
Loading...

Share This Page