微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C ++中的类声明

如何解决C ++中的类声明

我有一个班级: // add data if(!_instruments.TryGetValue(tick.Instrument,out var data)) _instruments.Add(tick.Instrument,data = new Data()); data.Add(tick); ... var Ohcl = _instruments[TickData.Instrument]; // example reading the data Console.WriteLine(Ohcl.High);

RTC_EXPORT定义为

public class Data
{
   private readonly Queue<Tick> _queue = new Queue<Tick>();
   public decimal High => _queue.Max(x => x.LatestPrice);
   public decimal Low => _queue.Min(x => x.LatestPrice);
   public decimal Open => _queue.LastOrDefault()?.LatestPrice ?? 0;
   public decimal Close => _queue.FirstOrDefault()?.LatestPrice ?? 0;

   public void Add(Tick tick)
   {
      _queue.Enqueue(tick);
      Update();
   }

   public void Update()
   {
      var age = DateTime.Now.AddMinutes(-5);
      while (_queue.Any() && _queue.Peek().TimeStamp < age)
         _queue.Dequeue();
   }
}

class RTC_EXPORT PeerConnectionInterface : public rtc::RefCountInterface { ... };中的RTC_EXPORT有什么作用?

通常,我们在c ++中将类定义#ifndef RTC_BASE_SYstem_RTC_EXPORT_H_ #define RTC_BASE_SYstem_RTC_EXPORT_H_ // RTC_EXPORT is used to mark symbols as exported or imported when WebRTC is // built or used as a shared library. // When WebRTC is built as a static library the RTC_EXPORT macro expands to // nothing. #ifdef WEBRTC_ENABLE_SYMBOL_EXPORT #ifdef WEBRTC_WIN #ifdef WEBRTC_LIBRARY_IMPL #define RTC_EXPORT __declspec(dllexport) #else #define RTC_EXPORT __declspec(dllimport) #endif #else // WEBRTC_WIN #if __has_attribute(visibility) && defined(WEBRTC_LIBRARY_IMPL) #define RTC_EXPORT __attribute__((visibility("default"))) #endif #endif // WEBRTC_WIN #endif // WEBRTC_ENABLE_SYMBOL_EXPORT #ifndef RTC_EXPORT #define RTC_EXPORT #endif #endif // RTC_BASE_SYstem_RTC_EXPORT_H_ 。额外的MACRO通常会做什么?

解决方法

除了德米特里(Dmitry)的答案,我还想添加一些实用的工具来检查__declspec()的影响。考虑以下源文件:

// library.cpp
__declspec(dllexport) int func(int x) { return 2 * x; }

在您的开始中从Visual Studio安装中启动本机工具命令提示符 菜单。在那里,您可以使用以下命令编译library.cpp并将其链接到DLL:

> cd <Directory containing library.cpp>
> cl /c library.cpp
> link library.obj /DLL /NOENTRY

library.dll旁边应该有一个新创建的library.cpp。以下命令检查导出的符号:

> dumpbin /EXPORTS library.dll

您应该会看到以下内容:

Dump of file library.dll

File Type: DLL

  Section contains the following exports for library.dll

    00000000 characteristics
    FFFFFFFF time date stamp
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00001000 ?func@@YAHH@Z

如您所见,library.dll导出函数?func@@YAHH@Z,这是C ++在内部命名函数func的方式。如果省略__declspec(dllexport),则不会看到此导出。同样,只有带有__declspec(dllexport)注释的类将导出其所有成员函数。

摘要:要从DLL中导出功能,您必须使用__declspec(dllexport)对其进行注释。

现在,从DLL导出类的常用方法是在标头中定义这样的#ifdef开关:

// header.h
#pragma once

#ifdef BUILD_LIBRARY
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif

EXPORT int func(int x);

在库的源代码中,您定义了以下魔术宏BUILD_LIBRARY

// library.cpp
#define BUILD_LIBRARY
#include "header.h"

int func(int x) { return 2 * x; }

因此该函数将从您的库中导出。 DLL的使用者将包括header.h,但不应定义BUILD_LIBRARY

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

int main() {
    std::cout << func(10) << std::endl;
    return 0;
}

由于此编译单元未定义BUILD_LIBRARY,因此EXPORT宏等于__declspec(dllimport)

,

宏在您的代码段中定义:

SESSION_KEY

这允许对动态库和调用者代码使用相同的类声明。

dllexport和dllimport存储类属性为 Microsoft对C和C ++语言的扩展。您可以使用 它们可以将功能,数据和对象导出或导入到 DLL。

https://docs.microsoft.com/en-us/cpp/cpp/dllexport-dllimport?view=vs-2019

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?