Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have a project that links to a library (OpenCV 2.1). I have compiled OpenCV from source, using the same compiler. 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". 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.

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)

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). (I looked in the source for OpenCV and found that the function in question is defined in this library)

1>Linking...
1>Searching libraries
1> Searching 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.

I ran dumpbin /all on the library file, and I see the following symbols defined. (I have the gnu tool chain for windows installed, so I can use grep).

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

So, as you can see, the linker is looking for the symbol _cvFloodFill, but that is not defined. 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.

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?

like image 269
Diane Avatar asked Jan 29 '26 13:01

Diane


1 Answers

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.

When I compiled my project, the platform was set to 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.

So now, the project and OpenCV are compiled for the same platform and everything links and runs correctly.

like image 77
Diane Avatar answered Jan 31 '26 07:01

Diane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!