Search code examples
c++cvisual-studio-2013ftdiextern-c

VS2013 C++ Compiler Mangling name defined with extern "C"


I'm trying to build a WIN32 console app that uses the current 2.12.28 ftd2xx.lib static library from FTDI. I'm using VS2013 and native unmanaged C++. My call looks like this.

#include "../ftd2xx.h"
 . . .
    DWORD port_count = 0;
    FT_STATUS status = FT_OK;
    status = FT_CreateDeviceInfoList(&port_count);

When I compile I get a link error

GetTopazVCP.obj : error LNK2019: unresolved external symbol __imp__FT_CreateDeviceInfoList@4 referenced in function "unsigned long __cdecl Get1stVirtualComPort(unsigned long *)" (?Get1stVirtualComPort@@YAKPAK@Z)

The unresolved symbol __imp__FT_CreateDeviceInfoList@4 appears to be a mangled name version of the FT_CreateDeviceInfoList function. So it's not being resolved in the ftd2xx.lib which uses C naming. What I don't understand is why the compiler mangled the name when the ftd2xx.h file has a conditional extern "C"

#ifdef __cplusplus
   extern "C" {
#endif
. . .
FTD2XX_API
    FT_STATUS WINAPI FT_CreateDeviceInfoList(
    LPDWORD lpdwNumDevs
    );
...
#ifdef __cplusplus
}
#endif

wrapping all the FT_??? declarations. I have confirmed that __cplusplus is defined during the compile. Any ideas what is causing the unexpected name mangling?


Solution

  • As ChronoKitsune pointed out in his comment there is no name mangling here. The problem was I was linking the static version of the FTDI library but the default for the ftd2xx.h header file is to declare the FT_??? functions as calls to the DLL version. When I replaced the ftd2xx.lib static version with the ftd2xx.lib DLL version it built successfully. FTDI only supplies one version of the ftd2xx.h header and looking closely inside it I discovered that if you want to use the static ftd2xx.lib you need to #define FTD2XX_STATIC before including ftd2xx.h

    #define FTD2XX_STATIC
    #include "ftd2xx.h"