Search code examples
c++visual-studiovisual-studio-2017lnk2019

Visual Studio throwing LNK2019 and solution missing headers/source files


-- Updates --

  1. Adding the source file settings.cpp directly to the project via Add -> Existing Item resolved the LNK2019 (as I suspected it couldnt find it).

-- Updated Question--

  1. How to specify a source file directory without having to add all the files within it manually as described in the update above? This is clearly achievable for the header files (as described below by adding the directory to the settings, is this not the same for source files?

-- Original Question --

I'm working on replicating a project from CPython into C++ as a challenge to learn more C++ but I can't seem to get the environment established so that I can compile a test run. When I build the solution, it throws a LNK2019 which I know has something to do with the Linker unable to locate the symbols. I have read through numerous solutions on SO that say to updated the properties with the directories of the files, but they do not resolve the issue.

The issues currently stand at:

  1. Some of the headers from other directories show in the explorer, but some do not, why?
  2. The source files are not found and therefore LNK2019 is thrown, cannot resolve, how to?

Here is the layout of my project:

/root
    /proj-cmd
        /src/main/cpp
            /proj/cmd
                -> main.cpp
    /proj-core
        /src/main/cpp/
            /proj/cmd
                -> command.h
                -> base_command.h
                -> base_command.cpp
            /proj/utils
                -> settings.h
                -> settings.cpp
 

The content of main.cpp for testing of environment:

// astro
#include <astro/core/util/settings.h>

// stdlib
#include <exception>
#include <string>
#include <iostream>
    
using namespace std;

// astro entry point
int 
main(int argc, char *argv[])
{   

    if (conf().hasKey("APP_CWD"))
    {
        cout << "APP_CWD is: " << conf().getKey("APP_CWD") << endl;
    }
    else
    {
        cout << "APP_CWD was not found" << endl;
    }

}

In order for #include <astro/core/util/settings.h> to work, I updated the include directories in the properties:

Project Settings

However, in the explorer only command.h and settings.h are shown, not base_command.h:

Explorer

Additionally, the base_command.cpp and settings.cpp do not display in the source files either, so I updated (similar to the include directories) the source directories:

ProjectSettings2

That takes care of the first issue I am noticing, but now onto LNK2019. I believe this is a related result of the former problem, in that the source files are unknown:

Errors

So following many other SO posts, I tried to update the Linker settings without success:

LinkerSettings

I'm not very familiar with the Visual Studio 2017 environment, so if somebody could provide input as to how to configure these settings so that I can compile I'd appreciate this.


Solution

  • You need to add all .cpp files to your project as Existing Items. Just being in a directory is not sufficient for the IDE to know to compile those files. Headers are found by directory via #include, but you should still add them to your project as Existing Items to make it easier to navigate them in the tree view.

    This is why you are getting linker errors: the code in settings.cpp and base_command.cpp are never getting built.

    See Microsoft Docs