Multiple definition of a const char*
You use wrong declaration for your string. You need to make your string a constant, since constants may be defined in several compilation units. This is why compiler does not report the same error for BUFFSIZE
: BUFFSIZE
is const, so it may be defined several times in different compilation units. But HOST_NAME
is not const, so it is reported. HOST_NAME
will be const if you change its declaration to
const char* const HOST_NAME = "127.0.0.1";
Then the error should disappear.
[C++11: 3.5/3]:
A name having namespace scope (3.3.6) has internal linkage if it is the name of
- a variable, function or function template that is explicitly declared
static
; or,- a variable that is explicitly declared
const
orconstexpr
and neither explicitly declaredextern
nor previously declared to have external linkage; or- a data member of an anonymous union.
This effectively makes the constant "local" to each translation unit in which it is defined, removing the opportunity for conflict.
You have included "connection.hpp" to both connection.cpp and main.cpp. Therefore it (const char* HOST_NAME = "127.0.0.1";
) is defined in 2 cpp files.
don't think that I have compiled some files twice
Nevertheless that's exactly what happened. You have compiled connection.hpp
several times, each time you have # include
d it into some translation unit.
Either add static
to the declaration, or add extern
to it, delete the = somestring
portion, and provide a definition in exactly one source file.