Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid name collision with macros defined in Windows header files?

Tags:

I have some C++ code that includes a method called CreateDirectory(). Previously the code only used STL and Boost, but I recently had to include <windows.h> so I could look-up CSIDL_LOCAL_APPDATA.

Now, this code:

filesystem.CreateDirectory(p->Pathname()); // Actually create it... 

No longer compiles:

error C2039: 'CreateDirectoryA' : is not a member of ... 

Which corresponds to this macro in winbase.h:

#ifdef UNICODE #define CreateDirectory  CreateDirectoryW #else #define CreateDirectory  CreateDirectoryA #endif // !UNICODE 

The pre-processor is redefining my method call. Is there any possible way to avoid this naming collision? Or do I have to rename my CreateDirectory() method?

like image 811
Mike Willekes Avatar asked Feb 23 '10 21:02

Mike Willekes


People also ask

Can we define macros in a header file?

Yes, it would work. (Disregarding the problem with underscores that others have pointed out.) Directive #include "MyClass. h" just copies the whole content of file MyClass.

In which header file is the annual macro defined?

stddef. h is a header file in the standard library of the C programming language that defines the macros NULL and offsetof as well as the types ptrdiff_t, wchar_t, and size_t.


1 Answers

You will be better off if you just rename your CreateDirectory method. If you need to use windows APIs, fighting with Windows.h is a losing battle.

Incidently, if you were consistent in including windows.h, this will still be compiling. (although you might have problems in other places).

like image 115
John Knoeller Avatar answered Sep 30 '22 19:09

John Knoeller