0

So this is the requirement asked by our professor, we need to use c89 standard (there is no long long type). And in linux before compile we have to use -ansi flag.

I prefer to debug in Windows first using VS Code, however one day I notice long overflow then I just realised the long of gcc in x64 windows is 4 byte, in x64 linux gcc long is 8 byte.

  1. I am very sure the installation that I've chosen is x86_64
  2. How I knew it's 4 byte by running printf("The size of long is: %d\n", (int)sizeof(long));

Do I need to add any command line flag to gcc to make the size of long become 8bit?
What can I do to make the GCC reserve long as 8 byte?
Btw I open the header file and I saw this What is this

C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\x86_64-w64-mingw32\include\_mingw.h
I tried to change to

#ifndef __LP64__    /* 32 bit target, 64 bit Mingw target */
#define __int64 long
#else           /* 64 bit Cygwin target */
#define __LONG32 int
#endif

But it didn't work somehow

Eric
  • 45
  • 1
  • 6
  • 2
    you don't want to make it 8-bit. The value you want is 8-byte – phuclv Feb 03 '21 at 16:37
  • 2
    Windows uses 32-bit long. Period. If you want a different size, you need to use something other than windows, such as cygwin or WSL. Trying to use a 64-bit long on plain windows will cause you all kinds of library incompatibility issues. – Chris Dodd Feb 03 '21 at 23:43
  • @ChrisDodd I tried Cygwin but Visual Studio code can't debug with it. It didn't display the output – Eric Feb 04 '21 at 00:28
  • @Eric: Hey, you could make a feature request to be able to debug Cygwin binaries. VSCode's got almost all of the required since it can remote debug from Windows to Linux already. https://code.visualstudio.com/docs/remote/remote-overview – Joshua Feb 04 '21 at 16:30

1 Answers1

0

To change long to 8 byte on windows, you need to use the Cygwin64 version of gcc rather than the mingw version. Microsoft set the model to LLP64 (that is, long long and void *) are 64 bit. You can't override this with mere compiler options as system linkage would fail. But Cygwin has its own linkage and provides 64 bit Unix compatibility where sizeof(long) = sizeof(void *).

Source: https://cygwin.com/cygwin-ug-net/programming.html

We can almost do a typedef or #define thing, but printf format specifiers won't work very well.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • thanks for the suggestion however I want to use it with Visual Studio Code. I can't use Cygwin. I can only debug windows executable – Eric Feb 03 '21 at 16:46
  • 1
    @Eric: Rebuilding the library stack in LP64 is too much work. – Joshua Feb 03 '21 at 16:47
  • so you are implying it is not possible right? I am thinking if we can change the define of header of long become long long – Eric Feb 03 '21 at 16:49
  • @Eric: That's easy. Now printf format specifier, that's hard. How will you do printf("... %ld ...", (mylong)3); – Joshua Feb 03 '21 at 16:50
  • no I mean change from Mingw32 header file so that it act long as long long (8byte) – Eric Feb 04 '21 at 00:26
  • 1
    @Eric VS Code and Cygwin have nothing to do with each other, one is a text editor and another is a runtime environment. And Windows executable use the Windows convention, you can't change that without breaking things unless you don't want to interact with the outside world and your program is tiny – phuclv Feb 04 '21 at 11:28
  • yes it is a tiny program that is why I am not concern. You should comment above not here. – Eric Feb 04 '21 at 22:04