Linux GCC Compiler Output in Mint: What to do with the file? - c

I wrote a simple c program. saved it with "c" suffix. Went to the terminal (in Linux Mint), typed in
gcc -o outputfile inputfile.c
In the directory containing inputfile.c a file called outputfile appeared, as I expected. I want to run the program, but clicking on the file does nothing.
What am I doing wrong?

The program is running, but you're not seeing its output because the terminal isn't open. Open up your terminal and run it with ./outputfile
If your program opened a window, you would see it by clicking on the file.

Related

Cannot run C program from command prompt on windows 10

I am using Windows 10 OS. I installed MinGW for compiling C programs. I tried running my program using the gcc command on the Command Prompt. The file compiles and an executable file(.exe) is formed in the same folder as my source file. But when I try running this file, I keep getting the message 'Access is denied'. Also the .exe file vanishes after this. I do not know what is wrong. Please help me out.
P.S Another time I did the same thing mentioned above and the .exe file ran and I was able to see the output on the Command line. And this time the .exe file did not vanish either.
What was the command you put to compile the program? Also to compile and make an executable file, you have to put this command - "gcc -o nameofexecutablefile nameofsourcefile.c", to run this program just type the name of your executable file in cmd. And to stop the program's window from closing, put "system("pause");" right before the "return 0;" in your program, at the end of the program it will display "Press any key to continue..." and when you press any key, the window will close. Also check if you did any mistakes in your program. I'll also give an example -
------------CODE-----------
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
system("pause"); // this will pause the window
return 0;
}
-------OUTPUT--------
Hello World
Press any key to continue...

How to execute a C file through cmd

In my current example, I have a C file called 'Main.c' which converts and prints US pounds into UK stones/UK pounds/kilos, and the variable US pounds is currently defined by the user's input (scanf). The file itself is executed through Visual Express 2013; however, I want to change this so that I can navigate to the C file and execute the file directly through the commmand prompt (cmd) whiles passing a value to define US pounds.
I understand that this is required in main:
void main(int argc, char *argv[])
And I know how to navigate to the C file through the command prompt:
>cd Directory/To/The/File
However this is where I get stuck; I don't know how to execute the C file. I have researched into this and found several examples, such as using 'gcc' and 'cc', but the system doesn't recognise these commands. None of the materials that I have found fully explains what exactly I have to do in order to accomplish what I am trying to do; do I have to install something, or am I using the wrong commands, is it possible what I am trying to do, what excactly do I have to do?
Broadly speaking, you need to do the following:
Compile the program using a compiler to generate an executable.
Run the executable file.
When you "run the program in Visual Studio", it is doing all of those steps for you.
I don't know much about command line compiling in Windows, but I believe msbuild is the name of the compiler which Visual Studio uses. Look into that, and you'll see how to compile. Compilation will then generate the executable, which you just type into the command line to run.
EDIT: I found an article which suggests that cl is a C compiler command available on the command line.
Here is the relevant excerpt:
At the command prompt, specify the cl command together with the name of your source file—for example, cl simple.c—and press Enter to compile the program. The cl.exe compiler generates an .obj file that contains the compiled code, and then runs the linker to build an executable program that has the name of your source file, but has an .exe file name extension—for example, Simple.exe.
You cannot cd to the file, only to the directory. The cd command means "change directory".
You cannot execute a C file, it's just a text file and your computer doesn't know how to run it. You need to compile it first (this is what Visual Studio does). There should be an EXE file somewhere close, you need to find it. It is the binary, executable, form of your C program which you can run directly from the command line.

error when i am calling c binary from tcl script

i have created (executable) binary "sample" from .c using
gcc sample.c -o sample
it's created binary named sample sucessfully.
when i run this from terminal like ./sample it display a result..
but when i run this from my tcl like exec ./sample it shows error like ./sample no such file or directory..
can anyone help me to solve the above error?
If sample in the current directory is executable, exec ./sample should work. Assuming that the binary itself does not generate that error message when it runs, of course. (Messages on stderr will become errors in Tcl by default.)
Check that puts [pwd] tells you the place you expected; if you had a cd elsewhere in your script then that relative path would no longer work. If this is the problem, use the full, absolute path to the compiled file. (You can compute this at the start of your script using file normalize sample, but you have to do this before you cd; good library packages do not use cd precisely because it is so confusing while simultaneously being usually totally unnecessary for them…)
If file exists ./sample is true (and file executable ./sample is also true, which it should be if it has just been produced by a compiler) then check immediately after the failed execution what the contents of the errorInfo and errorCode global variables are. They may give a bit more indication of what went wrong. (Or maybe not; can't tell for sure.)

Compiling and Running C code in notepad++

I have a problem with compiling and running C codes in notepad++.
I am using the nppexec plugin and wrote the following in the script section after pressing F6:
C:\MinGW\bin\gcc.exe -g "$(FULL_CURRENT_PATH)" -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
$(CURRENT_DIRECTORY)\$(NAME_PART).exe
After pressing OK, I get the following on the console:
C:\MinGW\bin\gcc.exe -g "D:\Silent\Documents\College Stuff\6th sem\NETWORKING lab\substitutioncypher.C" -o "D:\Silent\Documents\College Stuff\6th sem\NETWORKING lab\substitutioncypher.exe"
Process started >>>
<<< Process finished. (Exit code 0)
D:\Silent\Documents\College Stuff\6th sem\NETWORKING lab\substitutioncypher.exe
Process started >>>
Here, substitution.c is my program to be run. The problem is that the gcc part is working fine but I am not able to execute the program from here as there is no response.
As you see, it just says process started and after that nothing. No response to a key being pressed, it just accepts everything like a text editor.
If I go to the working directory and execute the program from there (double clicking the exe file) then it seems to run perfectly fine. The problem seems to be in my script or the plugin.
Please, can anyone find out what is wrong with my compiling and running script?
In addition to #paxdiablo 's answer, you may also find useful the following NppExec script for single file projects:
npp_save
cd "$(CURRENT_DIRECTORY)"
cmd /c del "$(NAME_PART)".o "$(NAME_PART)".exe *.o
C:\MinGW\bin\gcc.exe -g3 -std=c89 -pedantic -Wall -Wextra -Wno-nonnull "$(NAME_PART)".c -o "$(NAME_PART)".exe
npp_run "$(NAME_PART)".exe
The 1st line saves the document that is currently active inside notepad++.
The 2nd line ensures your current directory is the one of the active document. This let you refraining from using the "$(CURRENT_DIRECTORY)" variable in the rest of the lines.
The 3rd line removes any executables and object-file leftovers from previous successful compilations. Removing the last executable is a nice idea, because if you don't then the last line will cause your .exe produced by the last compilation to be run anyway, even if your current compilation fails. A failed compilation does not produce an .exe, so normally you don't want NppExec to run the previous .exe. Removing the previously produced object-file is optional, but it does ensure that it will not affect fresh compilations (it makes more sense in multi-file projects, as an alternative to the touch command-line tool).
The 4th line compiles the active document. Feel free to modify gcc's options according to your needs. If you add C:\MinGW\bin into the Windows PATH environment variable, and assuming you are using only one gcc installation on your system, then you can skip the absolute path, and write just gcc instead.
The last line executes the produced executable. The npp_run command tells NppExec to launch a new command-prompt window, and run your program in it (unless it is a WIN32 GUI program). I personally find it more convenient compared to the NppExec console embed in notepad++. It looks more natural and it also avoids some I/O redirection problems of the NppExec console.
However, if your program is a console app that does not interact with the user say via a loop, then this approach will cause the launched command-prompt window to close immediately after your program terminates, not giving you the chance to inspect its output. In that case you should have you program waiting for a key to be pressed by the user just before its termination. A quick-and-dirty way is to put a system("pause"); right before your main() function's return and/or exit() statements (it is much better though to write a simple cross-platform function or macro for this).
You may experiment with the above script by typing it in F6's <temporary script> and save it permanently for general use when you are happy with its behavior.
On a side note, you may also find it useful to have a look at this post, where I'm trying to explain how to setup NppExec so it jumps to the appropriate line in the source code, by double-clicking on any error gcc spits in the NppExec console during compilation.

"Too few arguments" error trying to run my compiled program

I'm trying to code to refresh my memory preparing myself for a course.
int main(){
int x;
for( x = 0;x < 10; x++){
printf("Hello world\n");
}
return 0;
}
But when I tried to run this I get Too few arguments
I compiled the code above using gcc -o repeat file.c Then to run this I just type repeat
Sorry if this was a stupid question, it has been a while since I took the introduction class.
When you type
filename
at a prompt, your OS searches the path. By default, Linux doesn't include the current directory in the path, so you end up running something like /bin/filename, which complains because it wants arguments. To find out what file you actually ran, try
which filename
To run the filename file gcc created in the working directory, use
./filename
Your code compiles fine. Try:
gcc -o helloworld file.c
./helloworld
UPDATE :
Based on more recent comments, the problem is that the executable is named repeat, and you're using csh or tcsh, so repeat is a built-in command.
Type ./repeat rather than repeat.
And when asking questions, don't omit details like that; copy-and-paste your source code, any commands you typed, and any messages you received.
The executable is named file, which is also a command.
To run your own program, type
./file
EDIT :
The above was an educated guess, based on the assumption that:
The actual compilation command was gcc file.c -o file or gcc -o file file.c; and
The predefined file command (man file for information) would produce that error message if you invoke it without arguments.
The question originally said that the compilation command was gcc file.c; now it says gcc -o filename file.c. (And the file command prints a different error message if you run it without arguments).
The correct way to do this is:
gcc file.c -o filename && ./filename
(I'd usually call the executable file to match the name of the source file, but you can do it either way.)
The gcc command, if it succeeds, gives you an executable file in your current directory named filename. The && says to execute the second command only if the first one succeeds (no point in trying to run your program if it didn't compile). ./filename explicitly says to run the filename executable that's in the current directory (.); otherwise it will search your $PATH for it.
If you get an error message Too few arguments, it's not coming from your program; you won't see that message unless something prints it explicitly. The explanation must be that you're running some other program. Perhaps there's already a command on your system called filename.
So try doing this:
gcc file.c -o filename && ./filename
and see what happens; it should run your program. If that works, try typing just
filename
and see what that does. If that doesn't run your program, then type
type -a filename
or
which filename
to see what you're actually executing.
And just to avoid situations like this, cultivate the habit of using ./whatever to execute a program in the current directory.

Resources