如何解决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 举报,一经查实,本站将立刻删除。