简体   繁体   English

c中的重新定义错误

[英]Redefinition error in c

I have to include "windows.h".The problem is redefinition, In windows.h: 我必须包含“windows.h”。问题是重新定义,在windows.h中:

_gdi_entry WINGDIAPI BOOL  WINAPI Polygon(__in HDC hdc, __in_ecount(cpt)
                                            CONST POINT *apt, __in int cpt);

Part of my code is(comes from other include): 我的部分代码是(来自其他包括):

typedef struct Polygon
{
    U8 numElements;
    PolygonPoints element[15];    
}Polygon;

I can't change the definition in my code. 我无法更改代码中的定义。 I am including windows.h for ShellExecute(). 我包括用于ShellExecute()的windows.h。 Tried to undef: 试图unf:

#ifdef Polygon 
#undef Polygon
#endif

Didn't help.. 没有帮助..

EDIT: Solved the problem with rodrigo's answer. 编辑:用罗德里戈的答案解决了这个问题。

The #undef Polygon won't work because Polygon() is a real function, not a macro. #undef Polygon不起作用,因为Polygon()是一个真正的函数,而不是宏。 In Windows, that trick mostly works for functions that takes at least one string argument, because of the Unicode vs ANSI stuff. 在Windows中,由于Unicode与ANSI的关系,该技巧主要适用于至少需要一个字符串参数的函数。

If you only want the ShellExecute() function you can omit all the GDI functions by defining NOGDI before including Windows.h : 如果您只想要ShellExecute()函数,则可以在包含Windows.h之前通过定义NOGDI来省略所有GDI函数:

#define NOGDI
#include <windows.h>

Alternatively you can add the NOGDI macro to the preprocessor project options of your IDE. 或者,您可以将NOGDI宏添加到IDE的预处理器项目选项中。

Separate the inclusion of windows.h and the definition of struct Polygon into two different files. 将包含windows.hstruct Polygon的定义分成两个不同的文件。

The file that includes windows.h will define a function that takes string parameters (and other non-struct Polygon parameter) and calls ShellExecute . 包含windows.h的文件将定义一个函数,该函数接受字符串参数(和其他非结构的Polygon参数)并调用ShellExecute The function is exported. 该功能已导出。

The file that defines struct Polygon must not include windows.h, but can convert the struct data to a string format and call the exported function defined in the other file. 定义struct Polygon的文件不能包含windows.h,但可以将struct数据转换为字符串格式并调用另一个文件中定义的导出函数。

Say hello to namespace collision. 跟命名空间冲突问好。 First things first, the best way to completely avoid this kind of problem is to prefix all your own identifiers with something unambiguous. 首先,完全避免此类问题的最佳方法是为所有自己的标识符添加明确的前缀。 For example if your project is called "foobar turboencabulator" a viable prefix would be fbtec… . 例如,如果您的项目被称为“foobar turboencabulator”,那么可行的前缀将是fbtec… You should do that unconditionally, because you'll never know which identifiers the OS and/or runtime environment may introduce in the future. 您应该无条件地执行此操作,因为您永远不会知道OS和/或运行时环境可能在将来引入哪些标识符。 The C language standard provisions a few namespaces (reserved prefixes for identifiers) for future expansion, but that's for C and not OSs and runtimes. C语言标准规定了一些名称空间(标识符的保留前缀)以供将来扩展,但这适用于C而不是操作系统和运行时。

You were already given answers how to split up compilation units. 您已经获得了如何拆分编译单元的答案。 But there's another possibility in your case and I strongly suggest you consider it. 但是在你的情况下还有另一种可能性,我强烈建议你考虑一下。 Your struct/typedef definition is 你的struct / typedef定义是

typedef struct Polygon
{
    U8 numElements;
    PolygonPoints element[15];    
}Polygon;

You tagged your question C so I suggest you embrace that fact: I suggest you drop the typedef . 你标记了你的问题C所以我建议你接受这个事实: 我建议你放弃typedef If you wrote just 如果你写的只是

struct Polygon
{
    U8 numElements;
    PolygonPoints element[15];    
};

without the typedef, then Polygon will exist in only the struct tag namespace, which is a different namespace than the one function identifiers reside in. So it will no longer collide with the Polygon function of Windows. 没有类型定义,那么Polygon在结构标签的命名空间,这是一个不同的命名空间比一个函数标识符居住在存在的。因此,这将不再与碰撞Polygon的Windows功能。

The downside to this is, that you'll have to change all variable and parameter definitions to refer to the struct namespace, ie Polygon a becomes struct Polygon a . 这样做的缺点是,您必须更改所有变量和参数定义以引用struct命名空间,即Polygon a变为struct Polygon a OTOH it's idiomatic in C to be explicit about the struct namespace. OTOH它在C中是惯用的,以明确struct命名空间。

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

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