LNK2019 & LNK1120 Unresolved Externals Probably an easy fix, but I'm having real trouble - lnk2019

I've been getting these LNK2019s for a little while now and can't seem to find a way to get rid of them. I'm aware that there are a lot of threads on these errors already, but I've yet to find anything that's helped me so hoped someone might miss something glaringly obvious I may have missed.
I've not learnt very traditionally, so sorry if my code's a bit messy.
main:
#include "eventLoop.h"
#include <time.h>
#include <iostream>
using namespace std;
bool buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG = false;
bool KeepProgramOpen = true;
time_t timer;
time_t oldtime;
time_t dtime;
time_t looptime;
int rate;
char Note;
bool FirstLoop = true;
eventLoop MainEventLoop;
int main()
{
rate = 60;
looptime = 1000 / rate;
while(KeepProgramOpen==true)
{
time(&timer);
dtime = timer-oldtime;
if(dtime<looptime)
{
continue;
}
oldtime = timer;
MainEventLoop.FindFinalNote(buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG, FirstLoop);
FirstLoop = false;
//Closing stuff goes here
}
}
eventLoop.h:
#pragma once
class eventLoop {
public:
void FindFinalNote(bool, bool, bool, bool, bool, bool, bool, bool);
protected:
};
eventLoop.cpp:
#include "eventLoop.h"
#include "MidiOutput.h"
#include "FileIO.h"
MidiOutput MidiOutputX;
FileIO fileioX;
void eventLoop::FindFinalNote(bool A, bool B, bool C, bool D, bool E, bool F, bool G, bool firstloop)
{
if(firstloop == true)
{
for (int loopindex=0; loopindex<10; loopindex++)
{
// Note[loopindex] = Filecheck for notes
}
MidiOutputX.FindDevice(
1, /*int argc number of ports*/
60, /*char argv argument vector - strings pointed to, i don't really get it*/
);
}
char Note[10];
int KeyIndex = 0;
FileIO::key CurrentKey;
CurrentKey = fileioX.RetrieveKey(KeyIndex);
for (int x = 0; x < 10; x++)
{
Note[x] = CurrentKey.Note[x];
}
// There's a bunch of simple if statements here, nothing I need to bore you with
}
MidiOutput.h:
#pragma once
class MidiOutput {
public:
void FindDevice(int, char);
void PlayNote(unsigned char);
void EndNote();
void CloseDevice();
protected:
};
MidiOutput.cpp:
#include "MidiOutput.h"
#include <Windows.h>
#include <mmsystem.h>
#include <stdio.h>
union { unsigned long word; unsigned char data[4]; } message;
int midiport;
HMIDIOUT device;
void FindDevice(int argc, char** argv)
{
if (argc < 2) {
midiport = 0;
} else {
midiport = atoi(argv[1]);
}
printf("Midi output port set to %d.\n", midiport);
midiOutOpen(&device, midiport, 0, 0, CALLBACK_NULL);
message.data[0] = 0x90; //command byte
message.data[1] = 60; //middle C
message.data[2] = 0; //volume, 0-100
message.data[3] = 0; //not used
}
void MidiOutput::PlayNote(unsigned char Note)
{
message.data[1] = Note;
message.data[2] = 100;
}
void MidiOutput::EndNote()
{
message.data[2] = 0;
}
void MidiOutput::CloseDevice()
{
midiOutReset(device);
midiOutClose(device);
}
Exact Errors:
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall MidiOutput::FindDevice(int,char)" (?FindDevice#MidiOutput##QAEXHD#Z) referenced in function "public: void __thiscall eventLoop::FindFinalNote(bool,bool,bool,bool,bool,bool,bool,bool)" (?FindFinalNote#eventLoop##QAEX_N0000000#Z) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\eventLoop.obj GHX
Error 2 error LNK2019: unresolved external symbol _imp_midiOutOpen#20 referenced in function "void __cdecl FindDevice(int,char * *)" (?FindDevice##YAXHPAPAD#Z) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 3 error LNK2019: unresolved external symbol _imp_midiOutClose#4 referenced in function "public: void __thiscall MidiOutput::CloseDevice(void)" (?CloseDevice#MidiOutput##QAEXXZ) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 4 error LNK2019: unresolved external symbol _imp_midiOutReset#4 referenced in function "public: void __thiscall MidiOutput::CloseDevice(void)" (?CloseDevice#MidiOutput##QAEXXZ) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 5 error LNK1120: 4 unresolved externals C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\Debug\GHX.exe GHX
Thanks in advance, and sorry about the wall of code, I'm not sure what's necessary.

