我想在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.dll
到Project > UseOfDll Properties > Linker > Input > Additional Dependecies
。我還將HelloWorldDll.dll
和HelloDll.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;
}
這是一個標準錯誤,你不能鏈接一個DLL。它沒有足夠的信息讓鏈接器做適當的工作。當鏈接器無法理解文件的內容時,鏈接器就會崩潰。您必須*鏈接DLL的導入庫。 –
你的類沒有被導出,把__declspec(dllexport)放在你的類定義上 – Matt
@Matt在'#ifdef DLLDIR_EX'後面的行中是否已經有了? – roscioli