I've been having some problems with declaring functions inline causing unresolved external reference linker errors. I must be misunderstanding something quirky about C++. I'm trying to reduce the compile time of my C++ SDK using a 3-file translation unit where there is one "codeless header" that has only declarations and no implementations, another "code header" that contains all of the templates with implementations, and a unique .cpp filename to minimize hashtable collisions. I'm trying to make either a statically compiled library, DLL, or compile directly into an executable. I want my functions to be inlined, but the problem is that this super basic code will not compile:
// in pch.h
#include <iostream>
#ifdef ASSEMBLE_DYNAMIC_LIB
#ifdef LIB_EXPORT
#define LIB_MEMBER __declspec(dllexport)
#else
#define LIB_MEMBER __declspec(dllimport)
#endif
#else
#define LIB_MEMBER
#endif
// in foo.h
#pragma once
#include "pch.h"
struct LIB_MEMBER Foo {
Foo ();
inline int Bar (); //< inline causes Unresolved external reference error???
};
// in foo.cpp
#include "foo.h"
Foo::Foo () {}
int Foo::Bar()
// main.cpp
#include "foo.h"
int main(int argv, char** args) {
Foo foo;
std::cout << "Hello StackOverflow. foo is " << foo.Bar();
while (1)
;
}
The code results in this linker error:
Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: int __cdecl Foo::Bar(void)" (?Bar@Foo@@QEAAHXZ) referenced in function main experiments C:\workspace\kabuki_toolkit\projects\experiments\main.obj 1
All of the code I've found on StackOverflow won't compile with the same error. For example:
// in foo.cpp
#include "foo.h"
Foo::Foo () {}
inline int Foo::Bar() {} //< Again, Unresolved external reference error :-(
The Visual-C++ documetnation has some stuff about how to inline a DLL class member, but they have no code examples.