链接错误,无法正确链接头文件到cpp文件 [英] linking error for not being able to link header file to cpp file correctly

查看:257
本文介绍了链接错误,无法正确链接头文件到cpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案中有两个项目。当我尝试从另一个项目访问一个项目的函数,我得到错误LNK2001:未解决的外部符号。但是链接错误只发生在函数在头文件中声明但在相应的cpp文件中定义。如果函数在头文件中定义的错误不会发生。同样从同一个项目调用函数不会给出任何错误。只有从另一个结果调用链接错误。

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.

编辑:我使用visual studio 2010.我不知道它值得一提的是被调用的函数

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.

推荐答案

如果定义了一个文件,函数,编译器将在构建.exe项目时看到函数实现,并将函数代码的副本直接编译到.exe项目中。当链接器在构建过程中轮到时,没有什么缺失,所以链接器很快乐,你不会得到错误消息。

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.

如果你定义的函数在.cpp文件,编译器不会看到函数实现。因此,它将引用一个函数(即外部符号),当构建期间链接器轮流时,需要解析它。要使链接器看到外部符号,您需要链接.exe项目与.lib项目。一旦建立了此链接相关性,链接器将能够找到外部符号并解析对编译器先前生成的函数的引用。因为您有一个.lib项目,它是一个静态库项目,链接器通过从.lib文件中获取函数的代码来解析该符号,并将代码的副本放入.exe文件中。

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.

这个理论太多了。现在,使.exe项目链接到.lib项目的最简单的方法可能是添加一个引用:

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:


  • 在.exe项目

  • 现在,您应该会看到.exe项目具有的引用列表。列表可能为空。

  • 点击对话框底部的添加新引用按钮,并添加.lib项目作为参考

  • 当您在引用列表中选择新引用时,您将看到该引用的一组属性。确保名为链接库依赖关系的属性设置为true。这将使.lib项目在构建.exe项目时作为链接器的输入自动添加。

  • 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.

如果您构建你的.exe项目,链接器错误现在应该消失了。

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

顺便说一下,通过添加项目引用,你也告诉Visual Studio在正确的如果你构建整个解决方案的顺序:首先是.lib项目,然后是.exe项目。

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.

这篇关于链接错误,无法正确链接头文件到cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