0

i created and wrote 3 different .c files but while compiling only a single a.exe file is getting created and when i try to compile another .c file, the code of the 2nd .c file is getting overwritten on the previous a.exe file.

So i am a beginner and started to learn C on VS Code.

At the very first time VS Code created separate .exe files by renaming the previous a.exe file with same name as of its .c file.

but after deleting those files when i started afresh the above problem is occuring.

so how do i make the VS Code create a separate .exe for each .c file?

Thank you

  • 1
    use the `-o` option of the compiler – rioV8 Jul 25 '23 at 14:47
  • 1
    Compilers generally link all files that's in a single directory / project so maybe try putting each file into a different directory / project – Chillzy Jul 25 '23 at 16:05
  • 1
    In general, the source code for a single .exe will consist of *multiple* .c files: `myapp.exe = src1.c+ src2.c + ...`. It sounds like your code (at least for the moment) is much simpler: `myapp1.exe = src1; myapp2.exe = src2.c; ...`. SUGGESTION: Just create a new VSCode project - in a new, separate directory - for each different .exe you wish to develop. PS: There are lots of alternatives ... but start out with the "simplest" approach possible: give each app its own VSCode project. – paulsm4 Jul 25 '23 at 16:07
  • And like rioV8 and phatakuddhav both said: add "-o xyz" to your build command if you want the .exe named something other than (default) "a.exe" :) But I'd definitely discourage you from putting a bunch of ".c" source files for a bunch of *DIFFERENT* .exe's in the *SAME* project directory. – paulsm4 Jul 25 '23 at 19:04
  • what compiler are you using? – starball Jul 26 '23 at 03:24
  • @starball i am using mingw gcc compiler installed it through msys as directed on microsoft website after installing vscode.. i am new to programming.. – Dhiren Vairagade Jul 26 '23 at 09:04
  • @rioV8 can u please tell the steps.. i am new to programming.. thank u.. – Dhiren Vairagade Jul 26 '23 at 09:05
  • Does this answer your question? [Determining C executable name](https://stackoverflow.com/questions/653807/determining-c-executable-name) – starball Jul 26 '23 at 09:08
  • @Chillzy i also thought about doing that but i am curious as to how initially did vscode created separate .exe files for different .c files? and now it is overwriting the new code on previous a.exe file.. is there an issue with my ide/compiler? – Dhiren Vairagade Jul 26 '23 at 09:08
  • @paulsm4 yes my code is simple as i am new to programming and just started learning.. the thing is when u said a single .exe file consist of multiple .c files, when i hit run the code from new .c file is only shown on the terminal not the code from previous .c file.. also for the very first time vscode created different .exe files for each of the .c file but when i deleted them and created new .c files this problem is occuring.. is this normal or theres something wrong with my ide or compiler? thank u.. – Dhiren Vairagade Jul 26 '23 at 09:12
  • 1
    the official VSC C++ doc pages tell you all you need to know, **READ** them from start to finish. Or use Visual Studio. – rioV8 Jul 26 '23 at 09:35
  • The behavior you're describing is "normal" - there's nothing wrong with your compiler or your IDE. Most "projects" consist of MULTIPLE source files (*.c, *.h, etc. etc.) that go together to build a SINGLE binary output (e.g. a single executable application). If you expect each of your .c files to build a different .exe ... then you should put each in a separate project. If you want to give your .exe a different name than the default, then you need to "tell" your linker (e.g. with "-o"). SUGGESTION: work through a good tutorial or two. – paulsm4 Jul 26 '23 at 15:54
  • @paulsm4 there is no need to add each `.c` file in a separate project if you set up your workspace correct – rioV8 Jul 26 '23 at 22:20
  • "Best practice" is to organize the artifacts for different builds in different projects. It's not a "hard and fast rule". But it's definitely a useful convention. The *IMPORTANT* thing we both need to do is to ensure the OP understands what goes into "program builds", especially the concept of "separate compilation". This link might be helpful: https://hackingcpp.com/cpp/lang/separate_compilation.html – paulsm4 Jul 26 '23 at 22:47
  • @paulsm4 thank u so much for the link paul.. i will go through it and get back if i have any problem.. ✌ – Dhiren Vairagade Jul 27 '23 at 10:01

1 Answers1

-4

I am not that much used to VS Code, I use Linux, generally when compiling a .c file in linux, an executable file called a.out is created for each program for the output.

Now what I think might be happening over here is that when you compile the .c file in VS Code, the executable file might be created with .exe ending, so I don't think that it is much of an issue, the executable file is meant to be overwritten.

  • It's only called a.out when you don't specify the name of the exe – Chillzy Jul 25 '23 at 16:00
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 28 '23 at 13:59