Unresolved external symbol after every change in code - c++

I've got a problem with my code. It basically looks like this:
someclass.hpp:
class SomeClass
{
public:
SomeClass();
~SomeClass();
//... some other methods
};
someclass.cpp:
#include "SomeClass.hpp"
SomeClass::SomeClass()
{
}
SomeClass::~SomeClass()
{
}
SomeClass.cpp doesn't include anything else.
The constructors are actually empty, too. I just don't want to leave them undefined or leave the standard constructor because I'm probably going to need it later.
SomeClass.hpp is actually a gamestate class which is included in only one place:
main.cpp
#include "SomeClass.hpp"
int main()
{
DoSomethingWithGamestate(new SomeClass());
return 0;
}
And then the project consists of a lot of other files, all of which are independent from SomeClass, though.
The problem is that whenever I change anything in the code, no matter in which file, I have to recompile the entire solution because if I only compile the changes the linker throws this:
error LNK2001: unresolved external symbol "public: __thiscall SomeClass::SomeClass(void) (??blablaSomeClassblabla)
Obviously, there's something weird going on here, since SomeClass() is clearly defined in someclass.cpp.
What could be the source of this?

In addition to what #Nawaz says, it's likely your build files are out of sync with the linker so that when Visual Studio goes to re-link, it can't because the files are out of sync. Just re-build and be done with it.
By the way, it may even be a bug in Visual Studio 2010 as seen here.

Related

VS 2022: "Unresolved external" error when calling methods that are defined outside of the class definition

