cygwin is a unix-like enviroment and command-line interface for microsoft windows.
cygwin is huge and includes most of the Unix tools and utilities. It also include the commonly used Bash shell.b
for c ,use gcc
for c++,use g++
below is an example
for hello.c. if the main function have parameter,than just put the paramter value after the executable file(below is hello.exe).
> gcc -o hello.exe hello.c //NT // Compile and link source file hello.c into executable hello.exe > hello // Execute hello.exe under CMD shell
NT:after -o,place hello.exe ahead,not hello.c
More GCC Compiler Options
A few commonly-used GCC compiler options are:
$ g++ -Wall -g -o Hello.exe Hello.cpp
-o
: specifies the output executable filename.-Wall
: prints "all
" warning messages.-g
: generates additional symbolic debugging information for use withgdb
debugger.
Compile and Link Separately
The above command compile the source file into object file and link
with other object files (system library) into executable in one step. You may separate compile and link in two steps as follows:
// Compile-only with -c option > g++ -c -Wall -g Hello.cpp // Link object file(s) into an executable > g++ -g -o Hello.exe Hello.o
The options are:
-c
: compile into object file "Hello.o
". By default, the object file has the same name as the source file with extension of ".o
" (there is no need to specify-o
option). No linking with other object file or library.- Linking is performed when the input file are object files "
.o
" (instead of source file ".cpp
" or ".c
"). GCC uses a separate linker program (calledld.exe
) to perform the linking.
Compile and Link Multiple Source Files
Suppose that your program has two source files: file1.cpp
, file2.cpp
. You could compile all of them in a single command:
> g++ -o myprog.exe file1.cpp file2.cpp
However, we usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.
> g++ -c file1.cpp > g++ -c file2.cpp > g++ -o myprog.exe file1.o file2.o
Compile into a Shared Library
To compile and link C/C++ program into a shared libary (".dll"
in Windows, ".so"
in Unixes), use -shared
option. Read "Java Native Interface" for example.
1.4 GCC Compilation Process
GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram. For example, a "gcc -o hello.exe hello.c
" is carried out as follows:
Verbose Mode (-v)
You can see the detailed compilation process by enabling -v
(verbose) option. For example,
> gcc -v hello.c -o hello.exe
above from below link:
GCC and Make: Compiling, Linking and Building C/C++ Applications
http://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
- Cygwin User's Guide
http://cygwin.com/cygwin-ug-net/cygwin-ug-net.html