LNK2005 error redefinition

Posted on 2006-04-25
Last Modified: 2012-06-27
I'm getting a LNK2005 error due to redefinition of some functions.  The project has a main (test.cpp), two classes (myclass1.h, myclass1.cpp, myclass2.h, myclass2.cpp), and another header file (morefuncs.h) that is not a class but contains some functions.

I've used #pragma once in all header files.

The test.cpp #includes both header files for the classes.

If I include the header file for the additional functions, #include morefuncs.h into only one of the class header files, everything is fine.  However if I try to include morefuncs.h into the second class, I get the above linker redefinition error.  I'm assuming this is becasue the morefuncs is not a class but simply a header file that contains functions.

I'd rather not make morefuncs a class. Is there a way to include it into multiple other classes without a redefinition problem?
Question by:JohnSantaFe
    1 Comment
    LVL 48

    Accepted Solution

    #pragma once prevents h-file to be included twice to the same source file. But it doesn't prevent h-file to be included number of times to different source files in the project. If h-file contains function implementations, this causes linker error.
    You have one the following options:
    1) Move function implementations to additional cpp file and leave only prototypes in h-file
    2) Declare all functions in h-file as inline
    3) Declare all functions in h-file as static
    4) Divide h-file on two parts: declaration and implementation:

    // declaration part
    // function prototypes

    // function bodies

    Include this h-file to number of source files, and define only once IMPLEMENTATION:

    #define IMPLEMENTATION    // only in one source file
    #include "MyFile.h"

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    improve Shell script 5 117
    java constructor error 8 65
    strCopies  challenge 17 60
    delphi parse string to params 3 49
    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    This is about my first experience with programming Arduino.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now