hello.cpp
#include <iostream>
using namespace std;
int main()
{
count << "Hello from C++!" << endl;
return 0;
}
hello.c
#include<stdio.h>
int main()
{
printf("Hello from C!\n");
return 0;
}
To compile and run these, give the commands:
g++ -g hello.cpp
ls
Notice that a file a.out has been created.
./a.out
gcc -g hello.c
./a.out
* The compiler generates an executable program called a.out If you don't like that name, you can use the mv command to rename it.
Alternatively, use a -o option to specify the name you would like for the compiled program:
g++ -g -o hello1 hello.cpp
./hello1
gcc -g -o hello2 hello.c
./hello2
* In the example above, we placed "./" in front of the file name of our compiled program to run it, in general, running prgrams is no different from running ordinary Unix commands. You just type
pathToProgramOrCommand parameters
In fact, almost all of the "commands" that we have used in this course are actually programs that were compiled as part of the installation of the Unix operation system.
* As we have noted earlier, we don't usually give the command/program name as a lengthy file path. We say, for example, "ls" instead of "/bin/ls". That works because certain directories, such as /bin, are automatically searched for a program of the appropriate name. This set of directories is referred to as your execution path. Your account was set up so that the directories holding the most commonly used Unix commands and programs are already in the execution path. You can see your path by giving the command
* echo $PATH
One thing that you will likely find i that your $PATH probably does not include ".", your current directory. Placing the current directory into the $PATH is considered a (minor) security risk, but that means that, if we had simply typed "a.out" or "hello", those programs would not have been found because the current directory is not in the search path. Hence, we gave the explicit path to the program files, "/a.out" and "./hello".
댓글 없음:
댓글 쓰기