If __declspec(selectany) is not used on the prototype but later used on a definition, GCC seems to ignore it, and you can get multiple-definition errors at link time.

That situation can arise in code like Microsoft's usbview utility that has multiple translation units including the following headers:

#include <windows.h>
#include <initguid.h>
#include <winioctl.h>

When I have two files that include those header files in that order and I try to compile them with "i686-w64-mingw32-gcc main.c other.c -o test.exe", I get a bunch of errors like this:

/home/david/tmp/ccB3GiUW.o:other.c:(.rdata+0x0): multiple definition of `GUID_DEVINTERFACE_DISK'
/home/david/tmp/ccRH9Bjf.o:main.c:(.rdata+0x0): first defined here
/home/david/tmp/ccB3GiUW.o:other.c:(.rdata+0x10): multiple definition of `GUID_DEVINTERFACE_CDROM'
/home/david/tmp/ccRH9Bjf.o:main.c:(.rdata+0x10): first defined here

This patch fixes that by adding __declspec(selectany) to the GUID declarations.

Thanks!

--David Grayson
From 7ce782720ce2190442bd835518ebc63e246b0050 Mon Sep 17 00:00:00 2001
From: David Grayson <davidegray...@gmail.com>
Date: Tue, 25 Apr 2017 07:41:45 -0700
Subject: [PATCH] guiddef.h: Use __declspec(selectany) on GUID declarations.

If __declspec(selectany) is not used on the prototype but later used
on a definition, GCC seems to ignore it, and you can get
multiple-definition errors at link time.

That situation can arise in code like Microsoft's usbview utility that
has multiple translation units including the following headers in this
order:  windows.h, initguid.h, winioctl.h.
---
 mingw-w64-headers/include/guiddef.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mingw-w64-headers/include/guiddef.h 
b/mingw-w64-headers/include/guiddef.h
index 8d0af1ed..6c9444cf 100644
--- a/mingw-w64-headers/include/guiddef.h
+++ b/mingw-w64-headers/include/guiddef.h
@@ -55,13 +55,13 @@ __extension__ template<typename T> const GUID 
&__mingw_uuidof();
 #ifdef __cplusplus
 #define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) EXTERN_C const GUID 
DECLSPEC_SELECTANY name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
 #else
 #define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) const GUID 
DECLSPEC_SELECTANY name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
 #endif
 #else
-#define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) EXTERN_C const GUID 
name
+#define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) EXTERN_C const GUID 
DECLSPEC_SELECTANY name
 #endif
 
 #define DEFINE_OLEGUID(name, l, w1, w2) DEFINE_GUID (name, l, w1, w2, 0xc0, 0, 
0, 0, 0, 0, 0, 0x46)
 
 #ifndef _GUIDDEF_H_
 #define _GUIDDEF_H_
-- 
2.12.1

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to