Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple definition of a const char*

I get the above message linker error for a global

const char* HOST_NAME = "127.0.0.1";

I don't think that I have compiled some files twice but here's my definition of the files anyway.

main.cpp

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include "connection.hpp"

connection.cpp

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "connection.hpp"

connection.hpp

#ifndef __connection__
#define __connection__
#include <unistd.h>
#include <netinet/in.h>

const int BUFFSIZE = sysconf(_SC_PAGESIZE);             //Define page size
const char* HOST_NAME = "127.0.0.1";                    //Local host
//Definition of a class
#endif

Any help?

like image 838
Billy Grande Avatar asked Jul 03 '14 15:07

Billy Grande


People also ask

What is a const char * in C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

What is meant by const char *?

const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to char* is deprecated.

Can a char * be passed as const * argument?

In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's ...

What is a static const char * in C++?

const char* is a pointer to a constant char, meaning the char in question can't be modified. char* const is a constant pointer to a char, meaning the char can be modified, but the pointer can not (e.g. you can't make it point somewhere else).


3 Answers

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 or constexpr and neither explicitly declared extern 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.

like image 80
Wojtek Surowka Avatar answered Oct 08 '22 11:10

Wojtek Surowka


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 # included 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.

like image 41
n. 1.8e9-where's-my-share m. Avatar answered Oct 08 '22 10:10

n. 1.8e9-where's-my-share m.


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.

like image 28
Sujith Gunawardhane Avatar answered Oct 08 '22 10:10

Sujith Gunawardhane