C++ Project randomly has 4 errors (unresolved externals)

Gavin Williams 761 Reputation points
2021-06-05T09:51:19.107+00:00

There is nothing wrong with my project but VS randomly gives errors:

1) LNK1120  3 unresolved externals .. PlayerDB.Console.exe  1

2) LNK2019  unresolved external symbol "public: void __cdecl Application::Start(void)" (?Start@Application@@QEAAXXZ) referenced in function main    ..\main.obj 1

3) LNK2019  unresolved external symbol "public: __cdecl Application::Application(void)" (??0Application@@QEAA@XZ) referenced in function main   ..\main.obj 1

4) LNK2019  unresolved external symbol "public: __cdecl Application::~Application(void)" (??1Application@@QEAA@XZ) referenced in function main  ..\main.obj 1

These keep popping up again and again, several times today alone. If I clean the solution the project builds and runs. How can I prevent this from happening or is it a bug in VS?

Developer technologies C++
{count} vote

5 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,546 Reputation points
    2021-06-05T21:27:05.52+00:00

    Your program is using a class that looks something like the following.

    class Application {
        public: void __cdecl Application::Start(void) ...
        public: __cdecl Application::Application(void) ...
        public: __cdecl Application::~Application(void) ...
    }
    

    The class is in a file that is not always compiled because Visual Studio does not know that it needs to. When you clean the project Visual Studio knows it needs to. So you need to determine why. We need more information to help you with that.

    0 comments No comments

  2. Gavin Williams 761 Reputation points
    2021-06-06T04:24:27.893+00:00

    This is the Application.h file...

    #pragma once
    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    #include <typeinfo>
    #include <iostream>
    #include "PlayerDB.h"
    #include "Command.h"
    #include "Enumerations.h"
    
    using namespace std;
    
    class Application
    {
    public:
     Application();
     ~Application();
     void Start();
     void Exit();
    private:
     PlayerDB* playerDB;
     List<string*>* operationalMsgs;
     void RenderHeader();
     void RenderOperationalMessages();
     void RenderDataView();
     void RenderPrompt();
     Command* GetCommand();
     void ToLowerCase(string* text);
     HANDLE consoleH = GetStdHandle(STD_OUTPUT_HANDLE);
     DataViewMode dataViewMode;
    };
    
    • Its File Type is a C++ Header File
    • Application.h properties has:
      • Excluded From build = Empty
      • Content = Empty
      • Item Type = C/C++ compiler

    And if I make an edit to Application::Start() method, and run, the program is built and run correctly with the edit being apparent at runtime - so Visual Studio is at least sometimes building the project.

    If there is any logic to how VS is working - I would suggest that such logic is at least unhelpful if not antagonistic.


  3. Guido Franzke 2,191 Reputation points
    2021-06-07T06:26:23.593+00:00

    Hello,
    you asked this question too: lnk4042-object-specified-more-than-once-extras-ign.html
    It looks like the questions belong to the same project. Did you check if there are two source files named Application.cpp ?
    Maybe you should rename the files for your Application.h and Application.cpp with your code of class Application.
    Regards, Guido

    0 comments No comments

  4. Gavin Williams 761 Reputation points
    2021-06-10T13:01:54.073+00:00

    So this issue happened again today. I'll put up the project here and I've removed vs folder. If anyone wants to take a look, that would be appreciated. Just build and the 3 errors will show up.

    PS. I can't attach a zip! That makes it a bit hard.

    Here's a OneDrive share..

    https://1drv.ms/u/s!AhbcRtciy6_0jcoR1zcefgToZrpUlA?e=GyzCkt


  5. Viorel 122.5K Reputation points
    2021-06-11T14:57:40.017+00:00

    • Its File Type is a C++ Header File
    • Item Type = C/C++ compiler

    Try changing Item Type of all .h files to “C/C++ header”, because in case of “C/C++ compiler” it will try to generate an Application.obj file from Application.cpp, then another one from Application.h, or vice versa. After this adjustments, which must be done for all of configurations and platform, the occasional linker errors should not appear. (Your log files contain “warning LNK4042: object specified more than once; extras ignored”, which denotes duplicate .obj files).

    Then solve all of compiler errors. It seems that you have List<Player> and List<string>. Then it does not have sense to allocate Player and string using ‘new’. Or you can define a List<Player*>. The Add and Insert functions and their parameters can be adjusted to work in any case. But this is a different topic.


Your answer

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