Search code examples
cwinapiprintfcygwinundefined-reference

Undefined Windows reference _ftprintf


I just recently installed GCC onto Windows through Cygwin64. I am trying to compile a program that contains the Windows _ftprintf function (as shown here in the MyHandleError Function and documented here), however, I am getting an undefined reference.

For example:

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

void main(void){
    _ftprintf(stderr, TEXT("Test Err. \n"));
}

The following example produces the following error when compiling as gcc test.c:

test.c: In function 'main':
test.c:6:5: warning: implicit declaration of function '_ftprintf'; did you mean '__eprintf'? [-Wimplicit-function-declaration]
 _ftprintf(stderr, TEXT("Test Err. \n"));
 ^~~~~~~~~
 __eprintf
/cygdrive/c/Users/Dan/AppData/Local/Temp/ccEa2phm.o:test.c:(.text+0x21): undefined reference to `_ftprintf'
/cygdrive/c/Users/Dan/AppData/Local/Temp/ccEa2phm.o:test.c:(.text+0x21): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `_ftprintf'
collect2: error: ld returned 1 exit status

Am I missing a flag or something?


Solution

  • If it is declared at all, it will be declared in tchar.h - open that file and check. However, since the linker also cannot resolve it it would appear that it does not it is not defined in the library at all.

    I would not expect this to work in Cygwin which is intended to support the Linux/GNU API on Windows - it is not appropriate if you are targeting the Windows/Microsoft API in any case. Moreover Cygwin uses UTF-8 rather then Windows UTF-16 encoding so it is not really surprising that Windows' wide-character support is not available.

    If you are writing Windows specific code and want to use GCC, then you will be better off using MinGW - it is the GCC compiler with the Windows C run-time library. A better alternative to Cygwin if you want a (true) Linux execution environment is to use Windows Subsystem for Linux - a thing that renders Cygwin largely obsolete.