如何定义函数在库中

How to define the function out side the Library?

本文关键字:函数 定义 何定义      更新时间:2023-10-16

我们有一些动态插件dll,这些插件是通过单独的保护生成的,并且在启动软件时使用。插件dll代码使用主项目中的一些标头文件。在编译插件代码时,如果主要项目标头文件包含某些功能和类别的定义,那么当时我会遇到错误,例如未解决的外部符号。

由于插件代码未找到(主项目(函数的定义,因此期望此错误。

注意:插件必须在主项目编译之前进行编译。

在两个情况下,插入代码已编译。1(如果主项目标头文件具有纯虚拟函数,那么它就是工作 完美(插头代码编译正常(。2(如果我要在类中编写函数定义。

我尝试了内联函数,但它不起作用。

---------------------
*** Main Project ***
---------------------
 ->This project generate .exe file 
mainProject.h
-------------
class MainProjectHeder{
   virtual void foo() = 0;    // this function is work 
   void foo1();              // this function giving me error like unresolved external symbol  
   void foo2{                // this work
     print("hello");
   };
};
void MainProjectHeder::foo1(){
     print("function not working");
}  

ExperimentFactoryInterface.h 
----------------------------
#include <MainProjectHeder.h>
class ExperimentFactoryInterface {
public:
    virtual ~ExperimentFactoryInterface() {}
    virtual MainProjectHeder* CreateExperiment(const QVariant& = QVariant()) = 0;
};

----------------------
  *** Pulgin code  ***  
----------------------
->This Project is generate DLL file
Experiment.h 
-------------
#include <mainProject.h>
class Experiment : public MainProjectHeder{
      virtual void foo();  
};
void Experiment:foo(){
    print("work fine");
}  

Factory.h
----------
#include "cyclicvoltammetry.h"
#include <ExperimentFactoryInterface.h>
class Factory : public ExperimentFactoryInterface {
public:
    MainProjectHeder* CreateExperiment(const QVariant& = QVariant());
};

MainProjectHeder* Factory::CreateExperiment(const QVariant&) {
    return new Experiment;
}

编译插件代码时;如何将函数定义void foo1();的定义告知编译器,在项目或另一个项目中写出一边?

类的成员函数是一个函数,它在类定义中具有其定义或原型,就像任何其他变量一样。它在其是成员的类的任何对象上运行,并且可以访问该对象的所有类成员。