Hello, and thanks for looking at my question!
Let's cut to the chase : I've got an LNK2019: unresolved external symbol error in my most recent project. I've never had a problem fixing these, but I simply cannot figure this out. I really hope this is one of those moments where I slap myself for being so blind.
The project is relatively large, but there are only three files that should be of concern. I'll paste a trimmed down version of each below.
The files are "shared.h", "shared.cpp", and "main.cpp".
Also, here is the actual error from the log for reference :
main.obj : error LNK2019: unresolved external symbol "int __cdecl random(int,int)" (?random@@YAHHH@Z) referenced in function "void __cdecl `dynamic initializer for 'hh''(void)" (??__Ehh@@YAXXZ)
shared.h begin
1 2 3 4 5 6 7
|
#ifndef _SHARED_H_
#define _SHARED_H_
int random(int min, int max);
char* readTextFile(const char* filename);
#endif
|
shared.h end
shared.cpp begin
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include "shared.h"
#include <windows.h>
#include <time.h>
bool _bRandom=false;
int random(int min, int max) {
if(_bRandom==false) {
_bRandom=true;
srand(time(0));
}
return(min+(rand()%max));
}
//readTextFile(const char* filename) definition omitted
|
shared.cpp end
main.cpp begin
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include<windows.h>
#include <iostream>
#include "shared.h"
int hh = random(0, 10);
int main() {
/* some code which has nothing to do with hh, infact, the hh identifier
exists only to demonstrate my problem*/
std::cin.sync();
std::cin.get();
return 0;
}
|
main.cpp end
This seems like it would be really simple, but it isn't. I've made sure countless times that the definition and declaration/prototype match. I've tried using the
extern keyword on my prototype. I've checked that all files are properly included in my project. I've tried narrowing down my problem by checking whether other functions (omitted above) such as readTextFile() work, and they do, which only makes it more confusing.
I've even tried starting a new project with just the bare-bone code I've posted, and everything seems to work fine. This makes me think that the error is SOMEHOW(???) caused by the other libs I'm using, which makes this post redundant.
It feels like I'm missing something, so I will update this post as needed.
Thanks for any suggestions you may have.