"bash: ./main: cannot execute binary file: Exec format error" using wsl2:Ubuntu on windows 11 Visual Studio Code

Viewed 58
#include <stdio.h>

int main()
{

printf("Hello World");

return 0;

}

I build by:

gcc -Wall -ansi -pedantic -c main.c -o main

uname -m gives "x86_64".

After compiling file main gives:

main: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

strace ./main gives:

execve("./main", ["./main"], 0x7ffc779cf7c0 /* 33 vars */) = -1 EACCES (Permission denied)
strace: exec: Permission denied
+++ exited with 1 +++"
1 Answers

You did only compile, but not link the program. Unlinked object files cannot be executed.

Change your build command to:

gcc -Wall -ansi -pedantic main.c -o main

Note the missing -c.

Related