LNK2019: Calling a function in WinMain [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I've searched everywhere and can't seem to find the answer. Nobody has made anything like this apparently. I'm trying to get input from a user.
while(!done)
{
PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE);
if (msg.message == WM_QUIT) {
done = true; //if found, quit app
} else if (msg.message == WM_KEYDOWN) {
game->KeyDown(msg.wParam);
} else if (msg.message == WM_KEYUP) {
game->KeyUp(msg.wParam);
} else {
/* Translate and dispatch to event queue*/
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
When I run it it gives me the error:
error LNK2019: unresolved external symbol "public: void __thiscall Game::KeyDown(unsigned int &)" (?KeyDown#Game##QAEXAAI#Z) referenced in function _WinMain#16
I have everything defined in the header of the game class... It's subsystem is set to nothing, but the "(/SUBSYSTEM:WINDOWS)" works..

Piece of cake. "unresolved external symbol" most commonly means you declared a function without specifying what it does. Consider the following example:
void undefined();
void LNK2019()
{
undefined(); //the offending line
}
void main()
{
//doesn't even have to do anything; it's not a runtime error
}
The function LNK2019() calls undefined(), but you never told undefined() what to do. Your linker doesn't know what to make of the line. In your specific case, I'm betting Game::KeyDown doesn't have a body.
A few other comments on your code:
For Windows messages, avoid if-else trees; use switch-case. It's commonly faster, and definitely more readable.
I've never seen messages handled in a loop like that. Your Windows Procedure (whose function is commonly shortened to WndProc) should handle all Windows messages - the entire purpose of DispatchMessage is to send messages to that function.

Related

DirectInput Unresolved External Symbol

This one is driving me nuts. I've tried everything I can think of. Here are the relevant parts of the DirectInput code.
BOOL CALLBACK EnumDevicesCallback(const DIDEVICEINSTANCE* DeviceInfo, VOID* Context);
if(DirectInput8Interface == DI_OK)
{
DirectInput8InterfacePointer->EnumDevices(
DI8DEVCLASS_GAMECTRL,
(LPDIENUMDEVICESCALLBACKA) EnumDevicesCallback,
NULL,
DIEDFL_ATTACHEDONLY);
}
When I try to compile, I get the error:
unresolved external symbol "int __stdcall EnumDevicesCallback(struct
DIDEVICEINSTANCEA const *,void *)"
(?EnumDevicesCallback##YGHPBUDIDEVICEINSTANCEA##PAX#Z) referenced in
function _WinMain#16.
As you can see, the external symbol the compiler can't find is related to the DIDEVICEINSTANCE parameter of the EnumDevicesCallback function. That shouldn't be, because I've included dinput.h and linked to dinput8.lib and dxguid.lib. I even tried defining DIDEVICEINSTANCE in my own code and got a message that it conflicted with a previous definition.
What could that error message mean?
That is not how callbacks work.
EnumDevicsCallback is not a function that exists. You're supposed to write your own function that EnumDevices will call for each device. Your function doesn't have to be called EnumDevicesCallback - that's an example.
For example, if you just wanted to print the name of each device, you might write
BOOL CALLBACK PrintDevicesCallback(const DIDEVICEINSTANCE* DeviceInfo, VOID* Context)
{
_tprintf("%s %s\n", DeviceInfo->tszProductName, DeviceInfo->tszProductName);
return DIENUM_CONTINUE;
}
and then pass PrintDevicesCallback to EnumDevices.
I dare say, the culprit is BOOL CALLBACK EnumDevicesCallback(const DIDEVICEINSTANCE* DeviceInfo, VOID* Context); - the function is likely a C-function, but you declare it in your .cpp file as C++-function.
Instead of doing that, include a proper .h file with the declaration, which is likely to have correct extern specified.

CPP error LNK2019: unresolved external symbol cpp [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall Sounds::soundBox(void)" (?soundBox#Sounds##QAEXXZ) referenced in function _main
For some reason i get this error, and i rly dont know what i did wrong.
Got wimm.lib added playsound works when called from main()
When i try to call it from class in playsound.cpp it calls error...
playsounds.h
#pragma once
#include <Windows.h>
class Sounds
{
public:
Sounds();
~Sounds();
void soundBox();
};
playsound.cpp
#include "playsound.h"
Sounds::Sounds()
{
}
void soundBox()
{
PlaySound(TEXT("fx/boom1.wav"), NULL, SND_FILENAME);
}
Sounds::~Sounds()
{
}
main.cpp
#include <iostream>
#include <conio.h>
#include "playsound.h"
int main()
{
Sounds newsound;
newsound.soundBox();
_getch();
}
You need to change the function definition in playsound.cpp
void soundBox()
To
void Sounds::soundBox()
This is because the function exists within the scope of the Sounds class, so you have to define it as such. Otherwise it would be a free function, and the version of the function in your Sounds class would be undefined (which is what the error is telling you).

How to fix LNK2019 unresolved external symbol

I have a very simple threading class I'm using for a project. I've just begin progress on it but I'm stuck because of a LNK2019 error I can't figure out how to fix. I've narrowed down the problem to a single line. Perhaps someone can help guide me on what I need to do to fix it.
The following is the class I'm making:
#ifndef __THREADING_H
#define __THREADING_H
#include <Windows.h>
class Threading {
public:
virtual void run() = 0;
void start();
void stop();
bool isStopped();
void cleanup();
private:
bool stopped;
HANDLE reference;
static DWORD WINAPI start_helperfunction(LPVOID ptr);
};
#endif // __THREADING_H
And the line where I'm getting the error is by 2nd line of the start_helperfunction, with the Threading::start_helperfunction below:
#include "Threading.h"
void Threading::start()
{
stopped = false;
reference = CreateThread(NULL, 0, Threading::start_helperfunction, this, NULL, NULL);
}
Finally the error message I'm getting is:
error LNK2019: unresolved external symbol "private: static unsigned long __stdcall Threading::start_helperfunction(void *)" (?start_helperfunction#Threading##CGKPAX#Z) referenced in function "public: void __thiscall Threading::start(void)" (?start#Threading##QAEXXZ)
I'm not sure what I've done wrong or what to try. I'm sure it's a simple fix. I'm not the most experienced in C++.
You didn't implement start_helperfunction, so the linker can't find it. You need to actually write a static member function with that name. The simplest possible one might look like this:
DWORD WINAPI Threading::start_helperfunction(LPVOID ptr)
{
return 0;
}

error LNK2019: unresolved external symbol "" referenced in function

Im currently getting the following error when I compile my code:
error LNK2019: unresolved external symbol "public: void __thiscall Agent::printSelf(void)" (?printSelf#Agent##QAEXXZ) referenced in function "public: void __thiscall World::processMouse(int,int,int,int)" (?processMouse#World##QAEXHHHH#Z) World.obj
Here is my code
Agent.h:
class Agent
{
public:
Agent();
void printSelf();
Agent.cpp:
void Agent::printSelf()
{
printf("Agent species=%i\n", species);
for (int i=0;i<mutations.size();i++) {
cout << mutations[i];
}
}
GLView.cpp:
void GLView::processMouse(int button, int state, int x, int y)
{
if(world->isDebug()) printf("MOUSE EVENT: button=%i state=%i x=%i y=%i\n", button, state, x, y);
if(button==0){
int wx= (int) ((x-conf::WWIDTH/2)/scalemult-xtranslate);
int wy= (int) ((y-conf::WHEIGHT/2)/scalemult-ytranslate);
world->processMouse(button, state, wx, wy);
}
mousex=x; mousey=y;
downb[button]=1-state;
}
void World::processMouse(int button, int state, int x, int y)
{
if (state==0) {
float mind=1e10;
float mini=-1;
float d;
for (int i=0;i<agents.size();i++) {
d= pow(x-agents[i].pos.x,2)+pow(y-agents[i].pos.y,2);
if (d<mind) {
mind=d;
mini=i;
}
}
if (mind<1000) {
//toggle selection of this agent
for (int i=0;i<agents.size();i++) {
if(i!=mini) agents[i].selectflag=false;
}
agents[mini].selectflag= !agents[mini].selectflag;
agents[mini].printSelf();
setControl(false);
}
}
}
Im pretty stumped. I haven't worked on this code in a long time, so im not sure what has changed to cause this. Anyone see anything wrong?
Most likely cause is that you're not linking in the object created from Agent.cpp.
You should check to ensure that it's part of the project and that you're using the correct version, compiled with this current compiler as well (since you state you haven't touched it in a while, it may be that the objects were built with an earlier compiler version, potentially making them incompatible - different name mangling methods, for example).
The first thing to try (once you've established all correct files are in the project) is a full clean-and-build.
On a few other points:
The error is occurring in World::processMouse meaning that the source for GLView::processMouse is probably irrelevant.
I find your mixing of printf and cout slightly ... disturbing. You should probably avoid printf for serious C++ programming. It works, but it's mostly intended for legacy C support.
Observed same issue in Visual studio 2008.
Clean, followed by Rebuild worked for me.

error LNK2001: unresolved external symbol _D3DX10CreateTextureFromFileW#24

I am trying to call a direct X funciton but I get the following error
error LNK2001: unresolved external symbol _D3DX10CreateTextureFromFileW#24
I understand that possibly there maybe a linker issue. But I am not sure where. I inluded both d3dx10.h and the d3d10.h. I also included the d3d10.lib file. Plus, the intellisense picks up on the method as well. below is my code. The method is D3DX10CreateTextureFromFile
bool MyGame::InitDirect3D()
{
if(!DX3dApp::InitDirect3D())
{
return false;
}
ID3D10Resource* pD3D10Resource = NULL;
HRESULT hr = D3DX10CreateTextureFromFile(mpD3DDevice,
L"C:\\delete.jpg",
NULL,
NULL,
&pD3D10Resource,
NULL);
if(FAILED(hr))
{
return false;
}
return true;
}
Is the appropriate DirectX library (or libraries) configured inthe project or makefile (or whatever build system is being used)?
For this particular function , it's D3DX10.lib.
You need D3DX10.lib: http://msdn.microsoft.com/en-us/library/bb172671%28VS.85%29.aspx