About the meaning of "./a" in gcc - gcc

When I run
$ gcc hello.c
$ ./a
Hello, World.
I don't know what ./a exactly indicates.
What is it? What does it stand for?
If you know the meaning of it, I'd really appreciate that you would share.

./ is the current directory when using a Unix-like shell (like bash.) The name of the executable GCC produces is a.exe.
So to run the produced executable, you need to specify the path to to it, in this case "the current directory", which is ./, and the name of the executable, which is a.exe. Since you can omit the .exe when running executables on Windows, instead of ./a.exe you can just run it with ./a.
If you were to use the Windows command-line shell (like cmd.exe or PowerShell) you would instead just type a, because the current directory (.\ in this case, Windows uses \ instead of / for the directory separator character) is searched for executables by default. Unix shells do not, which is why you need ./.
If you want to give the produced executable a different name, for example hello.exe, you can:
gcc hello.c -o hello.exe
You would then run that with:
./hello
or:
./hello.exe

.a is the standard/default output of the compiled program, when no output name is provided.
When you've compiled C program and given no name to the output file, gcc will automatically set the output file name as a.
The file name is overwritten for the last compiled C program, when no output name is provided.
This standard is same in both Unix and Windows.
To set name for the output program, Use -o argument followed by the output name sum_program
gcc sum_program.c -o sum_program
Depending on the library use, and other linkers, additional arguments like -o can be added for compilation.

Related

Change default ARM gcc option to thumb

I want to change the behavior of the ARM toolchain arm-linux-gnueabi-gcc in my Linux machine, that the compiled code will be in Thumb mode as default - same as passing the -mthumb flag.
I came across this document, which under the section of --with-mode describes exactly what I try to achieve. However, I couldn't understand from their explanation how can I actually set this option.
Can anyone clarify this for me, or suggest another way to achieve my goal?
You can "mask" the executable file /usr/bin/arm-linux-gnueabi-gcc with your own script that is named the same inside /usr/local/bin.
Create a file /usr/local/bin/arm-linux-gnueabi-gcc
With the content
#!/bin/sh
/usr/bin/arm-linux-gnueabi-gcc -mthumb "$#"
Add executable permissions to /usr/local/bin/arm-linux-gnueabi-gcc
Because PATH should list /usr/local/bin directory before /usr/bin, when you type arm-linux-gnueabi-gcc without the path in your console, your script will chosen first and will execute the real arm-linux-gnueabi-gcc executable with the additional option.

Create multple object files at once without using a makefile?

When I try
gcc -c *.c
I get an invalid argument error, and it says no input files.
If you run gcc from a directory where no C source files are present, gcc will receive the *.c argument unexpanded, will try to open a file named *.c and fail, will report this failure and in the absence of further arguments, will complain about the missing input files:
$ gcc -c *.c
gcc: error: *.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
$
The wildcard expansion is performed by the command line interpreter, aka the shell. On a unix system, there are many different shells, sh, csh, tcsh, bash, zsh... all of which expand unquoted wildcards before running the commands. On Windows, the default shells do not expand wildcards for external commands, some programs do it on their own, but most don't. If you run bash on Windows, with or without cygwin, you will get the Unix behavior, but if you run cmd.exe, you won't.
MinGW is a set of development tools to make Windows native executables. It does not provide a shell and favors using the native libraries and utilities when possible. This is the reason why your command gcc -c *.c does not undergo wildcard expansion on your machine. Install bash or cygwin for a more unix-friendly environment.
This should do what you're looking for.
find . -name '*.c' -print | xargs gcc -c

How to get started with gfortran on a Mac?

Just finished installing the compiler gfortran 6.1. El Capitan. Next, I want to know how to run a file. So, I have a few questions:
Which extension should I use to save the file? Is it .f90 or something else?
What kind of software can be used to edit and save the source code?
Once I save the file, how do I compile it? Is is gfortran followed by the file name (with path) in the Terminal? Also, how does the path look like on a Mac?
A step by step guide would be a great help. I am a first time Mac user. :)
.f90 for free formatted sources is commonly used
Any texteditor you are comfortable with and used to, vim for example
gfortran /your/source/file that would be with an absolute path. If you are in the directory of the source file already you do not need to specify the complete path.
Step by step "hello world":
Create a text file with your Fortran program
Just to give a command line example without the need for an editor (this is not what you typically would do):
Open the terminal, then enter
cat >hello.f90 <<EOF
program hello
implicit none
write(*,*) 'hello world'
end program hello
EOF
Compile your program in the terminal with: gfortran hello.f90
Execute it in the terminal with ./a.out
If you want another name use the -o option:
gfortran -o hello hello.f90
./hello

