2014-05-22 30 views
1

我想在C++中製作一個簡單的HelloWorld DLL,以獲得第一次使用C++ DLL的掛起。但是當我嘗試構建包含我的方法的項目時,我總是得到錯誤error LNK1107: invalid or corrupt file: cannot read at 0x2B8 C:\Users\octavio\Documents\Visual Studio 2013\Projects\UseOfDll\UseOfDll\HelloWorldDll.dll錯誤LNK1104鏈接.dll在Visual Studio中測試應用程序「無效或損壞的文件」

在我的UseOfDll項目中,我添加了C:\Users\octavio\Documents\Visual Studio 2013\Projects\UseOfDll\UseOfDll\HelloWorldDll.dllProject > UseOfDll Properties > Linker > Input > Additional Dependecies。我還將HelloWorldDll.dllHelloDll.h添加到UseOfDll項目目錄中。

這是程序(稱爲UseOfDll)的主要方法,它使用的DLL:

// UseOfDll.cpp ---------------------------------------------------- 

#include "stdafx.h" 
#include "HelloDll.h" 

int _tmain(int argc, _TCHAR* argv[]) { 
    HelloDll helloDll; 
    helloDll.hello(); 
    HelloDll::helloStatic(); 
    getchar(); 
    return 0; 
} 

在我單獨的Visual Studio項目的DLL我有:

// HelloDll.h ------------------------------------------------------ 

#pragma once 

#ifdef DLLDIR_EX 
    #define DLLDIR __declspec(dllexport) // export DLL information 
#else 
    #define DLLDIR __declspec(dllimport) // import DLL information 
#endif 

class HelloDll { 
    public: 
     HelloDll(); 
     ~HelloDll(); 
     void hello(); 
     static void helloStatic(); 
}; 

// HelloDll.cpp ---------------------------------------------------- 

#include "stdafx.h" 
#include "HelloDll.h" 
#include <iostream> 
using namespace std; 

HelloDll::HelloDll() {} 


HelloDll::~HelloDll() {} 

void HelloDll::hello() { 
    cout << "Hello World of DLL" << endl; 
} 

void HelloDll::helloStatic() { 
    cout << "Hello World of DLL static" << endl; 
} 
+0

這是一個標準錯誤,你不能鏈接一個DLL。它沒有足夠的信息讓鏈接器做適當的工作。當鏈接器無法理解文件的內容時,鏈接器就會崩潰。您必須*鏈接DLL的導入庫。 –

+0

你的類沒有被導出,把__declspec(dllexport)放在你的類定義上 – Matt

+0

@Matt在'#ifdef DLLDIR_EX'後面的行中是否已經有了? – roscioli

回答

1

解決方法:更換class HelloDllclass DLLDIR HelloDll。 這將類鏈接到DLL導出庫。

相關問題