2015-12-30 141 views
0

我創建了一個名爲測試庫DLL中的項目:功能被錯誤地標記爲dllimport

// main.h 
#ifndef __MAIN_H__ 
#define __MAIN_H__ 

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

extern "C" { 
    DLL_EXPORT void print(); 
} 
#endif 

// main.cpp 
#include "main.h" 
#include <iostream> 

#define BUILD_DLL 

using std::cout; 
using std::endl; 

extern "C" { 
    DLL_EXPORT void print() { 
     cout << "Success" << endl; 

     return; 
    } 
} 

上面的代碼是從下面我在網上找到,我可以理解一個例子。當我嘗試編譯和/或構建它,我收到以下錯誤&警告:

error: function 'void print()' definition is marked dllimport 
In function 'void print()': 
warning: 'void print()' redeclared without dllimport attribute: previous dllimport ignored 

這是我曾經創造,因爲我想在第一個複製問題的第二個庫,當這發生。哪裏不對?我使用的是Code :: Blocks。

回答

3

在包含頭文件main.h之前,您需要定義BUILD_DLL

#define BUILD_DLL 
#include "main.h" 

目前的情況是在你的程序中,你__declspec(dllimport)聲明print因爲當沒有定義BUILD_DLL頭文件進行處理。