CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    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 06:11 AM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    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 10:12 AM.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    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
  •  





Click Here to Expand Forum to Full Width

Featured