How to execute a program in linux after compiling it? - c

I am compiling a C code in linux with the following command:
gcc -o myprogram myprogram.c
If I hadn't given a name to it, I could have simply written the command ./a.out to execute it. But now, to execute the program I just write "myprogram" to the command line, but it says "command not found". What can I do to execute it?

It's possible that the current directory (".") isn't on your PATH. (You can check this by typing echo $PATH, this is a list of directories delimited with" :". "." should be in the list if you want to run something in the current directory.)
If the current directory isn't on your PATH, you'll need to type ./myprogram (or whatever the correct path is).

./myprogram
should do the trick.
(But really... have you looked at the contents of the directory after compiling the program "without name"? Or do you think ./a.out is a magic sequence Bash recognizes?)

Related

Executing windows commands in C

I have a batch file which changes the direction to a specific toolchain and executes one command like this:
cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin
avr-objcopy -O binary C:\Users\cinar\Desktop\hextobin\GccApplication.elf C:\Users\cinar\Desktop\hextobin\GccApplication.bin
I want to do this with my C application. I found this topic, tried the system(); command and it works partially. I can call this:
system("cd");
and get the direction back. But I can not change it with this command:
system("cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin");
This caused a compile warning about unknown escapes, so i added \ to escapes and tried this:
system("cd C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
I was able to compile and run this but that didn't change the direction.
Is there any possibility to execute my commands with system()? As I just want to change the direction and execute one command, I wanted to keep it simple.
Update: I found this topic afterwards:
system("cd <path>") in a C program
Then solved my query with this:
chdir("C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
system("avr-objcopy -O binary C:\\Users\\cinar\\Desktop\\ff.elf C:\\Users\\cinar\\Desktop\\ff.bin");
Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.
Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().
For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.
Also
system("dir Users\\whatEverNextFolder > test.txt");

Exact same command line produces error in Make yet succeeds if run from shell

I have a problem that I just can't wrap my head around. I have a minimal example makefile that is supposed to compile a very simple .c file into an executable program.
When I run make, the compiler starts compiling and then produces an error message
"T:\printOffsets.c:10:21: error: bootIfc.h: No such file or
directory"
Then I copy the exact same command line make is using to build the target and run it directly in the same Windows command shell instance, and suddenly compilation succeeds without errors!! The command line is (path names simplified):
T:\perl\c\bin\gcc.exe T:\printOffsets.c -IT:\include\ -o
D:\printOffsets.exe
How do I know? Well, make prints the command line before it executes it, so I simply copy&paste from the shell.
I don't get it! How is this possible?? How can the exact same command work on the shell and fail if launched from within a Makefile??
I'm using GNU Make 3.82 on Windows 7, by the way.
When command in makefile is giving different result from shell, just make sure it is using the shell you want.
Add a phony target in your make file:
.PHONY:testshell
testshell:
echo $(SHELL)
And run:
gmake testshell
If the result is not your favorite shell you can force it by adding a line such as this one at the beginning of your makefile:
SHELL=C:\Windows\System32\cmd.exe
If you are not sure of full path of your shell, just open a DOS console and launch:
where cmd
Edit: alternative solution
When using sh shell instead of cmd shell, you can also replaces all backslashes in commands with slashes and keep using sh.
Edit 2: change shell for a single target

Can't find how to select path to run a C program

I am using mac OS x and have written C program using GCC compiler.
But while running the program on terminal I am being shown that "No such file or directory found"
Please help me how to select the path?
run it with $./yourProgramFile command the ./ in the beginning is important. It means the program resides in the current directory.
Example:
/path/to/your/cFile $ gcc myfile.c -o myfile
/path/to/your/cFile $ ./myfile
You can do one of two things
1) Add the program's folder to the system's PATH; that way, you can call the program from any location. If it's a program you plan to use constantly, this is the best option.
Here's a way to do that:
Open up the .profile file in your home directory using any text editor.
Paste the following code anywhere in the file, preferably around the bottom of the file.
#make sure there's no space in the pasted code
export PATH=$PATH:path_to_the_program
Save it and restart your computer. That should put the program in the system's PATH.
2) Navigate to the folder of the program; then type
./program_name
Hope the explanation is clear and the answer helps.
Make sure that when you compile the program, you should use -o program_name to make the program name whatever you want it to be; otherwise, the program's name will be a.out, which would be very confusing.

