C++ Error: LINK2005
高洛峰
高洛峰 2017-05-16 13:23:40
0
1
623
  1. Problem Description
    common.h Defines all type aliases and structure declarations, which are mass_diffusion.h and heat_conduction.h is referenced, mass_diffusion.h and heat_conduction.h are referenced by man.cpp, and then the compilation is successful, but an error is reported when linkingLINK2005. I have used the defensive syntax #ifndef ... #define ... #endif; each file has only one init function exposed to the outside. Although there are functions with the same name inside the file, Why would they report an error if they were not exposed to the outside world?

  2. Error message
    Visual studio 2017 compiles C++ error: LNK2005 "void __cdecl compute(class std::vector<struct user , class std::allocator<struct user > > ,class std::vector<struct item ,class std::allocator<struct item > > ) "Already defined in heat_conduction.obj.

  3. Related codes

    //common.h
    #ifndef _COMMON_H
    #define _COMMON_H
    ...
    struct user;
    struct item;
    struct relation;
    ...
    typedef unsigned int _container_number_;
    typedef unsigned int _value_number_;
    ...
    typedef std::vector<user*> user_container;
    typedef std:: vector<item*> item_container;
    #endif // !_COMMON_H
    ...
    //mass_diffusion.h
    #include "common.h"
    extern void init_md(user_container, item_container);
    void compute(user_container _ulist, item_container _ilist);
    ...
    //mass_diffusion. cpp
    #include "mass_diffusion.h"
    void init_md(user_container _ulist, item_container _ilist){...};
    void compute(user_container _ulist, item_container _ilist) {...};
    ...
    //heat_conduction.h
    #include "common.h"
    extern void init_hc(user_container _ulist, item_container _ilist);
    void compute(user_container _ulist, item_container _ilist);
    ...
    //heat_conduction.cpp
    #include "heat_conduction.h"
    void init_hc(user_container _ulist, item_container _ilist){...};
    void compute(user_container _ulist, item_container _ilist) {...};
    ...
    //main.cpp
    #include "mass_diffusion.h"
    #include "heat_conduction.h"

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
PHPzhong

An error will be reported originally. Each of your modules is linked to the same file. The linker will naturally be confused when it sees that there are so many symbols with the same name. The linker will not care about anything exposed in .h. The solution is to make each module independent. Compile it into so or dll, and then call it, or use a namespace

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template