Strange Linking error

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.
closed account (S6k9GNh0)
You need to link in the object your shared.cpp file turns into.
I was able to properly build the executable on Linux (while omitting windows.h) but I had to #include <ctime> and #include <cstdlib> . time.h is deprecated in C++.
Last edited on
$ nm --demangle --undefined-only main.obj
$ nm --demangle --defined-only shared.obj


> such as readTextFile() work
weird
Last edited on
@computerquip
Are you referring to linking the shared.obj file directly? This is a new concept to me. I've tried looking for some information online, but wasn't quite sure what to look for. Could you elaborate a bit more?
Thanks, I do appreciate the input.

***EDIT***
I've attempted to import the shared.obj file and add it to my solution, however, a dialog window shows up telling me that there is no default build rule available for the .obj extension, and it prompts me to create a custom build rule.
I don't think this is the right way, is it?
Also, my IDE is Visual C++ 2008 Express Edition if it makes any difference.
Last edited on
Solved. Turns out I was an idiot. A #define sneaked it's way into one of my header files.
Last edited on
Topic archived. No new replies allowed.