15

I'm trying to use Boost.Thread on Windows using clang++.

While including boost/thread.hpp, I'm getting the following compile errors:

Using -DBOOST_USE_WINDOWS_H:

In file included from D:/env/boost/boost_1_58_0\boost/thread.hpp:13:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread.hpp:12:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread_only.hpp:15:
In file included from D:/env/boost/boost_1_58_0\boost/thread/win32/thread_data.hpp:11:
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:223:67: error: conflicting types for 'GetProcAddress'
                __declspec(dllimport) detail::farproc_t __stdcall GetProcAddress(void *, const char *);
                                                                  ^
/mingw/include\winbase.h:1675:27: note: previous declaration is here
WINBASEAPI FARPROC WINAPI GetProcAddress(HINSTANCE,LPCSTR);
                          ^
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread_only.hpp:15:
In file included from D:/env/boost/boost_1_58_0\boost/thread/win32/thread_data.hpp:11:
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:223:67: warning: redeclaration of 'GetProcAddress' should not add 'dllimport' attribute [-Wdll-attribute-on-redeclaration]
                __declspec(dllimport) detail::farproc_t __stdcall GetProcAddress(void *, const char *);
                                                                  ^
/mingw/include\winbase.h:1675:27: note: previous declaration is here
WINBASEAPI FARPROC WINAPI GetProcAddress(HINSTANCE,LPCSTR);
                          ^
In file included from D:\Documents\solid\solid\Solid.clion\src\Concurrent\tests\ThreadPoolTests.cpp:11:

In file included from D:/env/boost/boost_1_58_0\boost/thread.hpp:13:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread.hpp:12:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread_only.hpp:15:
In file included from D:/env/boost/boost_1_58_0\boost/thread/win32/thread_data.hpp:11:
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:225:56: error: functions that differ only in their return type cannot be overloaded
                __declspec(dllimport) void * __stdcall GetModuleHandleA(const char *);
                                                       ^
/mingw/include\winbase.h:1648:27: note: previous declaration is here
WINBASEAPI HMODULE WINAPI GetModuleHandleA(LPCSTR);
                          ^

In file included from D:/env/boost/boost_1_58_0\boost/thread.hpp:13:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread.hpp:12:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread_only.hpp:15:
In file included from D:/env/boost/boost_1_58_0\boost/thread/win32/thread_data.hpp:11:
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:225:56: warning: redeclaration of 'GetModuleHandleA' should not add 'dllimport' attribute [-Wdll-attribute-on-redeclaration]
                __declspec(dllimport) void * __stdcall GetModuleHandleA(const char *);
                                                       ^
/mingw/include\winbase.h:1648:27: note: previous declaration is here
WINBASEAPI HMODULE WINAPI GetModuleHandleA(LPCSTR);
                          ^

In file included from D:/env/boost/boost_1_58_0\boost/thread.hpp:13:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread.hpp:12:
In file included from D:/env/boost/boost_1_58_0\boost/thread/thread_only.hpp:15:
In file included from D:/env/boost/boost_1_58_0\boost/thread/win32/thread_data.hpp:11:
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:229:63: warning: redeclaration of 'GetTickCount' should not add 'dllimport' attribute [-Wdll-attribute-on-redeclaration]
                __declspec(dllimport) unsigned long __stdcall GetTickCount();
                                                              ^
/mingw/include\winbase.h:1758:25: note: previous declaration is here
WINBASEAPI DWORD WINAPI GetTickCount(VOID);
                        ^

Not using -DBOOST_USE_WINDOWS_H:

mingw32-make.exe[1]: *** [src/Core/CMakeFiles/Core.dir/all] Error 2
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1358:26: error: conflicting types for 'CreateEventA'
WINBASEAPI HANDLE WINAPI CreateEventA(LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR);
                         ^
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:166:55: note: previous declaration is here
                __declspec(dllimport) void* __stdcall CreateEventA(_SECURITY_ATTRIBUTES*,int,int,char const*);
                                                      ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1386:26: error: conflicting types for 'CreateMutexA'
