Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#17677 closed build error (fixed)

::AttachConsole has not been declared

Reported by: zackchen Owned by: Vadim Zeitlin <vadim@…>
Priority: normal Milestone:
Component: wxMSW Version: 3.1.0
Keywords: AttachConsole MinGW Cc:
Blocked By: Blocking:
Patch: no

Description (last modified by vadz)

compile:mingw 5.3
OS:win7 X64

g++ -c -o gcc_mswu\corelib_activex.o  -O2 -mthreads  -DHAVE_W32API_H -D__WXMSW__
   -DNDEBUG    -D_UNICODE -I..\..\lib\gcc_lib\mswu -I..\..\include  -W -Wall -DW
XBUILDING -I..\..\src\tiff\libtiff -I..\..\src\jpeg -I..\..\src\png -I..\..\src\
zlib -I..\..\src\regex -I..\..\src\expat\lib -DwxUSE_BASE=0   -Wno-ctor-dtor-pri
vacy   -MTgcc_mswu\corelib_activex.o -MFgcc_mswu\corelib_activex.o.d -MD -MP ../
../src/msw/ole/activex.cpp
g++ -c -o gcc_mswu\corelib_app.o  -O2 -mthreads  -DHAVE_W32API_H -D__WXMSW__   -
DNDEBUG    -D_UNICODE -I..\..\lib\gcc_lib\mswu -I..\..\include  -W -Wall -DWXBUI
LDING -I..\..\src\tiff\libtiff -I..\..\src\jpeg -I..\..\src\png -I..\..\src\zlib
 -I..\..\src\regex -I..\..\src\expat\lib -DwxUSE_BASE=0   -Wno-ctor-dtor-privacy
   -MTgcc_mswu\corelib_app.o -MFgcc_mswu\corelib_app.o.d -MD -MP ../../src/msw/a
pp.cpp
../../src/msw/app.cpp: In member function 'bool {anonymous}::wxConsoleStderr::Do
Init()':
../../src/msw/app.cpp:380:11: error: '::AttachConsole' has not been declared
     if ( !::AttachConsole(ATTACH_PARENT_PROCESS) )
           ^
makefile.gcc:14210: recipe for target 'gcc_mswu\corelib_app.o' failed
mingw32-make[1]: *** [gcc_mswu\corelib_app.o] Error 1
mingw32-make[1]: Leaving directory 'D:/DEV/GCC/msys/1.0/home/Administrator/wxWid
gets-3.1.0/build/msw'

Change History (12)

comment:1 Changed 4 years ago by ScratchMonkey

  • Summary changed from may be an bug to ::AttachConsole has not been declared

Adding a useful title. No idea what's wrong.

comment:2 follow-up: Changed 4 years ago by vadz

  • Component changed from base to wxMSW
  • Description modified (diff)
  • Keywords MinGW added
  • Milestone 3.1.1 deleted

I don't have MinGW-w64 5.3 yet, so I can't easily check this, but is AttachConsole() declaration present in x86_64-w64-mingw32\include\wincon.h file under the compiler root installation directory? It is there in the earlier versions and it would be really surprising if it were not present in the later ones...

P.S. Also reformatted the output in the ticket for readability.

comment:3 Changed 4 years ago by ericj

AttachConsole is only available under Windows XP or higher. This error usually occurs when _WIN32_WINNT is not defined or has a value lower than 0x0501.

comment:4 in reply to: ↑ 2 Changed 4 years ago by zackchen

Replying to vadz:

I don't have MinGW-w64 5.3 yet, so I can't easily check this, but is AttachConsole() declaration present in x86_64-w64-mingw32\include\wincon.h file under the compiler root installation directory? It is there in the earlier versions and it would be really surprising if it were not present in the later ones...

P.S. Also reformatted the output in the ticket for readability.

I don't have MinGW-w64 5.3 too,I use mingw32 5.3,I am not a software engineer,so i am unable to solve this problem,I hope this bug can be resolved as soon as possible​,My English is not good, I do not know whether to clear the table​

C:\Users\Administrator>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=d:/dev/gcc/bin/../libexec/gcc/mingw32/5.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-5.3.0/configure --build=x86_64-pc-linux-gnu --host=m
ingw32 --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i58
6 --enable-languages=c,c++,objc,obj-c++,fortran,ada --enable-static --enable-sha
red --enable-threads=posix --with-dwarf2 --disable-sjlj-exceptions --enable-vers
ion-specific-runtime-libs --enable-libstdcxx-debug --with-tune=generic --enable-
libgomp --disable-libvtv --enable-nls
Thread model: posix
gcc version 5.3.0 (GCC)

comment:5 Changed 4 years ago by vadz

You need to check the header I mentioned, you don't have to be a software engineer to search for a string appearing in a file.

comment:6 Changed 4 years ago by pb101

I downloaded mingw-get-setup.exe from ​https://sourceforge.net/projects/mingw/
Using the setup tool, I installed two packages: mingw32-base (reported in the setup tool as version 2013072200) and mingw32-gcc-g++ (reported in the setup tool as version 5.3.0-3).

In my MinGW\include\wincon.h dated September 20, 2016 there is only one declaration of AttachConsole (lines 133-136):

#if (_WIN32_WINNT >= 0x0501)
#define ATTACH_PARENT_PROCESS	((DWORD)-1)
BOOL WINAPI AttachConsole(DWORD);
#endif

comment:7 Changed 4 years ago by vadz

And as we have #define _WIN32_WINNT 0x0603 in include/wx/msw/wrapwin.h, it really ought to be visible to the compiler, I have no idea why would this error occur...

I honestly don't know what to do about it, there clearly seems to be some problem, but it risks being impossible to resolve it without more information from the OP.

comment:8 Changed 4 years ago by vadz

