简体   繁体   English

我可以在DLL中使用前向声明吗?

[英]Can I use forward declarations inside a DLL?

I have a main project and a DLL project. 我有一个主项目和一个DLL项目。 The main executable links to the DLL. 主要可执行文件链接到DLL。 In the main project I have a function: 在主项目中,我有一个功能:

int func(char *str)
{
    .....
}

In the DLL project, I want to use this function. 在DLL项目中,我想使用此功能。 Can I forward declare this function and use it? 我可以转发声明此功能并使用它吗? Like: 喜欢:

int func(char *str);

int dllFunc()
{
    ...
    status = func(str);
    ...
}

You cannot use forward declaration forward declaration is something different. 您不能使用前向声明前向声明是不同的。 If you want call some method in dll, you have to use GetProcAdress or Implicit linking . 如果要在dll中调用某些方法,则必须使用GetProcAdressImplicit linking

Anyway, It look that you want to call method placed inside you executable from some DLL, it can be done as far as I know. 无论如何,看起来您想从某个DLL调用可执行文件中放置的方法,据我所知可以完成。 And it doesn't make much sense, because DLL is designed to be shared between application not main executable. 而且这没有多大意义,因为DLL被设计为在应用程序而非主要可执行文件之间共享。

One (fairly common) way of doing it is to provide a callback from the application, ie 一种(相当普遍的)方法是从应用程序提供回调,即

In DLL: 在DLL中:

typedef void (*callback)();

callback my_callback;

void dll_register_callback(callback app_callback) { my_callback = app_callback; }

void do_things() { my_callback(); }

In application: 在应用中:

void cb() {}

dll_register_callback(cb);

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

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