首页 > 代码库 > Running the complier from the Command Line

Running the complier from the Command Line

Running the complier from the Command Line

CC prog1.cc

 

For windows, it will generate the executable file named prog1.exe

For UNIX, it tends to put their executables in files named a.out

 

Remarks:

About cc and gcc

 

Cc refers to the C compiler in UNIX while gcc comes from Linux, which is the abbreviation of GNU compiler collection. Notice that gcc is a collection of compilers(not only related to C and C++).

 

Actually, in Linux, cc is the same as gcc. cc is just a soft link of gcc to make it easy to transfer the project from UNIX to Linux.

 

About gcc and g++

 

Gcc regards file with suffix .c as C program while g++ regards it as C++ program. cpp is regarded as C++ program for both commands. 

In the compiling, g++ will invoke gcc command. Because gcc can not automatically connect to the libraries, while g++ can. So we usually use g++ instead of gcc.

 

In my Mac, when I enter gcc HelloWorld.cpp, it will throw a lot of error message for undefined symbols, which will not happen if you use g++ command.

The program in HelloWorld is very easy.

 

#include <iostream>

using namespace std;

 

int main(int argc, const char * argv[]) {

    // insert code here...

    std::cout << "Hello, World!\n";

    return 0;

 

 

Referencehttp://www.cnblogs.com/xiedan/archive/2009/10/25/1589462.html

 

After compiling, we can directly run the executable file:

For windows, we just enter “prog1"

For Unix, we should use ./a.out.    (./ indicates the file is in the current directory)

In my Mac, the output is as below:

181-39:CppStudy luke$ ./a.out 

Hello, World!

 

The value returnd from main is accessed in a system-dependent manner. On both UNIX and Windows systems, after executing the program, you must issue an appropriate echo command echo $?. In windows, we write echo %ERRORLEVEL%

In my Mac, the output of the echo $? command is as below:

181-39:CppStudy luke$ echo $?

0

 

Zero is the return value of the main function. 

 

 

If you use g++ -o prog1 prog1.cc, it will generate an executable file named prog1 instead of a.out. For example, in my Mac, when I enter g++ -o lukewang main.cpp, then the it will generate a file named lukewang, you can use ./lukewang to run the file. The -o is an argument(参数) to the compiler, which can not be omitted.

 

Exercise 1.2: If the return value is -1, the result of command echo $? is 255.

 

Remarks: I use GDB as the debugging tool.

Here is the reference about gdb and gcc.

http://blog.163.com/liuqiang_mail@126/blog/static/109968875201292625126644/

 

Running the complier from the Command Line