Linux C + + Compiler error: "Multiple definition of"/"does not name a type"

Source: Internet
Author: User

When you recently compiled the Larbin_daemon crawler server-side hypervisor, you will always report the following error:

g++-c-o editconf.o editconf.cppeditconf.cpp:49:8: error:redefinition of ' std::string ProjectG ' editConf.h:34:8: Error : ' std::string PROJECTG ' previously declared hereeditconf.cpp:50:8: Error:redefinition of ' std::string IpG ' editConf.h : 35:8: Error: ' std::string IpG ' previously declared hereeditconf.cpp:51:8: Error:redefinition of ' std::string HttpPortG ' Editconf.h:36:8: Error: ' std::string HTTPPORTG ' previously declared hereeditconf.cpp:52:8: Error:redefinition of ' std:: String PAGESCONNEXIONSG ' Editconf.h:37:8: Error: ' std::string PAGESCONNEXIONSG ' previously declared HereeditConf.cpp : 53:8: Error:redefinition of ' std::string dnsconnexionsg ' editconf.h:38:8: Error: ' std::string dnsconnexionsg ' Previously declared hereeditconf.cpp:54:8: Error:redefinition of ' std::string depthinsiteg ' editConf.h:39:8: Error: ' std::string Depthinsiteg ' previously declared hereeditconf.cpp:55:8: Error:redefinition of ' std::string Waitdurationg ' Editconf.h:40:8: Error: ' std::string waitdurationg ' Previously declared Hereeditconf.cpp:56:8: error:redefinition of ' std::string timeoutpageg ' editConf.h:41:8: Error: ' std:: String Timeoutpageg ' previously declared hereeditconf.cpp:57:8: Error:redefinition of ' std::string TIMEOUTINCRG ' Editconf.h:42:8: Error: ' std::string TIMEOUTINCRG ' previously declared heremake: * * * [EDITCONF.O] Error 1

Very confused, clearly defined only once!

In fact

In EditConf.h, there is the following code snippet:

String projectg;//g represents global, and the class also has a corresponding private variable string ipg;string httpportg;string pagesconnexionsg;string dnsconnexionsg; String depthinsiteg;string waitdurationg;string timeoutpageg;string TIMEOUTINCRG;

It is then used directly in the EditConf.cpp (to assign something) such as:

HTTPPORTG = r[6];

The editConf.h header file is included in both EditConf.cpp and Main.cpp, which is where the problem lies.


When you declare a variable in the header file, As I wrote in editConf.h, each source file that contains the header file, whether direct or indirect, obtains a copy of the variable itself, so when you link all the. o files together, linker will assume that the variable is instantiated in multiple. o files.

The workaround is to instantiate the variable in the header file (such as editConf.h) with extern, and in the corresponding source file (such as EditConf.cpp).


Resources:

Http://stackoverflow.com/questions/18763270/c-linking-error-with-multiple-definition-on-linux

Linux C + + Compiler error: "Multiple definition of"/"does not name a type"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.