multiple definition of foo, first defined here

This is a discussion on multiple definition of foo, first defined here within the C++ Programming forums, part of the General Programming Boards category; Hi, I am using C++, but a lot of the code is c-style (not explicitly using classes and such). In ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    multiple definition of foo, first defined here

    Hi,
    I am using C++, but a lot of the code is c-style (not explicitly using classes and such).

    In tx_mpi_client.cpp, I have at the top of the file,

    Code:
    int myrank, num_ranks;
    in tx_client.h, I include tx_mpi_client.h

    in tx_client.cpp I have

    Code:
    int myrank, num_ranks;
    also at the top of the file, before any functions.

    Why is it telling me that myrank and num_ranks were already defined? They aren't static variables, so they shouldn't be accessible to anything else?

    Very confused.

    Here are the errors:

    tx_mpi_client.o.bss+0x0): multiple definition of `myrank'
    tx_client.o.bss+0x0): first defined here
    tx_mpi_client.o.bss+0x4): multiple definition of `num_ranks'
    tx_client.o.bss+0x4): first defined here

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because they aren't static variables, they are accessible to anything (and everything) else.

    Did you intend to have one set of globals? If so, you should make one of them extern.

    Did you intend to have two completely different set of variables at file scope? If so, mark them static (which will close them off to other translation units).

    (Also, I am going to assume you have a good reason for doing things this way.)

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,163
    Those two lines are outside any function, each line is a definition. Assuming tx_client.cpp #include's tx_client.h, the compiler, when compiling tx_client.cpp, sees both lines, so sees two definitions. Then compiler dutifully complains because it is required to enforce the "one definition rule": No variable is allowed to be defined more than once, according to the standard.

    If your purpose in having the line in tx_client.h is to share the two variables as globals between source files, then add "extern" before the line in the header. If some other source file (say foo.cpp) #include's tx_client.h, then make sure there is only one definition in your project. In other words, if tx_client.cpp is in your project, don't have a definition in foo.cpp

    If you intend to only use the variables in tx_client.cpp (i.e. it is local to that file) don't put the definition into the header. Optionally, mark them as static like tabstop mentioned. In C++, it is sometimes considered better to place them inside an unnamed namespace though.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    245
    I only declare the variables in the .cpp files, not the .h files.

    Quote Originally Posted by grumpy View Post
    Those two lines are outside any function, each line is a definition. Assuming tx_client.cpp #include's tx_client.h, the compiler, when compiling tx_client.cpp, sees both lines, so sees two definitions. Then compiler dutifully complains because it is required to enforce the "one definition rule": No variable is allowed to be defined more than once, according to the standard.

    If your purpose in having the line in tx_client.h is to share the two variables as globals between source files, then add "extern" before the line in the header. If some other source file (say foo.cpp) #include's tx_client.h, then make sure there is only one definition in your project. In other words, if tx_client.cpp is in your project, don't have a definition in foo.cpp

    If you intend to only use the variables in tx_client.cpp (i.e. it is local to that file) don't put the definition into the header. Optionally, mark them as static like tabstop mentioned. In C++, it is sometimes considered better to place them inside an unnamed namespace though.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by dayalsoap
    I only declare the variables in the .cpp files, not the .h files.
    In this case, by declare you presumably also mean define. Hence, this is a problem: you should only define the variables in a single .cpp file, not in "the .cpp files". If you need to access the variables from other source files, you should declare them as extern in those other source files, or perhaps in a header that is included in those other source files.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    245
    Right, I don't want them to be accessible to other files. Each one has its own myrank and num_ranks, etc.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by dayalsoap
    Right, I don't want them to be accessible to other files. Each one has its own myrank and num_ranks, etc.
    In this case another of tabstop's suggestions applies:
    Quote Originally Posted by tabstop
    Did you intend to have two completely different set of variables at file scope? If so, mark them static (which will close them off to other translation units).
    However, in C++ we tend to prefer the use of an unnamed namespace instead, e.g.,
    Code:
    namespace
    {
        int myrank, num_ranks;
    }
    That said, why do you need to do this? Why are these variables not local variables, or perhaps encapsulated within a class?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 03:15 AM
  2. multiple definition??
    By coni in forum C Programming
    Replies: 4
    Last Post: 09-11-2009, 07:55 PM
  3. Multiple Definition
    By Finfarfin in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2006, 03:18 PM
  4. Multiple Definition problems
    By shiver in forum C++ Programming
    Replies: 12
    Last Post: 08-30-2006, 02:33 PM
  5. Multiple definition error
    By gustavosserra in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2003, 07:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21