ANSI terminal issue

I have been asked to port some Fortran 90 code from Linux to Windows 10. The code makes use of ANSI terminal commands to colour text. I have found that this does not seem to be supported by default in windows 10 terminals.

Would anyone be able to suggest a means to enable this using F90 / C code?

I think that I may need to switch ENABLE_VIRTUAL_TERMINAL_PROCESSING on. I found some code that appears to do this. I have pasted it below. I should point out that my understanding of C is rather elementary. I can get the pasted code to work using the gcc compiler but get several errors - pasted below code - when using pgcc or pgfortran. Can anyone advise what I can do to get this to work using pg compilers please?

Apologies if this is rather simple…



#include <stdio.h>
#include <windows.h>
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
int main(int argc, char** argv) {
  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  DWORD dwMode = 0;
  GetConsoleMode(hOut, &dwMode);
  dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
  SetConsoleMode(hOut, dwMode);
  printf("\x1b[101;93m Testing\x1b[0m\n");
  return 0;
}

pgcc c_code.c

PGC-W-0114-More than one type specified (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\WinNT.h: 435)
PGC-W-0143-Useless typedef declaration (no declarators present) (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\WinNT.h: 435)
PGC-W-0114-More than one type specified (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\commdlg.h: 734)
PGC-W-0143-Useless typedef declaration (no declarators present) (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\commdlg.h: 734)
PGC-W-0043-Redefinition of symbol, LPUINT (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\imm.h: 45)
PGC/x86-64 Windows 18.10-1: compilation completed with warnings

pgfortran c_code.c

PGC-W-0114-More than one type specified (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\WinNT.h: 435)
PGC-W-0143-Useless typedef declaration (no declarators present) (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\WinNT.h: 435)
PGC-W-0114-More than one type specified (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\commdlg.h: 734)
PGC-W-0143-Useless typedef declaration (no declarators present) (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\commdlg.h: 734)
PGC-W-0043-Redefinition of symbol, LPUINT (D:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/um\imm.h: 45)
PGC/x86-64 Windows 18.10-1: compilation completed with warnings
pgfortran5dzG2lkdMQDjJ.obj : error LNK2005: main already defined in f90main.obj
f90main.obj : error LNK2019: unresolved external symbol MAIN_ referenced in function main
c_code.exe : fatal error LNK1120: 1 unresolved externals

Hi TTilford,

Did you run the program under a Cygwin or Dos Shell?

I ran your code under a Dos shell and see “Testing” highlighted red with yellow text.

No idea why it doesn’t show up under Cygwin.

Note, when compiling a program with C main using pgfortran, add the flag “-Mnomain” so the compiler doesn’t link using the Fortran main.

-Mat

Hi Mat,

Thanks for your help and insight with this.

I am using a standard Windows 10 cmd prompt. if I use gcc it compiles and executes with no issue. The PGI compilers give the error. The Mnomain flag hasn’t fixed this.

From the error messages - More than one type specified / Useless typedef declaration - I would though there is a clash in the libraries somewhere?

Tim.

Hi Tim,

These are just warnings so shouldn’t cause the compile to fail. Is there a different issue not shown in your initial post that’s preventing compilation?

Again, I was able to successfully compile and run your code. The only issue I see was that the text was only highlighted when using a DOS cmd shell.

-Mat

Hi Mat,

I can now get it to compile. I had only been forming the object and was concerned about the errors/warnings. I have silenced the warnings with the -w flag.

Thanks for your help with this.

Tim.