简体   繁体   English

结合 C\C++ 翻译单元 state?

[英]combining C\C++ translation units state?

I have a main.c file which includes a guarded (#ifndef...) c header, called mydefinitions.h.我有一个 main.c 文件,其中包含一个受保护的 (#ifndef...) c header,名为 mydefinitions.h。

In the header i declare an extern function, lets call it CppMain, to which i then call from the main.c file.在 header 中,我声明了一个外部 function,我们称之为 CppMain,然后我从 main.c 文件调用它。

The CppMain function is defined in cppmain.cpp file which includes the (guarded) mydefinitions.h file as an extern "C" header. CppMain function 在 cppmain.cpp 文件中定义,其中包括(受保护的)mydefinitions.h 文件作为外部“C”header。

The problem i am encountering is that a certain function, INIT_Pfn, which is declared and defined in the mydefinitions.h file is being defined multiple times (compiler argues multiple definitions of said method).我遇到的问题是在 mydefinitions.h 文件中声明和定义的某个 function,INIT_Pfn 被多次定义(编译器争论所述方法的多个定义)。

to my understanding the compiler is processing cppmain.cpp as a result of the definition of the said extern CppMain function but reprocess the mydefinitions.h since it is outside the scope of main.c and therefore the guard (#ifndef...) is being reinitialized - which, to me, is totally reasonable.据我了解,由于上述 extern CppMain function 的定义,编译器正在处理 cppmain.cpp 但重新处理 mydefinitions.h,因为它在 main.c 的 scope 之外,因此守卫(#ifndef ...)是被重新初始化——对我来说,这是完全合理的。

The main gist of the issue is that i'm trying to implement some logic in C++ as opposed to doing it all in C, but to keep the global scope\state of the main.c program translation.这个问题的主要要点是,我试图在 C++ 中实现一些逻辑,而不是在 C 中实现所有逻辑,但要保持 main.c 程序翻译的全局范围\状态。

Is there any way to avoid taking out the INIT_Pfn out of the mydefinitions.h file?有什么办法可以避免从 mydefinitions.h 文件中取出 INIT_Pfn 吗? Any other way you might think of implementing this solution without affecting mydefinitions.h?您可能会想到在不影响 mydefinitions.h 的情况下实施此解决方案的任何其他方式? the file also defines a global variable which has dependencies all over the source...该文件还定义了一个全局变量,它在整个源代码中都有依赖性……

EDITTED (added code snippets):编辑(添加代码片段):

mydefinitions.h:我的定义.h:

#ifndef MyDefinitions
#define MyDefinitions

unsigned int GLOBAL_STATE = 0;
extern void CppMain();

#endif // !MyDefinitions

MyCPPFile.cpp:我的 CPP 文件.cpp:

#ifndef MyCPPFile
#define MyCPPFile

extern "C" {
#include "mydefinitions.h"
}

extern "C" void CppMain()
{
    // cpp code here
}
#endif // !MyCPPFile

main.c file: main.c 文件:

#include "mydefinitions.h"

int main(int argc, char *argv[])
{
    CppMain();
}

What's happening is that every object file compiled from source contains both the integer GLOBAL_STATE as well as a runtime initialiser for it.发生的事情是,从源代码编译的每个 object 文件都包含 integer GLOBAL_STATE以及它的运行时初始化程序。 You only need it defined once.你只需要定义一次。

In the header file, declare the variable extern :在 header 文件中,声明变量extern

extern unsigned int GLOBAL_STATE;

In your main C file, define it:在您的主 C 文件中,定义它:

unsigned int GLOBAL_STATE = 0;

You don't need the #define MyCPPFile malarky in the CPP file.您不需要 CPP 文件中的#define MyCPPFile恶意代码。

If the function is written in common subset of C and C++, then you could declare the function static inline and it should work.如果 function 写在 C 和 C++ 的公共子集中,那么您可以声明 function static 内联,它应该可以工作。

The variable has to be defined elsewhere or has to be static since C doesn't have inline variables.该变量必须在别处定义,或者必须是 static,因为 C 没有内联变量。

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

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