none
error LNK2005: RRS feed

  • Question

  • Hi.

    I have the follwoing linking problem after changing the code from C++ to C
    but not in C++. How can I solve it?
    MathFunc.obj : error LNK2005: _M already defined in PoseEst.obj
    Thanks.
    Thursday, June 18, 2009 4:29 PM

Answers

  • The compiler needs to know the size at compile time. use #define to define the size instead.
    You can also write your own array, something like STL's vector.

    MSMVP VC++
    • Marked as answer by yade Thursday, June 18, 2009 6:23 PM
    Thursday, June 18, 2009 6:12 PM

All replies

  • Yade,

    I had the same problem.  I had a file that was included in two different class files.  The file being included was not a class, just a collection of miscellaneous functions and variables.  It would build OK if I only had functions in the include file.  But when I added a variable, I got the LNK2005 error.  To solve it I had to make the variable 'static'.  You might want to try this with you _M variable.
    Thursday, June 18, 2009 4:57 PM
  • If you have funtion that will be used in multiple source files, declare it as extern in a header file and put function body OUTSIDE the header file.
    MSMVP VC++
    Thursday, June 18, 2009 5:06 PM
  • Thanks HyperGamer.

    For my case I have defined M and N as :

    PoseEst.h
    const int M=6;         // number of equations  (number of rows) 
    const int N=4;         // number of points     (number of cloumn
    Then I have couple of C files which use M and N globally :

    Prog1.c
    #include "PoseEst.h"
    ...

    Prog2.c
    #include "PoseEst.h"
    ...

    Prog3.c
    #include "PoseEst.h"
    ...


    Are you sure making the varable static will be safe?

    I want to know conceptually how this problem is caused and what are the options to solve it (not to force it however).

    Thanks for your comments.

















    Thursday, June 18, 2009 5:08 PM
  • declare M as extern and define it in one of your source files.
    MSMVP VC++
    Thursday, June 18, 2009 5:16 PM
  • Sheng extern doesn't solve it and i get the same error but static does.

    However I haven't test the running results sice I have another function linking problem which I will refer to that in a different thread.

    What do you think?!
    Thursday, June 18, 2009 5:20 PM
  • You #include the variable definition in three .c files so you get three copies of the same-named variable.  You should not define variables in .h files.

    // .h file
    extern const int M;

    // one .c file
    const int M = 6;
    Thursday, June 18, 2009 5:22 PM
  • You WILL get LNK2005 if you declare variables in header file without extern and include the header file multiple times. Check the number 5 cause in LNK2005's documentation
    MSMVP VC++
    Thursday, June 18, 2009 5:23 PM
  • I used static, because I will use the file in the same manner for other projects.  So, instead of having to remember to define the variables in on the the .cpp files that include this miscellaneous file, I just defined it as static.  I, also, did not have to use 'extern'.  I use VC 2008, and neither  '#pragma once' nor '#ifndef ....   #endif' worked.  However, it appears that the correct way to do it is what Scott has given.
    Thursday, June 18, 2009 5:34 PM
  • Thanks for your response.

    Scott and Shneg I followed your suggestion and had it this way :


    // .h file
    extern const int M;

    // one .c file
    const int M = 6;


    But now it seems these varaibles are not known inother files as I get the error :
    error C2057: expected constant expression
    for

    double rtmp[N]={0};

    Is it something related to this or that is beacuse in C I cannot define the vectors as rtmp[N] and I need to have it rtmp[4];
    In c++ this problem does not occur.

    Looking forward for your suggestions.

    Thursday, June 18, 2009 5:38 PM
  • The compiler needs to know the size at compile time. use #define to define the size instead.
    You can also write your own array, something like STL's vector.

    MSMVP VC++
    • Marked as answer by yade Thursday, June 18, 2009 6:23 PM
    Thursday, June 18, 2009 6:12 PM