So, I've tried to run the simplest C++ program imaginable in Visual Studio 2022:
main.cpp:
#include "TestClass.h"
int main() {
TestClass().testMethod();
}
TestClass.h:
#pragma once
class TestClass {
public:
void testMethod();
};
TestClass.cpp:
#include "TestClass.h"
inline void TestClass::testMethod() {
}
But for some reason, I get nothing but a linker error:
error LNK2019: unresolved external symbol "public: void __cdecl TestClass::testMethod(void)" (?testMethod#TestClass##QEAAXXZ) referenced in function main
I know that there are tons of questions on Stack Overflow discussing that specific error, but I wasn't able to find anything that applied to my situation, except for this one, which doesn't have an answer.
All files are included in the project (everything was generated in Visual Studio), I do not get any warnings from IntelliSense, and every file on its own compiles just fine (using Ctrl+F7)
I have no clue what is going on and would appreciate any help.
In C++ inline functions must have their body present in every translation unit from which they are called.
Removing inline doesn't change anything
Try to test it. I'm not the only one who proves that inline causes the lnk error. As
Sedenion says, it's a usage error.
I recommend reporting this wrong behavior to Developer Community and posting the link in this thread.

Using static library in current project

I'm facing the error when I'm linking an external library and code with the current project. You will understand the problem by this example.
//foo.h
namespace LMS
{
class foo
{
foo(); //implementation in cpp
foo(int dummy) //implemented here
{
//something
}
};
} // namespace LMS
//foo.cpp
#include"foo.h"
namespace LMS
{
foo::foo()
{
//something
}
} // namespace LMS
//externalProgram.h
#include "foo.h"
LMS::foo *ptr;
ptr = new LMS::foo(); //Linking error: LNK 2019
ptr = new LMS::foo(2); //No error
Now the problem is that the external program doesn't know about the foo.cpp and the implementation of class foo methods in it. I'm currently using VS2019 and using two projects in the same solution. I've tried several ways to correct this but it didn't work out. The current way I'm seeing is to implement all the functions in header files.
EDIT: I've already linked the files!!
You should be able to mark your externalProgram project as dependent on the foo project. This will link foo.o in with external program.
Using the UI you will select this in
Project > Project Dependencies : Depends on ...
If that is correct, then the problem is more subtle, sometimes just a simple typo. At this point you want to use the command line tools to break apart the library and confirm the object files, and break apart the object files and confirm the symbols within. A ten year old SO posting discusses using lib.exe to example the library file. The dumpbin tool has a /symbols option that might also be handy for looking at the actual code generated.

Including files from a seperate project in the same solution in Visual studio - LNK2001?

I had a solution named fun.sln with a project called fun.vcxproj.
I created a whole bunch of name spaces ready to be used.
I made another project called no_more_fun.vcxproj.
I added the includes directory for fun.vcxproj to the configuration of no_more_fun.vcxproj.
I added this to no_more_fun.cpp
#include "candy.h"
void main(void)
{
candy::get();
return;
}
candy.h is in the default directory for fun.vcxproj(which was added to the config)
But I get...
LNK2001 unresolved external symbol "int __cdecl candy::get(unsigned long)" (?get#candy##YAHK#Z) .....
Visual Studio shows no error before compiling.
The "candy" namespace works fine in the "fun" project so idn...
Is there a guide or something so that i can understand how i can go about sharing code efficiently among different projects within ONE solution?
This is a linker error. You didn't get any error at compile time, because the compiler found the candy::get() method in candy.h header, but the implementation (which I suppose is in a candy.cpp file) is not found by the linker.
Just add candy.cpp too to no_more_fun.vcxproj.
ps. I didn't observe but in the error message you can see that the function waits a parameter.
call it like this:
unsigned long foo = 0;
candy::get(foo);
This is going to sounds stupid but...i just dragged the files in to visual studio so that the no_more_fun project had the "files" in its "directory" too.
wow... I shouldn't need to do that...Am I wrong?(sarcasm).

Changes in my Header File break my linker LNK2019

Alright, so I'm trying to make a simple game engine using SFML in Visual Studio 2012. What's happening is, I have my header file, Game.hpp:
#ifndef _GAME_H
#define _GAME_H
#include<SFML\Graphics.hpp>
#include<iostream>
class Game
{
private:
sf::RenderWindow Window;
sf::Time ElapsedTime;
sf::Time PreviousTime;
float DeltaTime;
sf::Clock clock;
bool Running;
sf::Event polledEvent;
sf::RectangleShape rectshape;
sf::Font arialFont;
sf::Text testtext;
public:
Game();
int OnExecute();
public:
void Init();
void Update();
void Draw();
void Event(sf::Event polledEvent);
void Cleanup();
};
#endif
and my Game.cpp file for the constructor:
#include "Game.hpp"
Game::Game()
{
Window.create(sf::VideoMode(800,600),"Name Me!");
Window.setFramerateLimit(30);
Running = true;
}
Any changes in the slightest to Game.hpp break compilation, with the linker error:
error LNK2019: unresolved external symbol "public: __thiscall Game::Game(void)" (??0Game##QAE#XZ) referenced in function _main Program.obj SFML-Project
and the warning:
warning LNK4042: object specified more than once; extras ignored
After forcing it to alternately attempt to rebuild Game.hpp, then Game.cpp(by changing something minor, like adding and deleting a space), it compiles fine. Every single other function is in its own cpp file, and not caring. If Game.hpp doesn't need to be updated, the linker doesn't flag any errors. It is only these two that are fighting.
What causes this error and how to I fix it?
EDIT: I threw all of my implementation code into Game.cpp just to see if that'd do anything. Still the same issue. If I, for example, add a variable to my Game.hpp file, then reference it in a function in my Game.cpp file, it'll go through, build my main file, Game.cpp, and Game.hpp. It'll flag the error. I'll then go through, add a space and backspace, enough to flag the file as "changed," and hit build. It builds without errors.
As far as I can tell, my project is just broken beyond measure. My best guess is that I accidentally created "Game.hpp" as a cpp file, and then switched it to a header file, but it was still flagged for compilation, hence why it was continuously being compiled. I created a new project, and copy-pasted the code over, and everything seems to be working fine now. I tried a few little things here and there, but I can't seem to figure out what I did to the project. My issue is fixed, although I'd love to figure out why it happened in the first place.

Visual Studio 2012 Linking error

I'm facing a weird issue here. I have my VS2012 project all set up, working properly. But when i tried the simple task of adding a method to one of my classes, it won't link correctly, i get
error LNK2019: unresolved external symbol "public: void __thiscall Camera::calcularDirecao(class GLFWwindow *)" (?calcularDirecao#Camera##QAEXPAVGLFWwindow###Z)
Here's my Camera class:
class Camera {
public:
Camera() { ... inline constructor ... }
~Camera() {}
... other methods ( which link fine ) ...
void calcularDirecao(GLFWwindow *);
};
And in my implementation file i have
void Camera::calcularDirecao(GLFWwindow *janela) {
... code ...
}
... other methods ...
I already tried rebuilding and all, with no success. Thanks in advance.
Recreate your project from scratch, check if it works or not. After that compare your vcxproj files. They are text files. Any text compare tool will work. Their difference may tell a lot.
Also try to view your source file in a hex editor. Look for any non ASCII symbols in your troubled method and around. Such symbols might be not displayed in the IDE viewer but still may confuse the compiler.
I doubt that your GLFWwindow definition is getting overridden by some other file included in your .cpp.