Error C2491: Function declared in header and defin

2020-04-21 04:36发布

I have been trying to build a device adapter for a open-source software called Micro-manager to control a microscope and there are some problems that I am facing, there are these two files (one header and the other CPP) that are already present in the open source package of Micro-Manager.

//MoudluleInterface.h

#ifndef _MODULE_INTERFACE_H_
#define _MODULE_INTERFACE_H_

#ifdef WIN32
#ifdef MODULE_EXPORTS
  #define MODULE_API __declspec(dllexport)
#else
  #define MODULE_API __declspec(dllimport)
#endif

#else
#define MODULE_API
#endif
#define MM_MODULE_ERR_OK 1000
#define MM_MODULE_ERR_WRONG_INDEX   1001
#define MM_MODULE_ERR_BUFFER_TOO_SMALL 1002

 ///////////////////////////////////////////////////////////////////////////////
 // header version
 // NOTE: If any of the exported module API calls changes, the interface version
 // must be incremented
 // new version 5 supports device discoverability
 #define MODULE_INTERFACE_VERSION 7

#ifdef WIN32
const char* const LIB_NAME_PREFIX = "mmgr_dal_";
#else
const char* const LIB_NAME_PREFIX = "libmmgr_dal_";
#endif

#include "MMDevice.h"

///////////////////////////////////////////////////////////////////////////////
// Exported module interface
///////////////////////////////////////////////////////////////////////////////
 extern "C" {
 MODULE_API MM::Device* CreateDevice(const char* name);
 MODULE_API void DeleteDevice(MM::Device* pDevice);
 MODULE_API long GetModuleVersion();
 MODULE_API long GetDeviceInterfaceVersion();
 MODULE_API unsigned GetNumberOfDevices();
 MODULE_API bool GetDeviceName(unsigned deviceIndex, char* name, unsigned      bufferLength);
 MODULE_API bool GetDeviceDescription(const char* deviceName, char* name, unsigned bufferLength);

And here is a part of the CPP file which defines these functions

      //ModuleInterface.cpp
      #define _CRT_SECURE_NO_DEPRECATE
      #include "ModuleInterface.h"
      #include <vector>
      #include <string>

        typedef std::pair<std::string, std::string> DeviceInfo; 
        std::vector<DeviceInfo> g_availableDevices;

      int FindDeviceIndex(const char* deviceName)
   {
  for (unsigned i=0; i<g_availableDevices.size(); i++)
  if (g_availableDevices[i].first.compare(deviceName) == 0)
     return i;

   return -1;
   }

  MODULE_API long GetModuleVersion()
 {
 return MODULE_INTERFACE_VERSION;   
 }

  MODULE_API long GetDeviceInterfaceVersion()
 {
return DEVICE_INTERFACE_VERSION;   
 }

 MODULE_API unsigned GetNumberOfDevices()
{
 return (unsigned) g_availableDevices.size();
 }

 MODULE_API bool GetDeviceName(unsigned deviceIndex, char* name, unsigned bufLen)
{
 if (deviceIndex >= g_availableDevices.size())
  return false;

Now the problem is it gives me an error C2491 (definition of dllimport function not allowed) I did research about this and it usually is when a function is defined when it is supposed to be declared, I have already defined the function in ModuleInterface.h and then used it in ModuleInterface.cpp but it still shows the same error. Can there be some other possibility for this error to occur? Or is there something wrong with the code?

1条回答
Rolldiameter
2楼-- · 2020-04-21 05:05

You're not supposed to repeat your MODULE_API declaration in the definition, having it as part of the declaration is good enough. Remove the use of MODULE_API from the .cpp file and the code should compile.

查看更多
登录 后发表回答