简体   繁体   English

C ++ cpp文件作为模块

[英]C++ cpp files as modules

Sorry if I am duplicating other question but I don't know how to google it. 抱歉,如果我要重复其他问题,但是我不知道如何使用它进行搜索。 I want to add some minor modularity to my program: some .cpp files should be compiled as "modules". 我想在程序中添加一些次要的模块化:一些.cpp文件应编译为“模块”。 The main requirement is that I should be able to add modules by just adding new .cpp file to the project, without changing other files in any way. 主要要求是我应该能够仅通过向项目中添加新的.cpp文件来添加模块,而无需以任何方式更改其他文件。

Achieving this looks easy with dynamic loading libraries. 使用动态加载库可以轻松实现这一目标。 Main program can scan some folder for all .dll files, load each of them and call exported "load" symbol from every module. 主程序可以扫描某个文件夹中的所有.dll文件,加载每个文件并从每个模块调用导出的“加载”符号。 In destructor main program can call "unload" symbols so modules can clean up. 在析构函数中,主程序可以调用“卸载”符号,以便模块可以清理。

I want the same but with monolithic program. 我想要相同但带有整体程序。 Is there any way for .cpp files to register themselves so main program can call their init() functions at some point? .cpp文件有什么方法可以自己注册,以便主程序可以在某个时候调用其init()函数? Or for main program to find all such modules? 还是要在主程序中找到所有此类模块?

How it is done in Linux kernel? 如何在Linux内核中完成? I know simple adding .c files makes them work somehow. 我知道简单地添加.c文件可以使它们以某种方式工作。

You can use a static dummy variable inside every cpp file and initialize it by a lambda doing the initialization and registration. 您可以在每个cpp文件中使用一个静态虚拟变量,并通过lambda对其进行初始化和注册。 Trivial example: 琐碎的例子:

// registration.h

void register_cpp (std::string name);
void print_cpps ();

// registration.cpp
namespace {
   std::vector<std::string> & names () {
      static std::vector<std::string> names_ {};
      return names_;
   }
}

void register_cpp (std::string name) {
   names ().push_back (name); // well, push_back(std::move (name)) would be more efficient
}

void print_cpps () {
    for (auto && name : names()) { std::cout << name << "\n"; }
}

// a.cpp

#include "registration.h"
static std::nullptr_t e = ([] () { register_cpp ("a"); return nullptr; }) ();

// b.cpp

#include "registration.h"
static std::nullptr_t e = ([] () { register_cpp ("b"); return nullptr; }) ();

// main.cpp
#include "registration.h"
int main () {
   print_cpps ();
   return 0;
}

I think you need names_ to be a static local variable in order to make sure that it is initialized before being accessed for the first time. 我认为您需要names_是一个静态局部变量,以确保在首次访问它之前已对其进行了初始化。

You can add new .cpp files to a statically linked application without changing the existing code using a registration interface that is exposed from the main application (singleton). 您可以使用从主应用程序(单个)公开的注册界面将新的.cpp文件添加到静态链接的应用程序,而无需更改现有代码。

Something like 就像是

App.h: App.h:

 struct IModule {
     virtual void init() = 0;
     virtual ~IModule() {}
 };

 class App {
 public:
      void registerModule(IModule* newModule); // Stores the interface
                                               // pointer of an additional
                                               // module
      static App& instance() {
           static App theInstance;
           return theInstance;
      }
 };

NewModule.cpp: NewModule.cpp:

 #include "App.h"

 class NewModule : public IModule {
 public:
      void init();
 private:
      NewModule() {
          App::getInstance().registerModule(this);
      }

      static NewModule instance;
 };

 NewModule NewModule::instance;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM