代码之家  ›  专栏  ›  技术社区  ›  Etan bbum

LNK2019和LNK1120,具有外部文件中的函数

  •  0
  • Etan bbum  · 技术社区  · 15 年前

    utils/extFuncs.h

    #ifndef _extFuncs_h
    #define _extFuncs_h
    inline int someFunction (float v);
    #endif
    

    utils/extFuncs.cpp

    #include "utils/extFuncs.h"
    inline int someFunction (float v) {
        return 42;
    }
    

    foo/bar.h

    #ifndef _bar_h
    #define _bar_h
    #include "utils/extFuncs.h"
    class Bar {
    public:
        Bar (float x);
    };
    #endif
    

    foo/bar.cpp

    #include "foo/bar.h"
    Bar::Bar (float x) {
        int y = someFunction(x);
    }
    

    问题是,当我试图编译这个时,链接器会抱怨并说 someFunction 无法解决。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Frédéric Hamidi    15 年前

    someFunction 被宣布 inline 必须 在头文件中定义:

    utils/extFuncs.h
    
    #ifndef _extFuncs_h
    #define _extFuncs_h
    inline int someFunction (float v)
    {
        return 42;
    }
    #endif
    
        2
  •  0
  •   wengseng    15 年前

    是否尝试将extFunction.h&extFunction.cpp添加到项目工作区中?