简体   繁体   中英

Multiple Definitions of… | C++

This is a somewhat theoretical question...

I have been reading up on linking object files. Now I get error messages about multiple definitions of some functions. I believe to have identified the issue as follows:

  • I have two separate header files A and B.

  • Both headers access the same header H.

  • From A and BI have compiled object files AO and BO.

  • When I try to compile a cpp-File M, that includes A and B by linking AO and BO to M, I get error messages about multiple definitions.

  • The function(s) that the error message(s) are about are located in H.

I assume the issue is, that in both AO and BO is somehow references or compiled code to/ from H, so that it appears twice when I link AO ad BO when compiling M. Have I recognized the issue correctly, or am I likely missing something ele?

And if so, how do you avoid or work around such issue?

Edit: I said I compiled header files. Sorry, that was sloppy way to say it. I compiled the cpp files with the implementations of the class methods in the header files A and B.

You should never compile .h (header) files. A header file is a C++ source file that is meant to be included in other C++ source files via an #include directive.

The only files you should be compiling are .cpp files, which are often referred to as implementation files .

It is also important that you not include function definitions in your header files. Each function definition must only be compiled once, and thus must be coded into exactly one implementation file. Header files should only contain:

  • forward declarations
  • macros
  • other #include s
  • types
  • externs
  • template code
  • function prototypes

The cause of your problem is likely that you have included a full function definition in your H file, and so, when you compile your A and B implementation files (they should be called implementation files if you're compiling them; alternatively, you can think about moving function definitions from those header files into separate implementation files, and only compiling those implementation files), the function is being compiled into both AO and BO object files. That is why linking fails; the functions are multiply defined at link-time.

To solve the problem, you need to move any function definitions that you have in the H header file into a separate implementation file. Since H does not appear to be tied to any single implementation file that you already have (I say this because it appears to be a dependency of both A and B), it probably makes most sense to move those function definitions into a new H.cpp implementation file. As part of your build process, you will then have to build H.cpp, and link that into the final executable at link-time.

Did you remember the header guards in your header files, otherwise you end up with this issue.

#ifndef MY_HEADER_H_INC
#define MY_HEADER_H_INC

/* Put Header Code Here */

#endif // MY_HEADER_H_INC

You end up including the same file multiple times, because as you stated you have header H included in both then you include each. This sounds like the most likely cause.

In C++, you should separate declarations from implementation in order to avoid redefinitions (as in the situation you have described). Put the declarations into a header file filename.h, with some sort of include guard inside (either #def-based or #pragma once); put your definitions in filename.cpp, which includes the header via #include "filename.h". When you compile, you only compile .cpp files; do not try to compile headers (unless you need a precompiled header, which you probably do not).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM