Thread: Linker Error

  1. #1
    Registered User
    Join Date
    Mar 2006

    Linker Error


    I've got the usual Visual C++ MFC program going. It's got a main dialog, with two (not-main?) dialogs that are called with DoModal().

    The thing is that those two dialogs need the same files, which have the same functions and classes.

    Here's an example of what I mean

    #include <windows.h>
    class MyClass
    	int a;
    	int b;
    	char c;
    void doSomething()
    	//code to do something in here
    int DoSomethingAsWell ( int i )
    	//Do something here
    And then the header for both of the *.cpp files that apply to the DoModal dialogs looks like this:
    #include "MyClass.cpp"
    #include "Functions.cpp"
    MyClass mc;
    This code results in a bunch of linker errors (LNK2005, and LNK1169 for the last error). It's telling me the functions already exist in the other dialogs .obj file, however, if I don't include the file in the *.cpp file, it says that the functions/classes don't exist. So, it's either it doesn't exist, or it's already been defined!

    Here's the actual link error:
    Quote Originally Posted by Micro$oft
    1>MacroCfgDlg.obj : error LNK2005: "class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > __cdecl to_vkey(int)" (?to_vkey@@YA?AV?$CStringT@_WV?$StrTraitMFC@_WV?$C hTraitsCRT@_W@ATL@@@@@ATL@@H@Z) already defined in AntiRecoilCfgDlg.obj
    1>MacroCfgDlg.obj : error LNK2005: "unsigned long __stdcall getkey(void *)" (?getkey@@YGKPAX@Z) already defined in AntiRecoilCfgDlg.obj
    1>MacroCfgDlg.obj : error LNK2005: "void __cdecl set_key(struct HWND__ *,int &)" (?set_key@@YAXPAUHWND__@@AAH@Z) already defined in AntiRecoilCfgDlg.obj
    1>E:\Programming\Visual Studio 2008\Quick Keys\Debug\Quick Keys.exe : fatal error LNK1169: one or more multiply defined symbols found
    If you guies need the actual source code I'd be happy to post that as well.

    Thanks in advance,


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    #include "MyClass.cpp"
    #include "Functions.cpp"
    DO NOT do this!!!
    Each including file makes a copy of the code, and if you do it more than once, you end up with "multiply declared" symbols.

    You can include .h files in other .h files
    Looks like you need to include MyClass.h and Functions.h here.

    Implementation files (that's the .cpp files) go into the "project->settings Add source files..."
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM