-1

It appears that the issue is a result of Powershell but do correct me if I am wrong. Standard CMD produces the exe file.

I am new to C - moving from MATLAB and Python - and am struggling with compiling the code in windows powershell.

I have written a basic code, based on an example:

#include <stdio.h>

int main(void)
{
    printf("Hello, World!\n");
}

When i run this in windows powershell using

'gcc .\helloworld.c -o .\helloworld' 

but this prints nothing and I am unsure why. I have also tried the IDEs like codelite and VisualStudio with no success.

Update

Based on the comments I have no executable file produced which appears to be the problem.

If anyone can help me that would be great!

K-Q
  • 133
  • 8
  • 3
    "this prints nothing" What did you expect it to print? I'd expect an output file being generated and the compiler being like "Yeth, marthter." – Yunnosch Sep 28 '20 at 11:48
  • 1
    Or "mithreth" of course.... or "your magnifithenthe".... – Yunnosch Sep 28 '20 at 11:49
  • 1
    It prints nothing because `gcc` prints nothing is nothing bad happened – klutt Sep 28 '20 at 11:50
  • Ok, I assumed that it would print the statement in the compiler? It also doesn't produce an output file in the file source location. – K-Q Sep 28 '20 at 11:50
  • There are two steps: (1) compile and link your program (2) Run your program. You only did step 1. This should have produced an executable named `helloworld`, which you now need to run. Windows may have some requirement that it end in `.exe` (not sure about that). – Tom Karzes Sep 28 '20 at 11:50
  • 3
    Perhaps grab a good C book or tutorial? Programming by guessing is not usually very productive. – kaylum Sep 28 '20 at 11:51
  • @TomKarzes So i should have a file to run but this is not in my system after i run the above suggested code. – K-Q Sep 28 '20 at 11:53
  • @kaylum I would take recommendations – K-Q Sep 28 '20 at 11:53
  • 2
    Your comment contains the information that no file whatsoever is generated. That is an important info, please [edit] it into the question itself. – Yunnosch Sep 28 '20 at 11:56
  • @TomKarzes this didn't succeed either... – K-Q Sep 28 '20 at 12:00
  • 3
    [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – kaylum Sep 28 '20 at 12:01
  • @4386427 by simpliy using 'ls' in the command line Or I can visually check in the file browser but neither have it – K-Q Sep 28 '20 at 12:03
  • 1
    hmmm. Is `ls` a powershell command... anyway - have you tried a "normal" cmd window? – Support Ukraine Sep 28 '20 at 12:07
  • @4386427 yes, powershell uses linux based commands (something I am more familiar with) - hence why I hadn't used the normal CMD mode. It appears that Powershell doesn't like running C for producing the exe file but cmd does! Thank you all for your help, hopefully there are no more issues on this – K-Q Sep 28 '20 at 12:15
  • @TomKarzes the extension is added by the compiler, no need to add it. – anastaciu Sep 28 '20 at 12:34

1 Answers1

1

Gcc is a compiler which makes an output file. You are used to python and Matlab. Python is an interpreted language so you can get outputs without making an executable file.

For C you make an output file which is an executable which you run to get the desired results. Assuming your file name is helloworld.c Run the following :

gcc helloworld.c -o helloworld

This will create a file by name of helloworld in that directory. To execute the file you need to do ./<output_filename> which in your case would be ./helloworld and then you should get your output/printing

Mohit Sharma
  • 338
  • 2
  • 13
  • I think your answer overlapped with OP clarifying that no file is generated.... Please double check, I currently get the impression that this is not a solution to the described problem (if comments are included). – Yunnosch Sep 28 '20 at 11:54
  • Exactly, I didn't initially realise I would get an executible file to run but now it appears that is the issue. – K-Q Sep 28 '20 at 11:56
  • @Yunnosch I didn't realise no output file is being generated, I will modify the answer accordingly... – Mohit Sharma Sep 28 '20 at 12:00
  • That is why I mentioned "overlap". You cannot really be blamed. If you however update your answer you will be safe from misunderstandings.... – Yunnosch Sep 28 '20 at 12:03
  • @Yunnosch Can you please be kind enough to edit the answer, I can't figure out a way to write that and mention the 'overlap' in words. – Mohit Sharma Sep 28 '20 at 12:21
  • 1
    No I can't, not knowing the solution either. More debugging info is needed, but hard to tell which.... If you cannot answer the question as asked now, I recommend to delete the answer. (I cannot blame OP for a moving target question either....) – Yunnosch Sep 28 '20 at 12:23