1

I am using Visual Studio 2013. I have a file with a main function (file1.cpp). This main function uses a function that is defined in another file (file2.cpp). There is a header file for file2.cpp, but its name is different. So its not called file2.h but hdr2.h. When I build the project I get an unresolved external symbol error for the function that is used in main. The header file is defined in main but it doesn't seem to be compiling the function that is in file2.cpp. Can someone please tell me how to solve this?

Thanks

1 Answers1

4

The name of the header file and the name of the associated implementing source file need not be related. Indeed you can have a header file without any associated implementing source file, or a header file whose implementation spreads over multiple source files.

An unresolved external symbol error simply tells you that the linker cannot find a symbol that you used. That means that you declared a symbol, but did not define it. Since you have source for all the code, this means that you have not passed to to linker the compiled object that provides the definitions. The way you would resolve that is to simply add the defining source files to the project. Once you do that, Visual Studio will compile these source files, and pass the output objects to the linker.

According to your question, the missing symbols are defined in file2.cpp. Simply add that file to your Visual Studio project.


If you include the file in the project, and the error remains, then it would seem that the unresolved symbol is not in fact defined by that file.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I do have the source file in the project. I created a folder in the Visual Studio solution explorer and placed it in that. So do I need to do something else? – beandigital Jun 20 '14 at 12:02
  • No. If the source file is compiled and the output passed to the linker, and the linker cannot find a definition, then there is no definition. – David Heffernan Jun 20 '14 at 12:10
  • Putting a file inside the same folder tree as your project does not add the file to the project. You need to add it to the project in the Visual Studio GUI. – drescherjm Jun 20 '14 at 12:11
  • I just added the file to the prj without the folder and its ok now. But I have one more problem ;). There is a variable that is defined as extern in a file. The variable is defined as static in another file. I have both files in the prj but I am betting an unresolved external symbol error. – beandigital Jun 20 '14 at 12:51
  • I don't about that other problem. Hard to grasp the details in comments. I think I answered the question you asked here. – David Heffernan Jun 20 '14 at 12:52