WINBASEAPI HANDLE WINAPI CreateMutexA(LPSECURITY_ATTRIBUTES,BOOL,LPCSTR);
                         ^
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:164:55: note: previous declaration is here
                __declspec(dllimport) void* __stdcall CreateMutexA(_SECURITY_ATTRIBUTES*,int,char const*);
                                                      ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1412:26: error: conflicting types for 'CreateSemaphoreA'
WINBASEAPI HANDLE WINAPI CreateSemaphoreA(LPSECURITY_ATTRIBUTES,LONG,LONG,LPCSTR);
                         ^
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:165:55: note: previous declaration is here
                __declspec(dllimport) void* __stdcall CreateSemaphoreA(_SECURITY_ATTRIBUTES*,long,long,char const*);
                                                      ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1492:24: error: conflicting types for 'FileTimeToLocalFileTime'
WINBASEAPI BOOL WINAPI FileTimeToLocalFileTime(CONST FILETIME *,LPFILETIME);
                       ^
D:/env/boost/boost_1_58_0\boost/date_time/filetime_functions.hpp:58:45: note: previous declaration is here
        __declspec(dllimport) int __stdcall FileTimeToLocalFileTime(const FILETIME* lpFileTime, FILETIME* lpLocalFileTime);
                                            ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1648:27: error: functions that differ only in their return type cannot be overloaded
WINBASEAPI HMODULE WINAPI GetModuleHandleA(LPCSTR);
                          ^
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:225:56: note: previous declaration is here
                __declspec(dllimport) void * __stdcall GetModuleHandleA(const char *);
                                                       ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1675:27: error: conflicting types for 'GetProcAddress'
WINBASEAPI FARPROC WINAPI GetProcAddress(HINSTANCE,LPCSTR);
                          ^
D:/env/boost/boost_1_58_0\boost/thread/win32/thread_primitives.hpp:223:67: note: previous declaration is here
                __declspec(dllimport) detail::farproc_t __stdcall GetProcAddress(void *, const char *);
                                                                  ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1729:24: error: conflicting types for 'GetSystemTime'
WINBASEAPI VOID WINAPI GetSystemTime(LPSYSTEMTIME);
                       ^
D:/env/boost/boost_1_58_0\boost/date_time/filetime_functions.hpp:59:46: note: previous declaration is here
        __declspec(dllimport) void __stdcall GetSystemTime(SYSTEMTIME* lpSystemTime);
                                             ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:1734:24: error: conflicting types for 'GetSystemTimeAsFileTime'
WINBASEAPI void WINAPI GetSystemTimeAsFileTime(LPFILETIME);
                       ^
D:/env/boost/boost_1_58_0\boost/date_time/filetime_functions.hpp:57:46: note: previous declaration is here
        __declspec(dllimport) void __stdcall GetSystemTimeAsFileTime(FILETIME* lpFileTime);
                                             ^
In file included from /mingw/include\windows.h:50:
/mingw/include\winbase.h:2159:24: error: conflicting types for 'SystemTimeToFileTime'
WINBASEAPI BOOL WINAPI SystemTimeToFileTime(const SYSTEMTIME*,LPFILETIME);
                       ^
D:/env/boost/boost_1_58_0\boost/date_time/filetime_functions.hpp:60:45: note: previous declaration is here
        __declspec(dllimport) int __stdcall SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime, FILETIME* lpFileTime);
                                            ^
