Changes in my Header File break my linker LNK2019 - c++

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.

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.

"undefined reference to 'operator new(unsigned int)' and undefined reference to class functions (and constructor)

I'm attempting to write code for an ATxmega16E5 using Atmel Studio 7.
I've had a long search around the forums and cannot make head nor tail of the other suggestions that seem to be similar to my own issue.
(A lot of suggestions indicate to change compiling options, which I have no idea how to do this in Atmel Studio).
Basically, I've included my class GPP.h
BUT, when I try to instantiate a class in main.cpp it comes back as a series of undefined references to all functions including the constructor.
EDIT: Reduced the amount of included code to focus on the main issues at hand,
mainly, the files don't seem to be linking.
main.cpp :
//INCLUDES
#include "GPP.h"
//MAIN FUNCTION
int main()
{
GPP *gpp = new GPP();
//Turn on system power
gpp->setPowerPin();
gpp->screenOn();
// ...etc (just makes some function calls
// ...
while(1);
}
GPP.h:
#ifndef GPOWERPACK_H
#define GPOWERPACK_H
class GPP
{
public:
GPP();
//~GPP();
void setPowerPin();
void screenOn();
void SPI_Init();
void SPI_SendNext();
void displayLogo();
};
#endif
Any help would be fantastic to help grow my understanding of cpp.
I have coded in c and java previously.
Atmel Makefile is quite long, Could display sections on request?
For anyone using Atmel Studio for the first time.
And those who have simply forgotten the basics of makefiles.
I figured out a simple way to resolve my linker issue.
The error 'undefined reference' occurred because I had not included the files GPP.cpp and GPP.h in the project build.
The fix:
1) Open the "Solution Explorer"
2) Right click on your project, go to add -> existing item
3) select the files you are including/ files relevant to your project build and click
Apologies for the misguided question. Seems a simple mistake in hindsight

C++ Inclusion guards being "ignored" between projects

I have two projects; a static library and an executable (standard Win32 setup, using the multithreaded debug dll setting for both).
In my libray, I have a Globals.h, with the following code:
#ifndef _GLOBALS_H
#define _GLOBALS_H
#include <SDL.h>
#include <string>
namespace Eng
{
bool Run = true;
SDL_Window *Window = NULL;
SDL_GLContext GlContext;
Uint32 WindowFlags = SDL_WINDOW_OPENGL;
}
#endif
This is all fine and dandy, and within that project everything works fine. However, as soon as I include that file within more than one file in my executable project (all protected by inclusion guards), I start getting multiply defined symbol errors for each variable within Globals.h.
As well, I'm getting quite a few macro redefinition warnings from math.h (M_PI macro redefinition). I'm not sure if this is linked, but it seems likely due to the similar nature of error (guards defined in one project seemingly not applying in the other).
Anyone have any ideas how to resolve this? I feel like I'm missing some vitally important compiler setting somewhere :(

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.

Unresolved external symbol after every change in code

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.