简体   繁体   English

使用Visual Studio 2008的C ++中的链接错误和名称处理

[英]Link errors and name mangling in C++ with Visual Studio 2008

I am using the 64 bit version of Visual Studio 2008 on a 64 bit version of Windows. 我在64位版本的Windows上使用Visual Studio 2008的64位版本。 I have a project that links to a library (OpenCV 2.1). 我有一个链接到库(OpenCV 2.1)的项目。 I have compiled OpenCV from source, using the same compiler. 我已经使用相同的编译器从源代码编译了OpenCV。 My problem occurs in both Release and Debug mode. 我的问题出现在发布和调试模式下。

In the Properties pane or my project, I have added the directory containing the header files under C/C++ -> "Additional Include Directories". 在“属性”窗格或我的项目中,我已在C / C ++->“其他包含目录”下添加了包含头文件的目录。 I have added the directory containing the .lib files to the "Additional Library Directories", and I have added the names of the libraries to the "Additional Dependencies" entry under Linker -> Input. 我已将包含.lib文件的目录添加到“其他库目录”,并将库的名称添加到了链接器->输入下的“其他依赖项”条目。

When I compile and link, I get errors in this style: error LNK2019: unresolved external symbol _cvFloodFill referenced in function "int __cdecl Label(class cvImage &)" (?Label@@YAHAAV?$cvImage@M@@@Z) 编译和链接时,出现以下样式错误:错误LNK2019:函数“ int __cdecl Label(class cvImage&)”中引用的未解析的外部符号_cvFloodFill(?Label @@ YAHAAV?$ cvImage @ M @@@@ Z)

Under Linker->Command Line -> Additional options, I added the tag: /verbose:lib and I see in the compiler output window that it is searching and finding the library that should contain the functions (cv210d.lib). 在“链接器”->“命令行”->“其他选项”下,添加了标记:/ verbose:lib,并且在编译器输出窗口中看到它正在搜索并查找应包含函数的库(cv210d.lib)。 (I looked in the source for OpenCV and found that the function in question is defined in this library) (我查看了OpenCV的源代码,发现该函数在此库中定义)

1>Linking... 1>链接...
1>Searching libraries 1>搜索库
1> Searching C:\\Program Files\\OpenCV2.1\\lib\\cv210d.lib: 1>搜索C:\\ Program Files \\ OpenCV2.1 \\ lib \\ cv210d.lib:

In the OpenCV source, it has the extern "C" declaration wrapping the declaration of the functions in question. 在OpenCV源代码中,它有一个外部的“ C”声明,该声明包装了相关功能的声明。

I ran dumpbin /all on the library file, and I see the following symbols defined. 我在库文件上运行了dumpbin / all,并且看到了以下定义的符号。 (I have the gnu tool chain for windows installed, so I can use grep). (我安装了用于Windows的gnu工具链,因此可以使用grep)。

C:\\Program Files\\OpenCV2.1\\lib>dumpbin /all cv210d.lib | C:\\ Program Files \\ OpenCV2.1 \\ lib> dumpbin / all cv210d.lib | grep cvFloodFill grep cvFloodFill
732B4 __imp_cvFloodFille 732B4 __imp_cvFloodFille
732B4 cvFloodFill 732B4 cvFloodFill
1CA __imp_cvFloodFill 1CA __imp_cvFloodFill
1CA cvFloodFill 1CA cvFloodFill

So, as you can see, the linker is looking for the symbol _cvFloodFill, but that is not defined. 因此,如您所见,链接器正在寻找符号_cvFloodFill,但未定义。 only cvFloodFill and __imp_cvFloodFill are defined, so it looks like something funny is happening that is making the name-mangling different when I compile the library vs when I try to link against it. 仅定义了cvFloodFill和__imp_cvFloodFill,所以看起来好像发生了一些有趣的事情,这使我在编译库与尝试对其进行链接时的名称更改有所不同。

So that is where I am stuck. 这就是我被困住的地方。 Am I interpreting this information correctly? 我是否正确解释此信息? Is there something I need to do to affect the name-mangling? 我需要做些什么来影响名称处理吗? Do I need to compile the library with different flags of some sort? 我是否需要使用某种不同的标志来编译库?

I found it. 我找到了。 (I posted the question and answer for the benefit of anyone else who has this problem, because I didn't find anything like this when I searched the web). (我发布问题和答案是为了让其他任何有此问题的人受益,因为当我在网络上搜索时找不到类似的东西)。

When I compiled OpenCV, under the Solution Properties, Configuration Properties -> Configuration, the Platform for all of the projects was set to x64. 当我编译OpenCV时,在“解决方案属性”,“配置属性”->“配置”下,所有项目的“平台”都设置为x64。

When I compiled my project, the platform was set to Win32. 当我编译项目时,平台设置为Win32。 The answer was to click on Configuration Manager, Click on the Down arrow under "Platform" for the project, select "New", and then select x64. 答案是单击配置管理器,单击项目的“平台”下的向下箭头,选择“新建”,然后选择x64。

So now, the project and OpenCV are compiled for the same platform and everything links and runs correctly. 因此,现在,该项目和OpenCV是为同一平台编译的,所有内容都可以正确链接并运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM