Error C2491: definition of dllimport function not allowed
Asked Answered
A

3

6

I have a problem with make a dll on Visual Studio 2013. This code works on Code::Blocks. The error was definition of dllimport function not allowed" on line void DLL_EXPORT prim(map<string, vector<int>> nodes, map<pair<string, string>, pair<int, string>> edges). How to fix it?

main.h:
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>
#include <iostream>
#include <vector>
#include <map>

using namespace std;

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT prim( map<string,vector<int>> nodes, map<pair<string,string>,pair<int,string>> edges);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

And the second file:

main.cpp:
#include "main.h"
//some other includes

// a sample exported function

extern "C"
{
    void DLL_EXPORT prim(map<string, vector<int>> nodes, map<pair<string, string>, pair<int, string>> edges)
    {
        //some code
    }
}

I tried to fix it, but I have no more idea. When I changed the prim function in second file from definition to the declaration, the dll was compile without errors, but without the code responsible for the implementation of the algorithm.

Thanks for all replies.

Edit:

I add temporary #define BUILD_DLL to main.h and later in Cmake and I works. Thanks for replies.

Alti answered 7/6, 2016 at 7:46 Comment(0)
B
7

main.h and main.cpp will be used in DLL Project which you are creating.

only main.h will be used in client Executable/DLL which is accessing the DLL which you created.

So, main.h of DLL Project requires __declspec(dllexport). So that the functions can be exported from the DLL. So, define BUILD_DLL in DLL Project's Properties -> C/C++ -> 'Preprocessor definitions'

main.h of client Executable requires __declspec(dllimport). So that the functions can be imported from the DLL. So no need to define BUILD_DLL in Executable Project's Properties -> C/C++ -> 'Preprocessor definitions'

Boarish answered 7/6, 2016 at 8:52 Comment(0)
O
1

Your should just define BUILD_DLL is some of your headers or in Project Properties -> C/C++ -> 'Preprocessor definitions'. So DLL_EXPORT will be __declspec(dllexport) and that is what you want when your build your dll. __declspec(dllimport) needed if you want to import function from other dll. And this error means that you can't redefine imported function because it defined in dll from which you import it.

Outlying answered 7/6, 2016 at 7:52 Comment(0)
Y
-1

I think you just need to remove DLL_EXPORT in main.cpp. The error says it is not allowed in the definition. Since it has a body there {...}, it is a definition.

Yuri answered 30/6, 2017 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.