Why does Windows Defender detect compiled C executables as viruses? - c

I'm using Visual Studio 2019 (Community Edition). I've never had an issue when compiling C++ code. I simply get the final executable and I'm able to run it without a problem. Recently I've decided to learn C and this is the C program that I'm compiling:
#include <stdio.h>
int main()
{
printf("Hello, world! \n");
return 0;
}
The file name is "main.c".
I haven't changed any of the default project settings and the project that I selected was "empty C/C++ project".
The code compiles fine but when I attempt to run the executable I get this error:
Unable to start program [path to .exe file]
Operation did not complete successfully because the file contains a
virus or potentially unwanted software.
I can resolve this issue by going to Windows Defender and manually allowing this detected threat but obviously the issue with this approach is that other people won't be able to run my C programs without them being detected as a virus.
Ultimately, I believe this problem is related to how VS2019 is compiling my program rather than Windows Defender because I'm able to compile C++ (and other languages) into executables that run perfectly fine.

Related

"Availability status: error" for working C programs

I recently learned about Visual Studio Code & Intellisense and am currently writing code on that and trying to compile it using Cygwin. However, I'm never able to do it.
I have added C:\cygwin64\binand%systemdrive%:\cygwin\bin` to my system environment variables in settings. I'm doing this on a Windows 11 computer. But every time I try to compile a program, like:
gcc HelloWorld.c
I get:
cc1: fatal error: HelloWorld.c: No such file or directory
compilation terminated.
In file explorer, all the programs I'm trying to compile say "Availability status: error". Specifically, any programs (regardless of language) I have downloaded onto my computer hard drive are apparently unavailable. What does this mean?
I tried using MSYS Mingw-w64 but the same problem happens. I thought that I might not have included the "gcc-core" compiler file when I downloaded it, so i did that again with the file but nothing. I typed "gcc --version" and "gdb -- version" to check and debug but the same messages pop up, both for Cygwin and Mingw.
I tried using other text editors (notepad and vim) to type, but they didn't have any effect. I thought they might create compileable, "available" files on file explorer.

VSCode Microsoft C/C++ Extension Using Old MinGW System Path

I'm trying to run a simple Hello World C program in VSCode, on Windows 10, to test whether things are going fine or not (I previously had to reinstall the OS). So this is the code:
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
Should be simple, and it should run perfectly well, I thought. I tried it with these options, which I assume was from the Code Runner (by Jun Han) extension. When I try selecting the "Run Code" option, everything works well, and the code runs perfectly. When I tried the "Debug C/C++ File" or the "Run C/C++ File" option, however, this was what I got.
* Executing task: C/C++: gcc.exe build active file
Starting build...
C:\Compilers\MinGW\bin\gcc.exe -fdiagnostics-color=always -g D:\Alfred\Studies\College\Skripsi\resources\preparations\CTest\HelloWorld.c -o D:\Alfred\Studies\College\Skripsi\resources\preparations\CTest\HelloWorld.exe
The system cannot find the path specified.
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
* Terminal will be reused by tasks, press any key to close it.
Worth noting here that C:\Compilers\MinGW\bin\gcc.exe is my MinGW directory in the system path before I reinstalled the OS. I'm not sure why it pulls that specific path, considering:
I've tested the MinGW installation on the reinstalled OS, and running the gcc --version command works on cmd,
The system environment variable has been set to the new path (C:/MinGW/bin),
If it's really a problem with MinGW paths, logically the "Run Code" option should also fail. Yet it doesn't.
Finally, I've also tried restarting the laptop, reinstalling MinGW, reinstalling VSCode (along with deleting the .vscode folder in %USERPROFILE% and Code folder in %APPDATA%), but this problem still persists.
I've been looking for the possibility as to why for hours now, yet I haven't managed to. Was hoping some of you could tell me what I overlooked here.
P.S.:
The only extensions I have installed are C/C++ by Microsoft and Code Runner by Jun Han.
Please bear with me if my question was missing some details - I have very little experience in asking questions here.
Edit 1: I've been told this was due to Microsoft's C/C++ extension, and not Jun Han's Code Runner. I have edited the title to reflect so.
The two options you mentioned are from the Microsoft's C/C++ extension: they use the C_Cpp.default.compilerPath setting to locate your compiler. You can change it either the settings GUI or in the JSON file.

Windows Defender/Security deletes my "hello world" program

I'm trying to start a new C project using BearLibTerminal, compiling with gcc.
I'm able to compile the hello world, but when I try to launch, Windows Security gives an error, which reads
"Operation did not complete successfully because the file contains a
virus or potentially unwanted software."
Shortly after compilation, whether I attempt to run or not Windows deletes the executable.
I can of course disable Windows Security real-time protection, but this seems like a bad workaround. Does anyone know what could be changed so that Windows doesn't think my program is a virus?
Here is the code:
#include "BearLibTerminal.h"
int main()
{
terminal_open();
// Printing text
terminal_print(1, 1, "Hello, world!");
terminal_refresh();
// Wait until user close the window
while (terminal_read() != TK_CLOSE);
terminal_close();
}
and this is the makefile:
gcc -IC:/foo/include -LC:/foo/bin main.c -lBearLibTerminal -o test.exe
Thanks kindly for the assistance.
EDIT: I seem to have fixed the problem. For some reason, the version of MinGW found here, which is where I was directed to from mingw.org, causes things I compile to be detected as a virus. I removed MinGW completely, and downloaded the version found on SourceForge, and I no longer have the issue. I have no idea if the compiler was somehow compromised, or if Windows Security was just looking out for things made by a specific version of MinGW, but I would note that the version of "Hello World" from the first compiler was 4kb larger than the version from the SourceForge compiler, and likewise the installer was around 6kb larger too.
As a temporary fix you can exclude your compilation folder from Windows Defender.

Running a C program in Visual Studio Code (or VS2019)

I've just downloaded VS Code, and am struggling to work out the basics of running any programs. I'm attempting to run a basic Hello World program in C:
#include<stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
I found out that I needed to download a C compiler to run C programs on VS-Code, so I downloaded Visual Studio with C++ from the Microsoft website, which basically installed Visual Studio 2019. The heading on the link said "Develop C and C++ applications", so I figured it would at least let me program in C.
After doing this, in VS-Code I downloaded the C/C++ extension from Microsoft, and the Code Runner extension by Jun Han, which was recommended by a YouTube video. I then selected the Run Code (...environment? application?), and pressed the Play button on the top-right. The output in the terminal was:
[Running] cd "c:\Users\Leo\Documents\VSC Test Project\" && gcc test.c -o test && "c:\Users\Leo\Documents\VSC Test Project\"test
'gcc' is not recognized as an internal or external command,
operable program or batch file.
[Done] exited with code=1 in 0.078 seconds
I don't understand the error - so I opened Visual Studio 2019 to see if I could develop C applications in that. But no, only C++. Did Microsoft lie to me, or is there something else I need to do to configure C in VS2019?
Basically, I have no idea what I'm doing in VS-Code, and would really appreciate someone taking the time to break down how to actually run a program in there, and get C working. Or if there's a simple solution that allows me to write C in VS-2019, that would be equally great.

How to use VC++ compiler in command line(Windows)?

I tried to use VC++ commandline, instead of MinGW compiler for windows system programming. I wrote a simple hello world program and tried to compile it, then i got this error message.
test2.c(1): fatal error C1083:'stdio.h': No such file or directory
I also added "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\Hostx86\x86" directory (where c1.dll lives) to the System Envionment Variable(PATH).
How can I fix this issue? Other tutorials don't give much information about VC++. (A lot of MinGW compiler tutorial out there btw)
You should use visual studio command line if you want to compile or run program with the help of vc++ compiler.
Else all information related to setting environment variables ETC. resides in this MSDN document.

Resources