According to the comment in #17762, pb101 was indeed able to reproduce the bug, which makes things more interesting. I'll try to reproduce it myself, but in the meanwhile I suspect that something in wx/defs.h (which is the only header included before wx/msw/wrapcctl.h which definitely predefines _WIN32_WINNT to a high enough value when not using PCH) includes windows.h before we get the chance of including it ourselves. We need to find where does it happen and include wx/msw/wrapwin.h before it does.

comment:9 Changed 4 years ago by pb101

  • Status changed from new to confirmed

To recap:

  1. MinGW/GCC 5.3 (see comment:5 for exact version) sets (if it has not been defined already) _WIN32_WINNT to _WIN32_WINDOWS (in MinGW\include\sdkddkver.h), which evaluates to 0x0400 (Windows 95). Just for comparison, TDM-64-1 with GCC 5.1 sets _WIN32_WINNT to _WIN32_WINNT_WS03 (0x0502).
  2. wxWidgets GIT head sets (if it has not been defined already) _WIN32_WINNT to 0x0603 (in WXWIN\include\wx\msw\wrapwin.h).
  3. MinGW/GCC 5.3 declares AttachConsole only when _WIN32_WINNT >= 0x0501, see comment:5.

I am not a programmer and therefore I am not capable of doing a proper analysis so I did a simple thing: I added #pragma warning reporting the value of _WIN32_WINNT to:

  1. The end of WXWIN\include\wx\msw\wrapwin.h
  2. The end of MinGW\include\sdkddkver.h
  3. Just before the AttachConsole declaration #ifdef wrapper in MinGW\include\wincon.h

When building wxWidgets clean with

mingw32-make -f makefile.gcc SHARED=1 UNICODE=1 USE_OPENGL=1 USE_GDIPLUS=1 BUILD=release CXXFLAGS="-std=gnu++11 -Wno-unused-value"

I ended with

g++ -c -o gcc_mswudll\coredll_app.o  -O2 -mthreads  -DHAVE_W32API_H -D__WXMSW__
  -DNDEBUG    -D_UNICODE -I..\..\lib\gcc_dll\mswu -I..\..\include  -W -Wall -DWX
BUILDING -I..\..\src\tiff\libtiff -I..\..\src\jpeg -I..\..\src\png -I..\..\src\z
lib -I..\..\src\regex -I..\..\src\expat\lib -DWXUSINGDLL -DWXMAKINGDLL_CORE -Dwx
USE_BASE=0   -Wno-ctor-dtor-privacy  -std=gnu++11 -Wno-unused-value -MTgcc_mswud
ll\coredll_app.o -MFgcc_mswudll\coredll_app.o.d -MD -MP ../../src/msw/app.cpp
In file included from c:\mingw\include\w32api.h:59:0,
                 from c:\mingw\include\_mingw.h:73,
                 from ..\..\include/wx/msw/gccpriv.h:36,
                 from ..\..\include/wx/platform.h:355,
                 from ..\..\include/wx/defs.h:20,
                 from ..\..\include/wx/wxprec.h:12,
                 from ../../src/msw/app.cpp:20:
c:\mingw\include\sdkddkver.h:292:76: note: #pragma message: Hello from MinGW\inc
lude\sdkddkver.h, _WIN32_WINNT is
 #pragma message "Hello from MinGW\\include\\sdkddkver.h, _WIN32_WINNT is " _WIN
32_WINNT
                                                                            ^
c:\mingw\include\wincon.h:134:73: note: #pragma message: Hello from MinGW\includ
e\wincon.h, _WIN32_WINNT is
 #pragma message "Hello from MinGW\\include\\wincon.h, _WIN32_WINNT is " _WIN32_
WINNT
                                                                         ^
..\..\include/wx/msw/wrapwin.h:57:83: note: #pragma message: Hello from WXWIN\in
clude\wx\msw\wrapwin.h, _WIN32_WINNT is
 #pragma message "Hello from WXWIN\\include\\wx\\msw\\wrapwin.h, _WIN32_WINNT is
 " _WIN32_WINNT

   ^
../../src/msw/app.cpp: In member function 'bool {anonymous}::wxConsoleStderr::Do
Init()':
../../src/msw/app.cpp:416:11: error: '::AttachConsole' has not been declared
     if ( !::AttachConsole(ATTACH_PARENT_PROCESS) )
           ^
makefile.gcc:12792: recipe for target 'gcc_mswudll\coredll_app.o' failed
mingw32-make: *** [gcc_mswudll\coredll_app.o] Error 1

I believe that from the log above it is clear in which order and from where are the header files included and thus where the issue comes from.

Unfortunately I do not know how to fix this but perhaps this information proves helpful to someone who can.

comment:10 Changed 4 years ago by Vadim Zeitlin <vadim@…>

  • Owner set to Vadim Zeitlin <vadim@…>
  • Resolution set to fixed
  • Status changed from confirmed to closed

In 7477e94b2d456c93abb0cb65957b0e8f34a4f4a7/git-wxWidgets:

Fix build problems due to "missing" newer MSW APIs with MinGW 5.3.0

Set WINVER/_WIN32_WINNT ourselves before letting MinGW to set them to very low
values corresponding to Windows 2000 on its own and preventing our code from
seeing any later additions to the Windows API, such as AttachConsole()
function used in src/msw/app.cpp.

Closes #17677.

comment:11 Changed 3 years ago by pb101

I believe that if possible the fix for this issue should be backported to the 3.0 branch. Likewise for #17762

comment:12 Changed 3 years ago by pb101

Please disregard the backport request, the 3.0 branch is not affected by this issue (AttachConsole is loaded dynamically there).

I am sorry for the noise.

Note: See TracTickets for help on using tickets.