C++ Multiple Definitions of a Variable

Multiple definition of a global variable

Because with every include in a implementation file file, a new instance of your struct is created (and stored in the object file).

To avoid this, just declare the struct as "extern" in the header file and initialize it in the implementation file:

// In your header file: 
extern const struct dsm_config DEFAULT_DSM_CONFIG;

// In your *.c file:
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };

This will solve your problem.

c++ multiple definitions of a variable

I'm not going to include all of the details, but you define a global variable, wat twice in your compilation uint.

To fix, use the following:

FileB.h

extern int wat;

FileB.cpp

int wat = 0;

This (extern) tells the compile that the variable wat exists somewhere, and that it needs to find it on it's own (in this case, it's in FileB.cpp)

Multiple definitions of a global variable

A global variable can have any number of declarations, but only one definition. An initializer makes it a definition, so it will complain about having two of those (even if they agree).

Multiple definition error on variable that is declared and defined in header file and used only in its cpp file

If you declare your variable in the header file:

#ifndef GLOBAL_H
#define GLOBAL_H

int foo = 0;

#endif

In every include of your header file or translation unit, a new instance of your integer is created. As you mentioned, to avoid this, you need to declare the item as "extern" in the header file and initialize it in the implementation file:

// .h
extern int foo;

// .cpp
int foo = 0

A more C++ way to do that can be something like this:

#ifndef GLOBAL_H
#define GLOBAL_H

struct Global {
static int foo;
};
#endif

And in your cpp file:

#include "variables.h"

int Global::foo = 0;

C++17 fixes this problem with inline variables, so you can do:

#ifndef GLOBAL_H
#define GLOBAL_H

inline int foo = 0;

#endif

See How do inline variables work? for more information.

Multiple definition of object file

There is no such thing as a "local global variable" in C. myHandler is a global variable, defined in both source files. That is invalid, since each global variable can only have one definition.

If you want each source file to have its own file-local myHandler variable, you must declare it static:

static MyHandler_t myHandler =
{
.a = 0,
};

Note that this way, code in other source files cannot access that variable by name.

Multiple definition of variables ESP32

(Taking and extending comment by Some programmer dude, to get this question out of the list of unanswered questions. I am confident he does not mind, but offer to delete otherwise.)

You define the variables in the header file; note the absence of the extern keyword.
Here:

const char *TAG = "wifi_test";
const int CONNECTED_BIT = BIT0;

That means it will be defined in each translation unit (simplified: code file) where that header file gets included.

I.e. you get one defintion for each including code file, i.e. more than one, i.e. multiple definitions. That is what the linker is telling you with the error message you quoted.

Instead declare the variable in the header file (using the extern keyword and without initialization), i.e.:

extern const char *TAG;
extern const int CONNECTED_BIT;

then, in only a single source file, define (and initialize) the variable, i.e.:

/* only in one .c, not in a .h, that is the difference */
const char *TAG = "wifi_test";
const int CONNECTED_BIT = BIT0;

That unique code file part makes the single definition, no multiples, keeping the linker happy.

Meanwhile the declaration in a header makes the identifiers and their types visible/known to the compiler when compiling all other code files. So that accessing them becomes possible. The accessing code created by the compiler (using placeholders) is then made executable and so that it accesses always the same thing from all code files, by the linker, who fills in the placeholders.

A side note on reinclusion guards.

With

#ifndef WIFI_TEST_H
#define WIFI_TEST_H

The define sets the macro which was checked in the line before, effectively preventing the same code from being compiled again, but only as long as the definition is known. That in turn is however only applicable within the same code file.

The purpose is to avoid redoing the same thing twice within one code file, which can happen if this header is included e.g. indirectly by a header it includes. (This special case would additionally create a circular include...)

This does however NOT prevent the header from being included into the next code file, because the compiler does not know about macros being defined in other code files (or in headers included elsewhere). And it should not prevent that, because the declarations (not the definitions) are needed in all code fils which access the variables.

c++ compiler error multiple definition of a variable

Change the header file to

#ifndef FINDAVERAGE_H
#define FINDAVERAGE_H
int find_average();
// No - wrong place double first_no;
// No - wrong place double second_no;
#endif

as the variables will be included where ever the header file is in a .cpp file

and change the function to this

int find_average(){
double first_no;
double second_no;

std::cout << "Enter a number"<<endl;
std::cin>>first_no;
std::cout<<"Enter another number"<<endl;
std::cin>>second_no;
return (first_no+second_no)/2;
};

Should add error checking in case the user does not enter a number - but I leave that to the reader



Related Topics



Leave a reply



Submit