简体   繁体   English

如何在c ++项目之间共享类?

[英]how to share class between c++ projects?

My VS2012 Solution contains several VC++ projects. 我的VS2012解决方案包含几个VC ++项目。 I also have many common files that need to be shared. 我还有许多需要共享的常见文件。 It's easy to share enum's and structures. 分享枚举和结构很容易。 I just "include" corresponding header file and that's it. 我只是“包含”相应的头文件,就是这样。 I even don't need to compile my "commons" project! 我甚至不需要编译我的“公共”项目!

But what if I need to share more complex classes that contain both .h and .cpp files, and so need to be compiled? 但是如果我需要共享包含.h和.cpp文件的更复杂的类,那么需要编译呢?

And the most complicated question - can I share thread-safe singleton? 最复杂的问题 - 我可以共享线程安全的单例吗? I want to access it from different projects from different threads (but from one process). 我想从不同的线程(但来自一个进程)从不同的项目访问它。

I guess I should use something like static or dynamic linking, but i'm not sure. 我想我应该使用静态或动态链接之类的东西,但我不确定。 Probably someone can link step-by-step tutorial to solve such problem? 可能有人可以链接一步一步的教程来解决这个问题?

I would prefer something portable as I will move entire solution to Linux later. 我更喜欢可移植的东西,因为我稍后会将整个解决方案移植到Linux。

The projects that contain classes that you want to share should export their symbols. 包含要共享的类的项目应导出其符号。 When you create a DLL project in Visual Studio you can give it the option to "Export" symbols and it provides some boiler-plate code for you to use. 在Visual Studio中创建DLL项目时,可以为其提供“导出”符号选项,并提供一些样板代码供您使用。

In essence, in your libraries header file, it will give you: 实质上,在您的库头文件中,它将为您提供:

// myapi.h

#if defined(MYAPIEXPORTS)
    #define MYAPI __declspec(dllexport)
#else
    #define MYAPI __declspec(dllimport)
#endif

'MYAPIEXPORTS' is provided by the wizard, but it's a compiler preprocessor directive ONLY on the library itself. 'MYAPIEXPORTS'由向导提供,但它只是库本身的编译器预处理器指令。 Hence when you compile the library, MYAPI is for exporting and when the header file is included in your other projects, it will be for importing. 因此,当您编译库时, MYAPI用于导出, MYAPI文件包含在您的其他项目中时,它将用于导入。

Now let's look at a class you want to share. 现在让我们看一下你想要分享的课程。

// myclass.h

class MYAPI MyClass
{
public:
    MyClass();
    ~MyClass();
};


// myclass.cpp
#include "myClass.h"

MyClass::MyClass() { /* ... */ };
MyClass::~MyClass() { /* .... */ }

Your other projects then need to link with the resulting .lib file that is generated. 然后,您的其他项目需要链接生成的生成的.lib文件。

Note that if you have a template<> class contained entirely within a header file, you do not export it. 请注意,如果template<>类完全包含在头文件中,则不会将其导出。 That will behave like your enums and typedefs. 这将表现得像你的枚举和typedef。

To answer the second part of your question, yes a singleton defined in your library will be accessible to the main project also. 要回答问题的第二部分,是的,您的图书馆中定义的单身人士也可以访问主项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Delphi 和 C++ 项目之间共享通用定义 - Share common definition between Delphi and C++ projects 如何在Visual Studio中的项目之间共享C ++源代码文件? - How do I share C++ source code files between projects in Visual Studio? 如何在Visual C ++中的项目之间共享代码,或者在静态lib中为静态变量共享“未解析的外部符号” - How to share code between projects in Visual C++ or “unresolved external symbol” for static variables in static lib 如何在C ++中的命名空间之间共享变量? - How to share variables between namespaces in C++? 是否可以在C ++和C#之间共享“枚举类”? - Is it possible to share an “enum class” between C++ and C#? 在两个Visual Studio C ++项目之间扩展类 - Extending a class between two Visual Studio C++ projects 如何在类周围共享参数-C ++ - How to share parameters around a class - C++ C ++如何在cpp之间与extern共享常量-错误:指定了存储类 - C++ How to share constants with extern between cpp - Error: storage class specified 项目之间的C ++标头 - c++ headers between projects 如何在C ++和C#之间共享大字节数组 - How to share a big byte array between C++ and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM