Share via

Linking in VS2017

MkJ 0 Reputation points
2026-04-17T18:17:33.07+00:00

I'm trying to use VS2017 and have this linking problem.

Declaring a C++ class in a header file and defining it in a source file generates the linker error 2019.

If the declaration and definition go into the header file, the linking takes place.

The compiler definitely compiles the source file because basic procedures in it execute when I move the whole class definition to the header file (I use printf() in a console application).

Is there a setting change I need to make?

Thank you.

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Taki Ly (WICLOUD CORPORATION) 1,500 Reputation points Microsoft External Staff Moderator
    2026-04-20T03:50:13.0566667+00:00

    Hello @MkJ ,

    According to Microsoft’s documentation, LNK2019: unresolved external symbol means the compiler accepted a declaration of a symbol, but during the link step the linker could not find the matching definition in any compiled object file (.obj) or linked library (.lib). There is no special Visual Studio setting required to put class declarations in a header (.h) and definitions in a source file (.cpp). If you get LNK2019 when the definition is in the .cpp, it could be these below:

    • The .cpp file that contains the definitions is not being compiled/linked into the final build.
    • The definition in the .cpp does not exactly match the declaration in the header.

    Also, moving the definitions into the header doesn’t prove the original .cpp was compiled, when the definition is in the header, it gets compiled into whatever .cpp includes that header.

    I suggest you check the following:

    1. Make sure the .cpp is included in the project:
    • In Solution Explorer, confirm the .cpp file that contains the class definitions appears under Source Files of the same project that builds the .exe.
    • If the file exists on disk but isn’t added to the project, it won’t be compiled.
    1. Make sure the .cpp is not excluded from the build:
    • Right‑click the .cpp -> Properties
    • Configuration Properties -> General
      • Item Type = C/C++ Compiler
      • Excluded From Build = No
    • Verify this for the active Configuration/Platform (Debug vs Release, Win32 vs x64).
    1. If you have multiple projects (EXE + library), link the right one
    • If the class is defined in a different project (static lib/DLL), your EXE project must add a Project Reference (or link the correct .lib).
    1. Verify declaration == definition:
    • Missing const, wrong namespace, different parameter types, calling convention, etc.

    Example mismatch that causes LNK2019:

    // header
    void SayHello() const;
    
    // cpp (wrong: missing const)
    void MyClass::SayHello() { }
    

    I hope this helps clarify your question. If you found my response helpful, you could follow this guide to provide feedback.

    Thank you.

    Was this answer helpful?


  2. Q&A Assist
    2026-04-17T18:17:58.8933333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Error LNK2019 means the linker sees a reference to a function or variable but cannot find its definition in any object file or library.

    For a class declared in a header and defined in a .cpp file, common causes are:

    1. The .cpp file that contains the class member definitions is not being compiled
      • In Solution Explorer, right‑click the .cpp file → Properties.
      • Under Configuration Properties → General, ensure Item Type is C/C++ Compiler (not “Does not participate in build”).
      • Build and then check the intermediate output folder for a corresponding .obj file.
    2. The object file or library is not being linked
      • If the class implementation is in a separate project or library, ensure the project is added as a project‑to‑project reference in VS2017 (Project → Add Reference… → Projects).
      • If using a .lib, make sure it is listed under Linker → Input → Additional Dependencies, and its path is in Linker → General → Additional Library Directories.
    3. Declaration/definition mismatch
      • Verify that the function signatures in the header and the definitions in the .cpp file match exactly (name, parameter types, const‑ness, namespaces, templates, calling convention, etc.).
      • For member functions, definitions must be fully qualified, for example:
             // MyClass.h
             class MyClass {
             public:
                 void foo(int x);
             };
             
             // MyClass.cpp
             #include "MyClass.h"
             void MyClass::foo(int x) { /* ... */ }
        
    4. A function or variable is declared but never defined
      • Ensure every non‑inline member function that is declared in the header has a corresponding definition in some .cpp file.
    5. Architecture or calling‑convention mismatch
      • Make sure all projects/libraries are built for the same platform (all x86 or all x64).
      • Ensure any explicit calling conventions (e.g. __stdcall, __cdecl) match between declaration and definition.

    If moving the entire class definition into the header makes the error disappear, that strongly indicates either the .cpp file is not part of the build, or the member function definitions in the .cpp do not exactly match the declarations in the header.


    References:

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.