Question:
C++ Unresolved External Symbol?
Steven ~
2011-05-31 17:47:31 UTC
A very simple project, probaby a very simple solution. But I'm newb at this and failed with google.
(Laugh at my noobness, idc)

In my solution, I have 2 projects (Login Server and Common) (I'm messing around with an old source)

in Login Server I have main.cpp and main.h.
main.h is empty and main.cpp has the following

#include
#include "..\common\showmsg.h"
int main(int argc, char *argv[]) {
ShowMessage();
std::cin.get();
}

in Common i have showmsg.h and showmsg.cpp

showmsg.h has the following

#ifndef _SHOWMSG_H_
#define _SHOWMSG_H_

extern void ShowMessage();

#endif

showmsg.cpp has the following

#include

#include "showmsg.h"

void ShowMessage() {
printf("Hello");
}


The whole error is

1>main.obj : error LNK2019: unresolved external symbol "void __cdecl ShowMessage(void)" (?ShowMessage@@YAXXZ) referenced in function _main
1>C:\Users\XXXX\Desktop\XXXX\XXXX\Debug\Login Server.exe : fatal error LNK1120: 1 unresolved externals
Three answers:
The Phlebob
2011-05-31 18:09:57 UTC
The program compiles correctly because ShowMessage() has a declaration, but when the linker (a program the compiler automatically runs when it itself is finished) tries to pull together the machine code your compile generated and other pieces that are necessary, it doesn't find one with a function named ShowMessage() in it.



That's the "unresolved external symbol". See the "ShowMessage(void)" in it?



You have to dig up the file the function is supplied in, and give it and its path to the compiler so it can be handed off to the linker. Can't tell you exactly how to do this because different compilers do it different ways.



Hope that helps.



Note: It's also possible that ShowMessage() is supplied in a .cpp source file and you're supposed to compile it also. You'll still have to point the linker at the output of that compile also.
Chris C
2011-06-01 01:22:42 UTC
Are you compiling this with a GNU C++ compiler, or CodeBlocks, or Microsoft DevStudio?

If it is using the GNU C++ compiler, you can add to link in the object file from "showmsg".



Now, if it is using CodeBlocks, you can add "../common/showmsg.o" to the build.

Likewise the same can be done with DevStudio.
JoelKatz
2011-06-01 00:54:53 UTC
What you used to launch the compiler told it to compile and link 'main.cpp'. While 'main.cpp' can be compiled, it cannot be linked without also including whatever 'showmsg.cpp' compiles to because it requires symbols from it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...