1

I have two projects in one solution. When I try to access a function of one project's from another one I get error LNK2001: unresolved external symbol. But the linking error only happens when the function is declared in the header file but defined in the corresponding cpp file. If the function is defined in the header file than the error does not occur. Also calling function from same project doesn't give any error. Only calling from another results on linking error.

EDIT: I am using visual studio 2010. I don't know if its worth mentioning that the function which is being called is in a project that outputs a lib file and the one which is calling that function outputs a exe file.

5
  • Which IDE are you using? "Project" and "solution" are IDE specific terms. Maybe add it as a tag? Commented Mar 14, 2014 at 6:58
  • just add cpp file with definition to your project Commented Mar 14, 2014 at 7:01
  • @Sergey How do I do that? Commented Mar 14, 2014 at 7:01
  • On windows you can do it by drug'n'drop from folder to opened project or using ide menu command Commented Mar 14, 2014 at 7:06
  • That just messed up my whole solution. The source folder moved to the project from where I was calling the function. And then when I compiled I got more linking error. And now I can't get them back to where they were! Commented Mar 14, 2014 at 7:27

2 Answers 2

4

If you define the function in the header file, the compiler will see the function implementation when you build the .exe project and compile a copy of the function code directly into your .exe project. When it is the linker's turn during the build, nothing is missing so the linker is happy and you won't get an error message.

If you define the function in the .cpp file, the compiler will not see the function implementation. It will thus put a reference to the function (i.e. the external symbol) that needs to be resolved later on when it is the linker's turn during the build. To make the linker "see" the external symbol, you need to link your .exe project against your .lib project. Once you have established this link dependency, the linker will be able to find the external symbol and resolve the reference to the function that was earlier generated by the compiler. Because you have a .lib project, which is a static library project, the linker resolves the symbol by grabbing the code for the function from the .lib file and places a copy of the code into your .exe file.

So much for the theory. Now the simplest way to make your .exe project link against your .lib project probably is by adding a reference:

  • In the .exe project's settings, select the section named "Common Properties" at the top of the section list.
  • You should now see a list of references that the .exe project has. The list is probably empty.
  • Click the "Add new reference" button at the bottom of the dialog and add the .lib project as a reference
  • When you select the new reference in the list of references you will see a set of properties for that reference. Make sure that the property called "Link Library Dependencies" is set to true. This will cause the .lib project to be added automatically as an input to the linker when you build the .exe project.

If you build your .exe project, the linker error should now be gone.

Incidentally, by adding the project reference you have also told Visual Studio to build the two projects in the correct order if you build the entire solution: First the .lib project, then the .exe project.

Sign up to request clarification or add additional context in comments.

Comments

0

It is the role of the linker to resolve unknown symbols.

Thus if projet A uses methods from projet B defined in the cpp file, you need to link A against B.

As stated, it would be fine to have more information about both projects, IDE (visual???)...

1 Comment

I am using visual studio 2010. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.