The missing symbols, midiOutOpen, midiOutClose etc. are defined in the DLL Winmm.dll. You will need to link to Winmm.lib by either specifying it as an input to the link command or by including this in your file:
#pragma comment(lib, "Winmm.lib")
You're also getting an error about MidiOutput::FindDevice. You will need to fix up the signatures so that the .h file and .cpp match, and qualify the function definition in the .cpp file with the class name (MidiOutput::).

Related

Passing objects as parameters by another object visual c++

I'm trying to pass an object by reference in c++. I get these errors:
Error 1 error C2061: syntax error : identifier 'Common' graphics.h 6 1 SDLGameDev
Error 2 error C2511: 'void Graphics::CreateWindow(Common &)' : overloaded member function not found in 'Graphics' 4 1 SDLGameDev
I found answers about this area, but not any that covers how to do this:
object1.someFunction(object2);
Here is my code:
//Common.h
#ifndef COMMON_H
#define COMMON_H
#include "SDL.h"
#include "iostream"
class Common{
public:
void Init();
bool GetGameRunState(){ return GameRunState; }
void SetGameRunState(bool x){ GameRunState = x; }
private:
bool GameRunState;
};
#endif
//Commmon.cpp
#include "Common.h"
void Common::Init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
SetGameRunState(true);
}
else
{
SetGameRunState(false);
}
}
//Graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
class Graphics{
public:
void CreateWindow(Common & co);
};
#endif
//Graphics.cpp
#include "Graphics.h"
#include "Common.h"
void Graphics::CreateWindow(Common & co)
{
if (co.GetGameRunState() == true)
{
std::cout << "TEST for CreateWindow()\n";
}
}
//main.cpp
#include "Common.h"
#include "Graphics.h"
Common co;
Graphics go;
int main(int argc, char * args[])
{
co.Init();
go.CreateWindow(co);
while (co.GetGameRunState() == true)
{
std::cout << "Game is running\n";
SDL_Delay(2000);
break;
}
return 0;
}
You haven't included Common.h in the file Graphics.h so it doesn't know about the class.
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include "Common.h" // You need this line
class Graphics {
public:
void CreateWindow(Common & co);
};
#endif
I would recommend using singletons and put the initialisation of sdl, creation of the renderer and window etc all together in one class. Your question has already been answered.

error LNK2019: unresolved external symbol/

I'm having a lot of trouble trying to compile an ogre sample found on Github.
I've had several Intellisense errors, compilation & linking errors. Now I'm stuck with 2 linker errors. I know there's a lot of similar questions around here because I've read a lot on the subject but I can't find (or see) the right solution.
error LNK2019: unresolved external symbol "public: __thiscall NFSpace::PlanetMapTile::PlanetMapTile(struct NFSpace::QuadTreeNode *,class Ogre::SharedPtr<class Ogre::Texture>,class Ogre::Image,class Ogre::SharedPtr<class Ogre::Texture>,int)" (??0PlanetMapTile#NFSpace##QAE#PAUQuadTreeNode#1#V?$SharedPtr#VTexture#Ogre###Ogre##VImage#4#1H#Z) referenced in function "public: class NFSpace::PlanetMapTile * __thiscall NFSpace::PlanetMap::finalizeTile(struct NFSpace::QuadTreeNode *)" (?finalizeTile#PlanetMap#NFSpace##QAEPAVPlanetMapTile#2#PAUQuadTreeNode#2##Z)
error LNK2019: unresolved external symbol "public: struct NFSpace::QuadTreeNode const * __thiscall NFSpace::PlanetMapTile::getNode(void)" (?getNode#PlanetMapTile#NFSpace##QAEPBUQuadTreeNode#2#XZ) referenced in function "public: void __thiscall NFSpace::PlanetRenderable::setFrameOfReference(struct NFSpace::PlanetLODConfiguration &)" (?setFrameOfReference#PlanetRenderable#NFSpace##QAEXAAUPlanetLODConfiguration#2##Z)
here is the code associated with the first error:
PlanetMapTile.h
namespace NFSpace {
class PlanetMapTile {
public:
PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size);
~PlanetMapTile();
};
}
PlanetMapTile.cpp
#include "PlanetMapTile.h"
namespace NFSpace {
PlanetMapTile::PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size) {
//do something
}
PlanetMapTile::~PlanetMapTile() {
//do something
}
}
PlanetMap.h
#include "PlanetMapTile.h"
namespace NFSpace {
class PlanetMap {
public:
PlanetMapTile* finalizeTile(QuadTreeNode* node);
};
}
PlanetMap.cpp
#include "PlanetMap.h"
namespace NFSpace {
PlanetMapTile* PlanetMap::finalizeTile(QuadTreeNode* node) {
mStep = 0;
return new PlanetMapTile(node, mHeightTexture, mHeightImage, mNormalTexture, getInt("planet.textureSize"));
}
}
Any help would be appreciated.
That is how you should declare the name space
PlanetMApTile.h
namespace NFSpace{
class PlanetMapTile {
public:
PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size);
~PlanetMapTile();
};
}
PlanetMapTile.cpp
#include "PlanetMapTile.h"
NFSpace::PlanetMapTile::PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size) {
//do something
}
NFSpace::PlanetMapTile::~PlanetMapTile() {
//do something
}
PlanetMap.h
#include "PlanetMapTile.h"
class PlanetMap {
public:
NFSpace::PlanetMapTile* finalizeTile(QuadTreeNode* node);
}
PlanetMap.cpp
#include "PlanetMap.h"
NFSpace::PlanetMapTile* PlanetMap::finalizeTile(QuadTreeNode* node) {
mStep = 0;
return new NFSpace::PlanetMapTile(node, mHeightTexture, mHeightImage, mNormalTexture, getInt("planet.textureSize"));
}
So I've finally found the solution:
Considering the fact that PlanetMapTile() and getNode() were both involving QuadTreeNode* and that ~PlanetMapTile() didn't raise an error, I've started to look at the QuadTreeNode declaration which is located in PlanetCubeTree.h. Then I've just tried to add #include "PlanetCubeTree.h" to PlanetMapTile.h and it solved the error.
Thank you for your help

warning C4013: 'sleep' undefined; assuming extern returning int

My Goal is to get outputs from Dining Philosophers C Program.
I am using Visual Studio 2013 on Windows 7 to compile and execute C programs.
Got errors stating that pthread.h, semaphore.h were not available.
Downloaded the same for windows build and included in the project.
Now I get the following 8 errors
warning C4013: 'sleep' undefined; assuming extern returning int
error LNK2019: unresolved external symbol __imp__sem_init referenced
in function _main
error LNK2019: unresolved external symbol __imp__sem_wait referenced
in function _put_fork
error LNK2019: unresolved external symbol __imp__sem_post referenced
in function _put_fork
error LNK2019: unresolved external symbol __imp__pthread_create
referenced in function _main
error LNK2019: unresolved external symbol __imp__pthread_join
referenced in function _main
error LNK2019: unresolved external symbol _sleep referenced in
function _philospher
error LNK1120: 6 unresolved externals
The Code I have used is`
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N
sem_t mutex;
sem_t S[N];
void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);
int state[N];
int phil_num[N] = { 0, 1, 2, 3, 4 };
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&mutex, 0, 1);
for (i = 0; i<N; i++)
sem_init(&S[i], 0, 0);
for (i = 0; i<N; i++)
{
pthread_create(&thread_id[i], NULL, philospher, &phil_num[i]);
printf("Philosopher %d is thinking\n", i + 1);
}
for (i = 0; i<N; i++)
pthread_join(thread_id[i], NULL);
}
void *philospher(void *num)
{
while (1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
void take_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungry\n", ph_num + 1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}
void test(int ph_num)
{
if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and %d\n", ph_num + 1, LEFT + 1, ph_num + 1);
printf("Philosopher %d is Eating\n", ph_num + 1);
sem_post(&S[ph_num]);
}
}
void put_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d down\n", ph_num + 1, LEFT + 1, ph_num + 1);
printf("Philosopher %d is thinking\n", ph_num + 1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}`
It is too late, but today I faced this problem too.
Problem:
I can't use pthread in window platform.
Solution: find another variant of pthreadVC2.lib in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib (for me it was x86 instead of x64)

