gregCubed
gregCubed

Reputation: 77

printf not printing to console

Using VS 2019 to create a C program to run in Windows Subsystem Linux (Ubuntu). Compiling using gcc starfire.c as the original purpose of the program was for something else, but has devolved into this mismash.

void main() {
    printf("print this");
    return;
}

I've tried using fflush(stdout) and setbuf(stdout, NULL) and neither are working. Please help?

EDIT: I've also tried using \n, so it's not that, either. I'm also using #include <stdio.h> and it still just refuses to work properly.

Upvotes: 0

Views: 2005

Answers (1)

Craig Estey
Craig Estey

Reputation: 33631

The x86 64 bit calling convention is different for windows and linux.

See: https://en.wikipedia.org/wiki/X86_calling_conventions In particular, the "Microsoft x64 calling convention" and "System V AMD64 ABI" sections.

In Windows, the first four arguments are passed in registers. But, under linux, the first six arguments are passed in registers.

And, which register holds which argument is [again] different:

            arg0    arg1    arg2    arg3    arg4    arg5
Windows:    RCX     RDX     R8      R9
Linux:      RDI     RSI     RDX     RCX     R8      R9

Also, which registers must be saved by callee, which may be trashed, etc. are different.

Using gcc under Windows (cygwin or mingw) will use the MS convention. Using gcc under WSL will use the ubuntu/linux SysV ABI (i.e. linux) convention.

So, you can't just do a cross-compile without some option to tell the compiler to use the correct calling convention. Also, there are probably issues with dynamic libraries (under MS they are .dll, but under linux, they are .so).

Also, windows uses the PE (portable executable) format [derived from SysV COFF format]. But, linux uses ELF object/binary executable format.

Side note: IIRC, MS just decided to support VS under linux [but that's very recently], so you might get some relief there.

But, IMO, I'd just use gcc et. al. under WSL/ubuntu/linux. And, if you really need an IDE, try Eclipse.

You could try to get VS to cross-build, but, IMO, the sign on the wall is: Abandon Hope All Ye Who Enter Here


UPDATE:

Caveat: I posted the above based on the OP's original post [without the benefit of some of the top comments (re. how gcc was being used)]. But, the more I think about it, the less sense it all makes.

But, this begs the question: Are you using WSL or WSL2? They are implemented differently.

With WSL, MS just added an additional linux compatible syscall interface support to the win10 kernel. It could execute a limited number of programs. AFAIK, the programs still used the native NTFS file system. You aren't running a linux kernel, just executing some linux ABI compatible executables.

With WSL2, MS implemented a lightweight virtual machine [similar to vmware or virtualbox] that allows the full ubuntu/linux kernel to run. With this, you can install ubuntu by booting the VM from the "live" ubuntu installation .iso (straight from the ubuntu website). It then installs [pretty much] just like a native ubuntu install.

Because WSL2 is a VM, you can install fedora, suse, mint, etc. in a similar manner.

Upvotes: 1

Related Questions