Already defined function error LNK2005
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Already defined function error LNK2005

  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    232

    Already defined function error LNK2005

    I created a very simple c++ win32 project in MSVC 2010.

    I added a .cpp file with the function definition and a .h file with the function prototype and I get this "function already defined" error.

    I included both .h and .cpp file to the corresponding stdafx.h and stdafx.cpp files.
    I really dont see what can be the problem. I did exactly as on the video tutorial where it was working.

    Player.cpp
    Code:
    #include "stdafx.h"
    
    void f()
    {
        MessageBox(0,0,0,0);
    }
    Player.h
    Code:
    void f();
    1>stdafx.obj : error LNK2005: "void __cdecl f(void)" (?f@@YAXXZ) already defined in Player.obj
    1>D:\DIRECTSHOW\MediaPlayer\Debug\MediaPlayer.exe : fatal error LNK1169: one or more multiply defined symbols found
    Last edited by MasterDucky; July 9th, 2014 at 07:11 AM.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,738

    Re: Already defined function error LNK2005

    What is the code in stdafx.h and stdafx.cpp? It seems that f() is defined in both stdafx.cpp and player.cpp.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    232

    Re: Already defined function error LNK2005

    Its apparently not defined there and even if I change the function name I get the same error.


    Code:
    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    
    #pragma once
    
    #include "targetver.h"
    
    #define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
    // Windows Header Files:
    #include <windows.h>
    
    // C RunTime Header Files
    #include <stdlib.h>
    #include <malloc.h>
    #include <memory.h>
    
    // TODO: reference additional headers your program requires here
    
    #include "Player.h"
    Code:
    // stdafx.cpp : source file that includes just the standard includes
    // MediaPlayer.pch will be the pre-compiled header
    // stdafx.obj will contain the pre-compiled type information
    
    #include "stdafx.h"
    
    // TODO: reference any additional headers you need in STDAFX.H
    // and not in this file
    
    #include "Player.cpp"
    Last edited by MasterDucky; July 9th, 2014 at 11:12 AM.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,738

    Re: Already defined function error LNK2005

    In stdafx.cpp

    Code:
    #include "Player.cpp"
    You don't usually include a .cpp file. Try commenting out this line.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2007
    Location
    France
    Posts
    232

    Re: Already defined function error LNK2005

    That was it! Thanks a lot, you made my day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center