error LNK2001: unresolved external symbol __imp__Py_InitModule4

I'm trying to extend Python with C++. I'm using Visual C++ 2008 and Python 2.7. I have had a lot of problems building the .dll file, and finally when it seemed to be everything correct, I can't stop getting this error:
error LNK2001: unresolved external symbol _imp_Py_InitModule4
I know it isn't a linker error because I had this error before (it gave me the error but with all kind of Py_... functions) and I had resolved that.
I don't know if this is an important data but I have build python27_d.dll with VC++ 2008 too.
This is the code:
#include "Python.h"
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <stdlib.h>
#include <Aclapi.h>
struct file_perms {
char user_domain[2050];
unsigned long user_mask;
};
void lookup_sid ( ACCESS_ALLOWED_ACE* pACE, char user_domain[] ) {
char username[1024]="";
char domain[1024]="";
ULONG len_username = sizeof(username);
ULONG len_domain = sizeof(domain);
PSID pSID =(PSID)(&(pACE->SidStart));
SID_NAME_USE sid_name_use;
LPWSTR username1 = reinterpret_cast<LPWSTR>( username );
LPWSTR domain1 = reinterpret_cast<LPWSTR>( domain );
if (!LookupAccountSid(NULL, pSID, username1, &len_username, domain1, &len_domain, &sid_name_use)){
strcpy(user_domain, "unknown");
} else {
strcat(user_domain,domain);
strcat(user_domain,"\\");
strcat(user_domain,username);
}
}
void acl_info( PACL pACL, ULONG AceCount, file_perms fp[]){
for (ULONG acl_index = 0;acl_index < AceCount;acl_index++){
ACCESS_ALLOWED_ACE* pACE;
if (GetAce(pACL, acl_index, (PVOID*)&pACE))
{
char user_domain[2050]="";
lookup_sid(pACE,user_domain);
strcpy(fp[acl_index].user_domain,user_domain);
fp[acl_index].user_mask=(ULONG)pACE->Mask;
}
}
}
static PyObject *get_perms(PyObject *self, PyObject *args)
{
PyObject *py_perms = PyDict_New();
//get file or directory name
char *file;
if (!PyArg_ParseTuple(args, "s", &file))
return NULL;
//setup security code
PSECURITY_DESCRIPTOR pSD;
PACL pDACL;
//GetNamedSecurityInfo() will give you the DACL when you ask for
//DACL_SECURITY_INFORMATION. At this point, you have SIDs in the ACEs contained in the DACL.
LPWSTR file1 = reinterpret_cast<LPWSTR>( file );
ULONG result = GetNamedSecurityInfo(file1,SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL,
&pDACL, NULL, &pSD);
if (result != ERROR_SUCCESS){ return NULL;}
if (result == ERROR_SUCCESS){
ACL_SIZE_INFORMATION aclSize = {0};
if(pDACL != NULL){
if(!GetAclInformation(pDACL, &aclSize, sizeof(aclSize),
AclSizeInformation)){
return NULL;
}
}
file_perms *fp = new file_perms[aclSize.AceCount];
acl_info(pDACL, aclSize.AceCount, fp );
//Dict
for (ULONG i=0;i<sizeof(fp);i++){
PyObject *domain = Py_BuildValue("s",fp[i].user_domain);
PyObject *user = Py_BuildValue("s",fp[i].user_mask);
PyDict_SetItem(py_perms,domain,user);
}
}
return py_perms;
};
static PyMethodDef fileperm_methods[] = {
{ "get_perms", get_perms, METH_VARARGS, "Execute a shell command." },
{ NULL, NULL, 0, NULL }
};
extern "C"
__declspec(dllexport)
void init_fileperm(void)
{
PyObject *m=Py_InitModule("fileperm",fileperm_methods);
return;
}
I'm working in Windows 7 64bits.
I know that Py_InitModule is deprecated for Python 3 but I'm working in Python27 (2.7.3 ).
Does someone know why I get this error?
Thanks!
I had the same problem.
If you're compiling a 64-bit pyd, make sure python27.lib is also 64-bit (same goes for compiling a 32-bit pyd with a 32-bit python27.lib).

