I'm using Notepad++ to write C programs and compiling them through a command prompt and MinGW. How can I make it so that I don't have to manually recompile the executable file through a command prompt every time I update the C file? There are no restrictions on the solution.
-
Why do you think that you have to recompile each time? What is the reason for doing that? What happens if you don't? – Yunnosch Jun 17 '18 at 07:23
-
Are you aware of the tool `make`? Or one of its siblings? – Yunnosch Jun 17 '18 at 07:25
-
If I don't recompile then it doesn't update the executable with the changes I made to the C file. – Toby Jun 17 '18 at 07:25
-
That is of course true, but it implies that you want the executable to be updated for each change. Asking about not compiling if an updated executable after each edit is what you want to achieve is therefor puzzling. – Yunnosch Jun 17 '18 at 07:26
-
To put it differently, if you do NOT want the executable to be updated after each edit, but do instead need the executable to be surely up to date whenever you execute it, that makes a different and (to me) more understandable question. I.e. "Ensure freshness of executable before executing?" instead of "Automatically recompile C program after changing?". – Yunnosch Jun 17 '18 at 07:27
-
I mean that I don't want to have to manually recompile it through a command prompt - I want it to automatically do that. – Toby Jun 17 '18 at 07:29
-
Are you aware of this concept: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Yunnosch Jun 17 '18 at 07:30
-
**When** dou you want that? – Yunnosch Jun 17 '18 at 07:30
-
1No, I'm not aware of that. When do I want what? – Toby Jun 17 '18 at 07:32
-
I think you want the program to be up to date when you execute it. Your attempt to achieve that is to automatically build after each edit. You ask about how to do your solution approach, which prevents you from considering other approaches. That is a perfect example for a XY-problem. – Yunnosch Jun 17 '18 at 07:33
-
When do you want "I don't want to have to manually recompile it through a command prompt - I want it to automatically do that." After edit or before executing? Those times are not identical, e.g. when you edit twice before executing once or if you edit, go to bed and try to execute next morning. – Yunnosch Jun 17 '18 at 07:35
-
They both produce the same results so it doesn't really matter. – Toby Jun 17 '18 at 07:37
-
3Then you want a build mechanism, which, when you attempt to execute, first builds the program, but only if necessary (i.e. if the source has been changed). The tool to do that is `make`. – Yunnosch Jun 17 '18 at 07:38
-
Thanks, but how do I use it? – Toby Jun 17 '18 at 07:39
-
Please edit your question, twice info from your comments has been added to it for you. Rephrase your goal (e.g. "after each edit or before execution"). Describe more about the context (e.g. how it is going to be executed: can a call from commandline be assumed? does a windows double-click have to be covered? Are there commandline parameters? Can `make` be used? Can shell scripts/batches be used? ...). – Yunnosch Jun 17 '18 at 07:44
-
Please make a [mcve] by providing code for (a very simple version of) your program. It should demonstrate the method of calling and API (parameters, return values...). I.e. turn your XY-problem question into one about your actual goal. – Yunnosch Jun 17 '18 at 07:46
-
I don't feel that I need to provide code for this question - it's not an identifiable error. – Toby Jun 17 '18 at 08:00
-
Then find a different way of defining precisely enough how exactly you need to be able to execute your auto-update-executable. Showing a little program which has the same behaviour and use-cases was just the most convenient way I could think of. Keep in mind that it is you who wants the question clear enough to be helpfully answered. – Yunnosch Jun 17 '18 at 08:02
-
I don't see what's unclear about the question. I want a solution to not having to manually recompile my code each time I updated it. – Toby Jun 17 '18 at 08:10
-
My crystal ball predicts: I provide a `make` solution, you say "No I want to double click in windows." Then I provide a batch which can be double-clicked to trigger build+execute, you say "No, it needs to work with commandline parameters, too." I extend to support them and you say "No, for some reason it cannot be script, it has to be a .exe." Or if I do the full-fledged super-solution you answer "Why not just use a simple make file?". Does this make it clear to you what is unclear about your question? – Yunnosch Jun 17 '18 at 08:17
1 Answers
Scenario:
Ok, guess about a program that is made to automatically recompile the C program each time the source is edited. You make some changes and save the file to prevent data loss, but still you have some modifications remaining for the source file. But as you have done some modifications and saved the file, then your so-called automatic recompilation bot would think it as a valid edit. Thus it will trigger compilation of the source code and this may sometimes lead to compilation error, if you haven't completed the source code writing fully. And after writing two/three lines, you again hit save on notepad++. And again compilation occurs. And it is continuing....
Isn't the above situation worse for a computer system???
Solution:
As I've mentioned the problem in the above scenario, there must be a specific situation when the user will want to compile the source by his own, not automatically after each edit.
Solution-1:
To do that you should have wanted to hit at least one button for compilation. Any GUI IDE does the same thing. So, use some IDE like- Code::Blocks or similar tools out there. The interface is better suited than notepad++ in terms of coding c
/c++
. And after you finish modifications, simply hit the compile button there and no more hassle for you to write up the commands in the command prompt, I guess.
Solution-2:
If you are aware about the IDE and don't want to use one such, then it will be better to write a batch script (Windows Only) like this answer: Call and compile C code from a batch file. Still you have to run that batch file every time you modify your code, it will not happen automatically.
N.B.: If you are a beginner, try to inculcate yourself with the command line method. It is not a bad practice at all. Many good programmers are using the command line method for years.

- 5,509
- 6
- 32
- 47