错误LNK1104链接.dll以测试Visual Studio中的应用程序"无效或损坏的文件"

时间:2014-05-22 18:13:58

标签: c++ dll linker visual-studio-2013

我试图在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

在我的UseOfDll项目中,我向C:\Users\octavio\Documents\Visual Studio 2013\Projects\UseOfDll\UseOfDll\HelloWorldDll.dll添加了Project > UseOfDll Properties > Linker > Input > Additional Dependecies。我还将HelloWorldDll.dllHelloDll.h添加到UseOfDll项目目录。

这是使用DLL的程序的主要方法(称为UseOfDll):

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

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

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

在我单独的DLL的Visual Studio项目中,我有:

// 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;
}

1 个答案:

答案 0 :(得分:1)

解决方案:将class HelloDll替换为class DLLDIR HelloDll。 这会将类链接到DLL导出库。