Matlab / Microsoft Visual C++ 2010 Express: What is "error LNK2019: unresolved external symbol"?

I am trying to create a simple C++ class and a Matlab mex file. My code is as follows:
Matlab: mexTest1.cpp
#include "mex.h"
#include "K:\\My Documents\\Visual Studio 2010\\Projects\\HelloWorld\\HelloWorld\\Class1.h"
/* Input Arguments */
#define X prhs[0]
/* Output Arguments */
#define RESULT plhs[0]
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ){
/* Initialize input and output pointers
// Inputs */
double *x;
/* Outputs */
double r;
double *result;
mwSize m,n;
m = mxGetM(X);
n = mxGetN(X);
/* Create a matrix for the return argument */
RESULT = mxCreateDoubleMatrix(1, 1, mxREAL);
/* Assign pointers to the various parameters */
result = mxGetPr(RESULT);
x = mxGetPr(X);
/* Do the actual computations in a subroutine */
Class1 c1(2, 15.0);
r = c1.product();
result[0] = r;
return;
}
Class1.h:
#pragma once
#include <string> // Standard string class in C++
class Class1
{
public:
int a;
double b;
public:
Class1(const int& a, const double& b);
//virtual ~Class1();
void print() const;
double product() const;
};
Class1.cpp:
#include "stdafx.h"
#include <iostream>
#include "Class1.h"
Class1::Class1(const int& a, const double& b){
Class1::a = a;
Class1::b = b;
}
void Class1::print() const{
std::cout << "a=" << Class1::a << " * b=" << Class1::b << " = " << Class1::product() << std::endl;
}
double Class1::product() const{
return a*b;
}
Running the Matlab command mex mexTest1.cpp gives the error messages:
Creating library C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.x and object C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.exp
mexTest1.obj : error LNK2019: unresolved external symbol "public: double __thiscall Class1::product(void)const " (?product#Class1##QBENXZ) referenced in function _mexFunction
mexTest1.obj : error LNK2019: unresolved external symbol "public: __thiscall Class1::Class1(int const &,double const &)" (??0Class1##QAE#ABHABN#Z) referenced in function _mexFunction
mexTest1.mexw32 : fatal error LNK1120: 2 unresolved externals
C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'mexTest1.mexw32' failed.
??? Error using ==> mex at 208
Unable to complete successfully.
Can anyone help me fix this problem?
Thanks.
The linker is telling you that in trying to construct an executable, it wasn't supplied with an object file that contains Class1::product and Class1::Class1. That's because those functions would be supplied by compiling Class1.cpp, which your command line doesn't ask for.
You should use the syntax of mex for multiple files: mex mexTest1.cpp Class1.cpp
Your linker is unable to find deinitions(bodies) Class1 methods (constructor and product). This may be due to
You haven't provided any definition(body)
The definitions are in a lib file which you forgot to link to

Resources