Search code examples
c++linkerlinker-errorslnk2019

LNK2019 and LNK1120 with functions in an external file


I have taken out some functions from a source file into another since I want to use them also in other files. The current structure is as follows

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);
}

Problem is, that when I try to compile this, the linker complains and says that the symbol someFunction could not be resolved.


Solution

  • someFunction is declared inline, so it must be defined in your header file:

    utils/extFuncs.h
    
    #ifndef _extFuncs_h
    #define _extFuncs_h
    inline int someFunction (float v)
    {
        return 42;
    }
    #endif