何时在C ++中使用__declspec(dllexport) [英] When to use __declspec(dllexport) in C++

查看:173
本文介绍了何时在C ++中使用__declspec(dllexport)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



进口商(.exe)文件编译干净,导入后没有生成错误方法在我的DLL文件。



由于没有使用任何这些调用约定,我会在这里搞乱吗?



我的DLL文件在命名空间中,并以OOP方式实现。



但有时或大多数时候,.lib不会生成将我的DLL的设置切换到LIB并复制生成的.lib文件,并再次将其重新回到DLL文件。



我甚至不知道我使用的是一个.dll或.lib文件。



有人向我解释清楚了吗?



EDIT



我将添加一些情况,我真的很混淆什么时候使用它。



假设是我有这些:

 命名空间
{

基本抽象
{

public:
Base()
{
//初始化基本组件
}
virtual void func ()= 0;

public:

// interface
};

class Derive:public Base
{

public:
Derive();
void func()override;

private:

//接口

};

}

我应该使用 declspec )这里:构造函数




    < (例如 Base() Derive())?


  • virtual 功能?我会在哪里放?在派生或在基地?或两者?



  • 公共变量? (可怕的!)



我看到其他头文件,他们添加 extern 进去。我需要它吗?



这会毁掉我所有的代码语法



解决方案

听起来像是在您的库文件中链接应该在dll。所以你不使用dll的。一切正在进入exe文件。



Microsoft需要dll和<$ c中的 __ declspec(dllexport) $ c> __ declspec(dllimport )在exe。

  CLASS_DECLSPEC 

编译器然后排序使用 __ declspec 的哪一个。



请参阅: http ://msdn.microsoft.com/en-us/library/8fskxacy(v = vs.80).aspx


I am making this DLL project and do not include any of compiler-specific macros.

The importer(.exe) files compiles clean and no error generated after importing methods in my DLL file. They are on different projects but same solution.

Do I mess up things here because of not using any of those calling convention?

My DLL files are in a namespace and implemented in OOP manner.

But sometimes or most of the times, the .lib does not generated so I got to switch the setting of my DLL to LIB and copy the generated .lib file and turn it back again to DLL file again.

And I don't even know if the library I am using is a .dll or .lib file.

Someone explains it to me clearly?

EDIT

I will add some situations where I am really confuse on when to use it.

suppose to be I have these:

namespace
{

    class Base abstract
    {

    public:
        Base()
        {
            //initialize base components
        }
        virtual void func() = 0;

    public:

        //interface
    };

    class Derive : public Base
    {

    public:
        Derive();
        void func() override;

    private:

        //interface

    };

}

Should I use declspec(dllexport) here:

  • The constructors (such as Base() and Derive()) ?

  • virtual functions? where will I put it? in the derive or in the base? or both?

  • public variables? (horrible!)

I see on other header files, they add extern into it. Do I need it here?

This will ruin all of my code syntaxes

answers please....

解决方案

It sounds as if you are linking in your library files that should be in the dll. So you are not using the dll's at all. Everything is going into the exe file.

Microsoft require the __declspec(dllexport) in the dll and __declspec(dllimport) in the exe. It gets complicated so there are macros to sort it out.

CLASS_DECLSPEC

The complier then sorts out which one of the __declspec's to use.

See:http://msdn.microsoft.com/en-us/library/8fskxacy(v=vs.80).aspx

这篇关于何时在C ++中使用__declspec(dllexport)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