编译时动态链接库不生成 .lib 文件 (Visual Studio C++ Express)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5019809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:11:54  来源:igfitidea点击:

Dynamic link library does not generate a .lib file when compiled (Visual Studio C++ Express)

c++visual-studio-2010.lib

提问by Narf the Mouse

As part of learning C++, I wrote a simple class library + application that references it. Everything builds, except the class library does not generate a .lib file, which results in the application throwing a "LINK : fatal error LNK1104: cannot open file". This seems very reasonable; obviously, if a necessary file isn't there, there's an error and it's fatal. (Side note: I don't have a book yet)

作为学习 C++ 的一部分,我编写了一个简单的类库 + 引用它的应用程序。一切都会构建,除了类库不生成 .lib 文件,这会导致应用程序抛出“链接:致命错误 LNK1104:无法打开文件”。这似乎很合理;显然,如果没有必要的文件,就会出现错误并且是致命的。(旁注:我还没有书)

So, I went looking for reasons a .lib file might not be generated. My search-fu, by the way, is rather weak. All I did find was that, if the library did not have any __declspec(dllexport) tags, it would not export a .lib.

因此,我开始寻找可能无法生成 .lib 文件的原因。顺便说一句,我的搜索功能相当弱。我所发现的只是,如果库没有任何 __declspec(dllexport) 标签,它就不会导出 .lib。

I shall now post the header and .cpp contents of the class library (A simple "Console" class with one "Write(std::string)" method).

我现在将发布类库的标题和 .cpp 内容(一个简单的“控制台”类,带有一个“Write(std::string)”方法)。

Header:

标题:

// Extensions.h

#pragma once

#include "stdafx.h"

namespace Extensions {

    __declspec(dllexport) class Console
    {
    public:
        __declspec(dllexport) static void Write(std::string text);
    };
}

I am unsure whether I need to tag the function when I've tagged the class, but I can check that when it works.

我不确定在标记类时是否需要标记函数,但我可以检查它何时起作用。

And the .cpp file:

和 .cpp 文件:

// This is the main DLL file.

#include "stdafx.h"

// #include "Console.h"

namespace Extensions {

    void Console::Write(std::string text)
    {
        std::cout << text.c_str();
    }
}

I've checked and it is set to generate a dynamic link library.

我已经检查过,它被设置为生成一个动态链接库。

Thanks.

谢谢。

采纳答案by Nathanael

Here is some sample code that demonstrates how to correctly export a class. Pay attention to the CONSOLETEST_EXPORT macro. This is the missing part of your solution. You need to define this macro in your DLL project, and leave it undefined in the projects that reference this dll.

下面是一些演示如何正确导出类的示例代码。注意 CONSOLETEST_EXPORT 宏。这是您的解决方案中缺失的部分。您需要在 DLL 项目中定义此宏,并在引用此 dll 的项目中保持未定义。

// MAIN.CPP - TestApplication

#include <iostream>
#include "ConsoleTest.h"

int main(int argc, char** argv)
{
    std::cout << "Hello World" << std::endl;

    ConsoleTest test;

    test.Write();
    ConsoleTest::StaticWrite();

    system("pause");
}


// ConsoleTest.h - TestDll 

#include <iostream>

#ifdef CONSOLETEST_EXPORT
    #define CONSOLETEST_API __declspec(dllexport)
#else
    #define CONSOLETEST_API __declspec(dllimport)
#endif

class CONSOLETEST_API ConsoleTest
{
public:
    ConsoleTest();
    ~ConsoleTest();
    void Write();
    static void StaticWrite();
};


// ConsoleTest.cpp - TestDll

#include "ConsoleTest.h"

ConsoleTest::ConsoleTest()
{
}

ConsoleTest::~ConsoleTest()
{
}

void ConsoleTest::Write()
{
    std::cout << "Instance Write" << std::endl;
}

void ConsoleTest::StaticWrite()
{
    std::cout << "Static Write" << std::endl;
}

Check out this article on codeproject for more details. HowTo: Export C++ classes from a DLL

查看这篇关于 codeproject 的文章以获取更多详细信息。 如何:从 DLL 导出 C++ 类