saarraz1
  • 2,999
  • 6
  • 29
  • 44
  • Apologies if this seems picky: if you are using clang++, you are **not** using MinGW. It may be that the clang folks have adopted header's taken from MinGW.org, for use with their compiler, but that doesn't make it a MinGW compiler, (and it is **not** supported by MinGW.org). This is a clang issue; it is **not** a MinGW issue. – Keith Marshall May 03 '15 at 05:55
  • You are using MinGW headers for compilation of Win32 code. That won't work. – StenSoft May 14 '15 at 22:24
  • Can you be more specific? The error seems to arise from boost's headers, so which win32 code are you referring to? – saarraz1 May 17 '15 at 05:46
  • 1
    What's your mingw version? I have 4.8.2 and 4.9.1 (GCC, though) and none of them declare these functions on `winbase.h`. **As an experiment**, you could try to comment lines 1648, 1675 and 1758 on your version of this header (since there's no way to deactivate this using a define or flag, neither on `winbase.h` nor on `thread_primitives.hpp`). Commenting the analogous lines on `thread_primitives` could also work. – Not a real meerkat May 19 '15 at 13:30
  • @CássioRenan Well this actually seems to solve the problem! (IDK if it will cause more problems later on, but for now all's good) I can't award you the bounty because this is not an answer (try to post it as an answer if you still can) – saarraz1 May 22 '15 at 19:00
  • Don't worry about it, since it's not really an answer. If this worked, try then updating your mingw headers (maybe using a newer version of clang? I never used it on windows, so I don't know if it has a full installation of mingw or just copies of the headers), if possible. Since newer versions don't have the offending lines, it will solve your problem while preventing anything bad from happening due to messing up headers you shouldn't be messing with :) – Not a real meerkat May 22 '15 at 19:37
  • @saarraz1 Have you considered accepting the answer of [Tom Tanner](http://stackoverflow.com/a/33599277/2436175)? – Antonio Apr 21 '16 at 11:46
  • @Keith Clang can use MinGW-w64 headers just fine, just as it can use MSVS headers. LLVM/Clang doesn't ship with or rely on MinGW headers, why would a compiler ship platform headers anyway? – rubenvb Dec 13 '18 at 21:49
  • @saarraz1 I strongly suggest you try (MSYS2's environment)[www.msys2.org] and its Clang in combination with MinGW-w64 . I bet it works out of the box. In any case, it at least ships more modern tools, headers, and libraries. – rubenvb Dec 13 '18 at 21:53
  • @rubenvb, So what? mingw-w64 is NOT MinGW; it's a rogue project which improperly uses the MinGW trademark, without the permission of the trademark owner (Software in the Public Interest registered it on behalf of MinGW.org, so you need their permission, which you don't have). – Keith Marshall Dec 31 '18 at 23:07

2 Answers2

4

Had similar issue and fixed it using the boost_system argument flag. Try this:

clang++ test.cpp -lboost_thread -lboost_system

Here's my clang info:

~$clang++ --version
  clang version 3.2 (trunk 163783)
  Target: x86_64-unknown-linux-gnu
  Thread model: posix

This works with g++ too:

~$g++ --version
  g++ (GCC) 4.9.2
  Copyright (C) 2014 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Also, have a look at these threads here:

Compiling C++ source file using Boost.Thread

https://stackoverflow.com/questions/19017335/cmake-force-use-of-non-mt-boost-libraries

Community
  • 1
  • 1
Constantinos Glynos
  • 2,952
  • 2
  • 14
  • 32
  • 2
    I wonder in which way adding a linker option could solve a problem happening at the stage of creating an obj file (ad compile stage, not at linking stage). This answer does not address the actual question at all. – Antonio Apr 07 '16 at 10:03
4

I've just run across this trying to run include-what-you-use on a project under windows.

The program compiles fine with QT creator but to get it to compile with include-what-you-use I had to add -DBOOST_USE_WINDOWS_H to the compile and to block out the extern "C" block at line 220ish in 'thread_primitives.hpp' with #ifndef _APISETLIBLOADER_...#endif

At least I know that's safe, but it's not at all clear why it doesn't work normally.

Geezer
  • 5,600
  • 18
  • 31
Tom Tanner
  • 9,244
  • 3
  • 33
  • 61