Executing a compiled C program

I compiled a silly little "hello world" C program called main.c:
gcc main.c
As expected, a file called a.out appeared, which they say is an executable. From that same directory, if I type
a.out
and hit enter, it says "command not found". But if I type
./a.out
It says "hello world", as desired. I've never seen an executable that requires a './' in front of it to run. Why now?
All executables that aren't in your PATH require an explicit path from root / or the local directory ./ to run. A quick search turns up other threads with essentially the same question:
Why do you need ./ (dot-slash) before script name to run it in bash?
This also has the added benefit of helping with your auto completion in your shell (assuming it supports it). If you type just aTabTab then it will list every executable in your path that starts with "a". However, if you type ./aTab it will probably just auto-complete as a.out since it will only look at executable files in the current directory starting with "a". So, looking at it that way, the "./" actually saves you typing a few keys!
It is standard practice in Unix and Linux not to have the current working directory in the path. If you want to have MSDOS/Windows behavior, alter your PATH variable to include . as the first directory.
It's because the system is looking for a.out or any other exec. file in some special paths. And the current dir in not in that list by default (usually).
look at the list of such paths:
$ env|grep PATH
you can add such current dir to PATH env. variable:
$ export PATH=$PATH:.
But you better avoid doing that and run ./a.out.
Such tech. provides us understanding that we are running specified file from current dir,
not the other file with the same name from another (potentially) dir. So, we know what we run exactly.
When you type something like a.out into a Linux terminal, you're implying that you want to run a command called a.out. By default, the terminal does not look in the current directory for these commands, it looks in PATH - a set of directories for executable programs. It is usually these directories:
/bin
/usr/bin
/usr/local/bin
among others (you can check them all by running echo $PATH)
You have to specifiy the directory directory of your program for it to run, if it is not in one of the directories of PATH. For example:
./a.out works because . refers to the directory you're in
../a.out could work if a.out is in a parent directory (.. refers to the parent)
directory
projectdir/a.out also works, if your program is in the sub-directory, projectdir
That's because a.out is not in your $PATH.
The command you provide is searched in the $PATH (environment variable in linux) by the shell.
$PATH basically is the list of directories. When you provide the executable name, shell searches it in the directories provides by $PATH.
Since a.out is not in your $PATH, you've to explicitly provide the path to a.out.

How to run a .exe file with command prompt?

I am making an online judge. This my school project.
I am taking a .c file from the user. I am able to compile the .c file with command prompt. But I don't know how to run this file. I need to run this file, take input from a text file and save output in a text file. The compilation code was:
gcc main.c -o HelloWorld
I need to run this file, take input from a text file and save output
in a text file.
Assuming you're on Linux, this should work:
./HelloWorld < input.txt > output.txt
Just type in the full path. For example, if you compiled the file in %homedrive% with the name dummy.exe, type %homedrive%/dummy.exe.
Also, if you're already in %homedrive%, you can just type dummy.exe.
Edit: Assuming you're on Windows.
When you type HelloWorld in Linux terminal, your system will be searching this program in place, where PATH variable indicates. Most likely it is /bin. You can check your PATH by typing:
echo $PATH
So, you must precise, that HelloWorld is in concrete dirctory or change $PATH variable.
./HelloWorld
Dot indicates current directory
I assume you're on Linux? If yes, run it with:
./HelloWorld
The ./ is needed so that the shell knows to look for the executable file in the current directory. (Executables are not looked for automatically in the current directory due to security reasons.)
If on Windows, just type its name:
HelloWorld
Appending .exe to the filename is optional.
Redirecting standard input works like this:
HelloWorld < inputfile
Standard output is redirected with > instead:
HelloWorld > outputfile
You can combine both:
HelloWorld < inputfile > outputfile
You can install TCC
#!/usr/local/bin/tcc -run
or else try the option ./HelloWorld

Resources