Thread: multiple definition of foo, first defined here

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

    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,336
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    269
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 04: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, 08:33 PM