how to run makefile in ubuntu

There are two simple .c files add.c and hello.c. this is my makefile-
all: add.exe hello.exe
add.exe: add.o
gcc -o add.exe add.o
hello.exe: hello.o
gcc -o hello.exe hello.o
add.o: add.c
gcc -c add.c
hello.o: hello.c
gcc -c hello.c
on typing make on terminal it shows all those four commands, but when i try to run this using ./all it says
bash: ./all: No such file or directory.
./hello.exe or ./add.exe works fine.
when I type ./all on my friend's pc, it shows proper output.
Your friend's PC is doing something strange and its not clear from your question what they've done. Your PC is acting normally.
Calling make all Will compile everything, but it doesn't actually make a program called all. It only compiles two programs: hello.exe and add.exe.
So calling ./all should fail because that is asking to run a program called all which doesn't exist.
It's quite possible that your friend has written themselves a program or script called "all". You'll need to ask your friend what that script / program does and how it does it.
Edit
To see what your friend has done open a terminal on your friends pc (like the one in your screen shot) and type the command
ls -lh
This will list all the files in that directory. Look for one named "all". It might look something like this (with the word "all" in green):
-rwxr----- 1 appy appy 67 Oct 23 15:05 all
Assuming that's there you can look at the contents of it by typing
cat ./all
To get yours to work like your friends, create a similar file with the same contents. To make it runnable you may need to change the file permissions:
chmod u+x all
"all" in this case is what is called a "pseudo-target" in make terminology. Since there is no build command under the all: ... line, there is nothing that will actually create a file called all (and in fact, having a file in that directory named all and having a timestamp newer than the .c, .o and .exe files is likely to confuse make). The pseudo-target here is simply a way to say that both the other .exe files must be built for make to consider its job done.
Assuming this is GNU Make, an entry .PHONY: all will tell it that all does not really correspond to a file that should exist.

Hello World in C++ says "Nothing More to be Done" [duplicate]

How can I compile/run C or C++ code in a Unix console or a Mac terminal?
If it is a simple single-source program,
make foo
where the source file is foo.c, foo.cpp, etc., you don’t even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus the extension.
Running the executable just built is the same as running any program - but you will most often need to specify the path to the executable as the shell will only search what is in $PATH to find executables, and most often that does not include the current directory (.).
So to run the built executable foo:
./foo
gcc main.cpp -o main.out
./main.out
This is the command that works on all Unix machines... I use it on Linux/Ubuntu, but it works in OS X as well. Type the following command in Terminal.app.
g++ -o lab21 iterative.cpp
-o is the letter O, not zero
lab21 will be your executable file
iterative.cpp is your C++ file
After you run that command, type the following in the terminal to run your program:
./lab21
Two steps for me:
First:
make foo
Then:
./foo
All application execution in a Unix (Linux, Mac OS X, AIX, etc.) environment depends on the executable search path.
You can display this path in the terminal with this command:
echo $PATH
On Mac OS X (by default) this will display the following colon separated search path:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
So any executable in the listed directories can by run just by typing in their name. For example:
cat mytextfile.txt
This runs /bin/cat and displays mytextfile.txt to the terminal.
To run any other command that is not in the executable search path requires that you qualify the path to the executable. So say I had an executable called MyProgram in my home directory on Mac OS X I can fully qualify it like so:
/Users/oliver/MyProgram
If you are in a location that is near the program you wished to execute you can qualify the name with a partial path. For example, if MyProgram was in the directory /Users/oliver/MyProject I and I was in my home directory I can qualify the executable name like this, and have it execute:
MyProject/MyProgram
Or say I was in the directory /Users/oliver/MyProject2 and I wanted to execute /Users/oliver/MyProject/MyProgram I can use a relative path like this, to execute it:
../MyProject/MyProgram
Similarly if I am in the same directory as MyProgram I need to use a "current directory" relative path. The current directory you are in is the period character followed by a slash. For example:
./MyProgram
To determine which directory you are currently in use the pwd command.
If you are commonly putting programs in a place on your hard disk that you wish to run without having to qualify their names. For example, if you have a "bin" directory in your home directory for regularly used shell scripts of other programs it may be wise to alter your executable search path.
This can be does easily by either creating or editing the existing .bash_profile file in your home directory and adding the lines:
#!/bin/sh
export PATH=$PATH:~/bin
Here the tilde (~) character is being used as a shortcut for /Users/oliver. Also note that the hash bang (#!) line needs to be the first line of the file (if it doesn't already exist). Note also that this technique requires that your login shell be bash (the default on Mac OS X and most Linux distributions). Also note that if you want your programs installed in ~/bin to be used in preference to system executables your should reorder the export statement as follows:
export PATH=~/bin:$PATH
Do all of this in "Terminal".
To use the G++ compiler, you need to do this:
Navigate to the directory in which you stored the *.cpp file.
cd ~/programs/myprograms/
(the ~ is a shortcut for your home, i.e. /Users/Ryan/programs/myprograms/, replace with the location you actually used.)
Compile it
g++ input.cpp -o output.bin (output.bin can be anything with any extension, really. Extension .bin is just common on Unix.)
There should be nothing returned if it was successful, and that is okay. Generally you get returns on failures.
However, if you type ls, you will see the list of files in the same directory. For example, you would see the other folders, input.cpp and output.bin
From inside the directory, now execute it with ./outbut.bin
A compact way to go about doing that could be:
make foo && ./$_
It is nice to have a one-liner so you can just rerun your executable again easily.
Assuming the current directory is not in the path, the syntax is ./[name of the program].
For example ./a.out
To compile C or C++ programs, there is a common command:
make filename
./filename
make will build your source file into an executable file with the same name. But if you want to use the standard way, You could use the gcc compiler to build C programs and g++ for C++.
For C:
gcc filename.c
./a.out
For C++:
g++ filename.cpp
./a.out
Add the following to get the best warnings, and you will not regret it. If you can, compile using WISE (warning is error).
- Wall -pedantic -Weffc++ -Werror
Step 1 - create a cpp file using the command
touch test.cpp
Step 2 - Run this command
g++ test.cpp
Step 3 - Run your cpp file
./a.out
I am on a new MacBook Pro with the Apple M1 Pro chip. I have my Xcode installed - both IDE and command line tools. This is how it worked for me:
g++ one.cpp -o one
./one
Use a makefile. Even for very small (= one-file) projects, the effort is probably worth it because you can have several sets of compiler settings to test things. Debugging and deployment works much easier this way.
Read the make manual. It seems quite long at first glance, but most sections you can just skim over. All in all, it took me a few hours and made me much more productive.
I found this link with directions:
http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/
Basically you do:
gcc hello.c
./a.out (or with the output file of the first command)
In order to compile and run C++ source code from a Mac terminal, one needs to do the following:
If the path of .cpp file is somePath/fileName.cpp, first go the directory with path somePath
To compile fileName.cpp, type c++ fileName.cpp -o fileName
To run the program, type ./fileName
Just enter in the directory in which your .c/.cpp file is.
For compiling and running C code.
gcc filename.c
./a.out filename.c
For compiling and running C++ code.
g++ filename.cpp
./a.out filename.cpp
You need to go into the folder where you have saved your file.
To compile the code: gcc fileName
You can also use the g++ fileName
This will compile your code and create a binary.
Now look for the binary in the same folder and run it.
For running C++ files, run the below command, assuming the file name is "main.cpp".
Compile to make an object file from C++ file.
g++ -c main.cpp -o main.o
Since #include <conio.h> is not supported on macOS, we should use its alternative which is supported on Mac. That is #include <curses.h>. Now the object file needs to be converted to an executable file. To use file curses.h, we have to use library -lcurses.
g++ -o main main.o -lcurses
Now run the executable.
./main
Running a .C file using the terminal is a two-step process.
The first step is to type gcc in the terminal and drop the .C file to the terminal, and then press Enter:
gcc /Desktop/test.c
In the second step, run the following command:
~/a.out

Resources