1
votes

I have one header file and two .cpp files.
E.g. normal.h, normal.cpp, abnormal.cpp.
I have included the header files in both the .cpp files.
Now I have an external header, say external.h. I included external.h in normal.h and abnormal.cpp.
But I am getting link errors. Kindly help me on how to include the external header file.

This is my normal.h code

#pragma once
#include<iostream>
#include<external.hpp>

class normal
{
    public:
           normal();
           ~normal();
           void function();
           void Function();
 
};

normal.cpp code

#include"normal.h"
normal::normal(){
}
normal::~normal(){
}

abnormal.cpp code

#include"normal.h"
#include<external.hpp>
void normal::function()
{
 //external library's supported function call
}

Errors Severity Code Description Project File Line Suppression State

Error LNK2005 "public: virtual void __cdecl external::tools::Predicate_ite::reset(void)" (?reset@Predicate_ite@tools@aff3ct@@UEAAXXZ) already defined in abnormal.obj trial3 F:\trial3\trial3\normal.obj 1

Error LNK2005 "public: virtual bool __cdecl external::tools::Predicate_ite::operator()(void)" (??RPredicate_ite@tools@aff3ct@@UEAA_NXZ) already defined in abnormal.obj trial3 F:\trial3\trial3\normal.obj 1

Error LNK2005 "public: __cdecl external::tools::Predicate_ite::Predicate_ite(int)" (??0Predicate_ite@tools@aff3ct@@QEAA@H@Z) already defined in abnormal.obj trial3 F:\trial3\trial3\normal.obj 1

Error LNK1169 one or more multiply defined symbols found trial3 F:\trial3\x64\Release\trial3.dll 1

1
There is nothing wrong about what you describe. You need to show to allow finding a problem. Please read How to Ask and provide a minimal reproducible example. - Yunnosch
Check the path of the external.h. - ytlu
"I am getting link errors" - If you're getting link errors, you have missed to link with a library or object file an it has nothing to do with the inclusion of header files. Please include the exact link error in the question. - Ted Lyngmo
Exact error message please. What you need to do depends on the actual error you are seeing (amazingly enough). It may not be an error with the header files. - john
What you likely need are Header Guards to prevent multiple inclusions of the same header file. Search Header Guards. - David C. Rankin

1 Answers

2
votes

Including headers into more than one code file is not a prolem.
There is nothing abnormal about a second code file including the same header.

Linker problems might arise from doing one of the following:

  • including headers which contain code instead of only declarations, typedefs, macro definitions and implementations of templated classes
  • the same applies to including code files
  • including the same header more than once into the same code file (though that is not a problem in many cases); the adverse effect of this can be avoided with "header guards" also known as "reinclusion guards" (#ifndef HEADERNAME_H #define HEADERNAME_H /* content */ #endif /* HEADERNAME_H */ ), credits to David C. Ranking for bringing this up; a special case of this is to include the header twice by once including direclty and once indirectly via another header (which seems to be what you worry about)

Linker problems can also occur unrelated to header including, if the smybols are only declared but not defined, e.g. when the corresponding libraries are not correctly added to the project (thanks to Ted Lyngmo for bringing that up).

Other problems than linker problems might arise from:

  • includiong headers with conflicting/contradictory content
  • circular inclusions
  • unsolved dependencies caused by including headers in the wrong order which wrongly have a dependency on their order