How To Use Gcc Compiler
GCC and Brand
Compiling, Linking and Building
C/C++ Applications
GCC (GNU Compiler Collection)
A Brief History and Introduction to GCC
The original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-similar operating organization as gratuitous software, to promote freedom and cooperation among computer users and programmers.
GCC, formerly for "GNU C Compiler", has grown over times to support many languages such as C (gcc
), C++ (g++
), Objective-C, Objective-C++, Java (gcj
), Fortran (gfortran
), Ada (gnat
), Go (gccgo
), OpenMP, Cilk Plus, and OpenAcc. Information technology is now referred to equally "GNU Compiler Collection". The female parent site for GCC is http://gcc.gnu.org/. The electric current version is GCC 7.3, released on 2018-01-25.
GCC is a fundamental component of so-called "GNU Toolchain", for developing applications and writing operating systems. The GNU Toolchain includes:
- GNU Compiler Collection (GCC): a compiler suite that supports many languages, such every bit C/C++ and Objective-C/C++.
- GNU Make: an automation tool for compiling and building applications.
- GNU Binutils: a suite of binary utility tools, including linker and assembler.
- GNU Debugger (GDB).
- GNU Autotools: A build system including Autoconf, Autoheader, Automake and Libtool.
- GNU Bison: a parser generator (similar to lex and yacc).
GCC is portable and run in many operating platforms. GCC (and GNU Toolchain) is currently bachelor on all Unixes. They are also ported to Windows (by Cygwin, MinGW and MinGW-W64). GCC is also a cross-compiler, for producing executables on dissimilar platform.
GCC Versions
The various GCC versions are:
- GCC version 1 (1987): Initial version that support C.
- GCC version 2 (1992): supports C++.
- GCC version 3 (2001): incorporating ECGS (Experimental GNU Compiler Arrangement), with meliorate optimization.
- GCC version iv (2005):
- GCC version 5 (2015):
- GCC Version half dozen (2016):
- GCC Version 7 (2017):
C++ Standard Back up
There are various C++ standards:
- C++98
- C++11 (aka C++0x)
- C++14 (aka C++1y)
- C++17 (aka C++1z)
- C++2a (adjacent planned standard in 2020)
The default mode is C++98 for GCC versions prior to six.1, and C++14 for GCC half-dozen.1 and to a higher place. You can use command-line flag -std
to explicitly specify the C++ standard. For example,
-
-std=c++98
, or-std=gnu++98
(C++98 with GNU extensions) -
-std=c++11
, or-std=gnu++11
(C++eleven with GNU extensions) -
-std=c++14
, or-std=gnu++14
(C++14 with GNU extensions), default mode for GCC 6.1 and above. -
-std=c++17
, or-std=gnu++17
(C++17 with GNU extensions), experimental. -
-std=c++2a
, or-std=gnu++2a
(C++2a with GNU extensions), experimental.
Installing GCC on Unixes
GNU Toolchain, including GCC, is included in all Unixes. It is the standard compiler for most Unix-like operating systems.
Installing GCC on Mac Os 10
Open a Terminal, and enter "gcc --version
". If gcc
is not installed, the system will prompt you to install gcc
.
$ gcc --version ...... Target: x86_64-apple tree-darwin14.5.0 Thread model: posix
Installing GCC on Windows
For Windows, you lot could either install Cygwin GCC, MinGW GCC or MinGW-W64 GCC. Read "How to install Cygwin and MinGW".
- Cygwin GCC: Cygwin is a Unix-like environment and control-line interface for Microsoft Windows. Cygwin is huge and includes virtually of the Unix tools and utilities. It too included the commonly-used Bash shell.
- MinGW: MinGW (Minimalist GNU for Windows) is a port of the GNU Compiler Collection (GCC) and GNU Binutils for use in Windows. It also included MSYS (Minimal Organization), which is basically a Bourne shell (
bash
). - MinGW-W64: a fork of MinGW that supports both 32-scrap and 64-bit windows.
Diverse GCCs under Cygwin
There are many GCCs under Cygain/MinGW. To differentiate these variations, you demand to understand the followings:
- Windows/Intel uses these instruction sets: x86 is a 32-fleck education prepare; i868 is a 32-bit enhanced version of x86; x86_64 (or amd64) is a 64-chip pedagogy set.
- 32-bit compilers/programs can run on 32-bit or 64-bit (astern compatible) Windows, only 64-bit compiler can only run on 64-scrap Windows.
- 64-bit compilers may produce target of 32-fleck or 64-bit.
- If you employ Cygwin's GCC, the target could be native Windows or Cygwin. If the target is native Windows, the code can exist distributed and run under Windows. However, if the target is Cygwin, to distribute, you need to distribute Cygwin runtime environment (
cygwin1.dll
). This is because Cygwin is a Unix emulator under Windows.
MinGW-W64 Target 32/64-scrap Native Windows
The MinGW-W64 (a fork of MinGW, bachelor at http://mingw-w64.org/doku.php) supports target of both 32-bit and 64-fleck native Windows. You tin install "MinGW-W64" under "Cygwin" by selecting these packages (nether "devel" category):
-
mingw64-x86_64-gcc-core
: 64-flake C compiler for target of native 64-bit Windows. The executable is "x86_64-w64-mingw32-gcc
". -
mingw64-x86_64-gcc-g++
: 64-bit C++ compiler for target of native 64-bit Windows. The executable is "x86_64-w64-mingw32-g++
". -
mingw64-i686-gcc-core
: 64-fleck C compiler for target of native 32-flake Windows. The executable is "i686-w64-mingw32-gcc
". -
mingw64-i686-gcc-yard++
: 64-scrap C++ compiler for target of native 32-chip Windows. The executable is "i686-w64-mingw32-g++
".
Notes:
- I suggest you install "
mingw64-x86_64-gcc-core
" and "mingw64-x86_64-gcc-g++
" to provide native 64-chip Windows codes, but skip "mingw64-i686-gcc-core
" and "mingw64-i686-gcc-g++
", unless you need to produce 32-bit Windows applications. - For JNI (Java Native Interface) in 64-flake Coffee, y'all need to use "
x86_64-w64-mingw32-gcc
" or "x86_64-w64-mingw32-g++
" to produce 64-fleck native Windows code.
Run the executables and check the versions:
$ x86_64-w64-mingw32-gcc --version
x86_64-w64-mingw32-gcc (GCC) half-dozen.4.0 $ x86_64-w64-mingw32-gcc -5
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/6.4.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: .....
Thread model: posix
gcc version 6.4.0 (GCC) $ x86_64-w64-mingw32-chiliad++ --version
x86_64-w64-mingw32-g++ (GCC) 6.4.0 $ i686-w64-mingw32-gcc --version
i686-w64-mingw32-gcc (GCC) vi.4.0
$ i686-w64-mingw32-thousand++ --version
i686-w64-mingw32-one thousand++ (GCC) 6.4.0
Other GCCs in Cygwin
Other GCC packages in Cygwin are:
-
gcc-core, gcc-chiliad++
: Basic 64-bit C/C++ compiler target 64-chip Cygwin. You probably should install these two packages besides. However, to distribute the lawmaking produced, yous demand to distribute Cygwin Runtime Surround (cygwin1.dll
). This is because Cygwin is a Unix emulator nether Windows. -
cygwin32-gcc-cadre, cygwin32-gcc-g++
: Older 32-bit C/C++ compiler for target 32-bit Cygwin (Obsoleted bygcc-code
andgcc-m++
?). -
mingw-gcc-core, mingw-gcc-thousand++
: Older MinGW 32-bit C/C++ compiler for 32-fleck Windows (Obsoleted by MinGW-W64 packages?).
Post Installation
Versions
You could display the version of GCC via --version
option:
$ gcc --version gcc (GCC) 6.4.0 $ g++ --version yard++ (GCC) six.iv.0
More details tin be obtained via -v
option, for example,
$ gcc -five Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: ......
Thread model: posix
gcc version 6.4.0 (GCC) $ chiliad++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/6.four.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: ......
Thread model: posix
gcc version six.four.0 (GCC)
Help
You tin get the assistance manual via the --assist
selection. For example,
$ gcc --aid
Man Pages
You lot can read the GCC manual pages (or man pages) via the man
utility:
$ human gcc $ human g++
Reading man pages under CMD or Bash shell can be difficult. You could generate a text file via:
$ human gcc | col -b > gcc.txt
The col
utility is needed to strip the backspace. (For Cygwin, it is available in "Utils", "util-linux" bundle.)
Alternatively, you could look for an online man pages, e.g., http://linux.die.net/man/1/gcc.
The GCC man pages are kept under "usr/share/man/man1
".
$ whereis gcc gcc: /usr/bin/gcc.exe /usr/lib/gcc /usr/share/man/man1/gcc.1.gz
Getting Started
The GNU C and C++ compiler are called gcc
and g++
, respectively.
Compile/Link a Simple C Program - hello.c
Below is the Hi-globe C plan hello.c
:
one ii iii 4 5 vi 7 | #include <stdio.h> int chief() { printf("Hello, world!\n"); return 0; } |
To compile the hello.c
:
> gcc how-do-you-do.c
The default output executable is chosen "a.exe
" (Windows) or "a.out
" (Unixes and Mac OS X).
To run the programme:
> a $ chmod a+x a.out $ ./a.out
Notes for Unixes and Fustigate Vanquish:
- In Bash beat, the default PATH does non include the current working directory. Hence, you demand to include the electric current path (
./
) in the command. (Windows include the electric current directory in the PATH automatically; whereas Unixes do not - y'all need to include the electric current directory explicitly in the PATH.) - You too demand to include the file extension, if any, i.due east., "
./a.out
". - In Unixes, the output file could be "
a.out
" or simply "a
". Furthermore, you need to assign executable file-mode (x) to the executable file "a.out
", via control "chmod a+ten filename
" (add executable file-mode "+ten
" to all users "a+ten
").
To specify the output filename, use -o
option:
> gcc -o hello.exe hello.c > hello $ gcc -o hello hello.c $ chmod a+x hello $ ./how-do-you-do
Note for Unixes:
- In Unixes, we typically omit the
.exe
file extension (meant for Windows simply), and simply name the output executable ashullo
(via control "gcc -o hello hello.c
". - You demand to assign executable file mode via command "
chmod a+x hullo
".
Compile/Link a Simple C++ Program - howdy.cpp
1 2 three 4 v vi 7 8 | #include <iostream> using namespace std; int master() { cout << "Hello, world!" << endl; render 0; } |
Yous need to utilise one thousand++ to compile C++ plan, as follows. We use the -o
option to specify the output file proper noun.
> g++ -o hello.exe howdy.cpp > hi $ g++ -o howdy hello.cpp $ chmod a+10 hello $ ./hullo
More GCC Compiler Options
A few commonly-used GCC compiler options are:
$ thousand++ -Wall -g -o Hello.exe Hello.cpp
-
-o
: specifies the output executable filename. -
-Wall
: prints "all
" Westarning letters. -
-chiliad
: generates boosted symbolic debuggrandging information for use withgdb
debugger.
Compile and Link Separately
The in a higher place control compile the source file into object file and link
with other object files and arrangement libraries into executable in i stride. You may separate compile and link in two steps as follows:
> g++ -c -Wall -g Hello.cpp > grand++ -k -o Hello.exe How-do-you-do.o
The options are:
-
-c
: Compile into object file "Hullo.o
". By default, the object file has the same name as the source file with extension of ".o
" (at that place is no need to specify-o
option). No linking with other object files or libraries. - 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
. Y'all 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 i file does not crave re-compilation of the other files.
> chiliad++ -c file1.cpp > thou++ -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 library (".dll"
in Windows, ".so"
in Unixes), use -shared
pick. Read "Java Native Interface" for example.
GCC Compilation Procedure
GCC compiles a C/C++ plan into executable in 4 steps as shown in the above diagram. For case, a "gcc -o hello.exe hello.c
" is carried out every bit follows:
- Pre-processing: via the GNU C Preprocessor (
cpp.exe
), which includes the headers (#include
) and expands the macros (#define
).> cpp how-do-you-do.c > hello.i
The resultant intermediate file "hullo.i
" contains the expanded source lawmaking. - Compilation: The compiler compiles the pre-processed source lawmaking into assembly code for a specific processor.
> gcc -South hello.i
The-S
option specifies to produce assembly lawmaking, instead of object lawmaking. The resultant assembly file is "hi.s
". - Associates: The assembler (
equally.exe
) converts the assembly code into machine code in the object file "howdy.o
".> as -o hi.o hello.s
- Linker: Finally, the linker (
ld.exe
) links the object code with the library code to produce an executable file "hello.exe
".> ld -o hullo.exe hello.o ...libraries...
Verbose Fashion (-v)
You tin see the detailed compilation procedure by enabling -v
(verbose) choice. For case,
> gcc -5 -o hello.exe hi.c
Defining Macro (-D)
You lot can apply the -Dname
choice to define a macro, or -Dname=value
to define a macro with a value. The value
should be enclosed in double quotes if it contains spaces.
Headers (.h), Static Libraries (.lib, .a) and Shared Library (.dll, .and so)
Static Library vs. Shared Library
A library is a collection of pre-compiled object files that can be linked into your programs via the linker. Examples are the organisation functions such equally printf()
and sqrt()
.
There are two types of external libraries: static library and shared library.
- A static library has file extension of "
.a
" (archive file) in Unixes or ".lib
" (library) in Windows. When your programme is linked against a static library, the machine code of external functions used in your program is copied into the executable. A static library can be created via the archive program "ar.exe
". - A shared library has file extension of "
.so
" (shared objects) in Unixes or ".dll
" (dynamic link library) in Windows. When your program is linked against a shared library, only a small table is created in the executable. Before the executable starts running, the operating organisation loads the machine lawmaking needed for the external functions - a procedure known as dynamic linking. Dynamic linking makes executable files smaller and saves deejay space, considering one re-create of a library can be shared between multiple programs. Furthermore, most operating systems allows one copy of a shared library in memory to be used by all running programs, thus, saving memory. The shared library codes tin can be upgraded without the need to recompile your programme.
Considering of the advantage of dynamic linking, GCC, by default, links to the shared library if it is bachelor.
You lot can list the contents of a library via "nm filename
".
Searching for Header Files and Libraries (-I, -L and -l)
When compiling the program, the compiler needs the header files to compile the source codes; the linker needs the libraries to resolve external references from other object files or libraries. The compiler and linker will not detect the headers/libraries unless y'all set the appropriate options, which is not obvious for first-time user.
For each of the headers used in your source (via #include
directives), the compiler searches the then-chosen include-paths for these headers. The include-paths are specified via -Idir
option (or environs variable CPATH
). Since the header's filename is known (east.g., iostream.h
, stdio.h
), the compiler only needs the directories.
The linker searches the so-called library-paths for libraries needed to link the programme into an executable. The library-path is specified via -Fiftydir
option (majuscule 'L'
followed past the directory path) (or environment variable LIBRARY_PATH
). In improver, you also accept to specify the library name. In Unixes, the library libxxx.a
is specified via -50xxx
option (lowercase letter 'l'
, without the prefix "lib
" and ".a
" extension). In Windows, provide the full name such every bit -lxxx.lib
. The linker needs to know both the directories likewise as the library names. Hence, two options need to be specified.
Default Include-paths, Library-paths and Libraries
Effort list the default include-paths in your arrangement used by the "GNU C Preprocessor" via "cpp -v
":
> cpp -v ...... #include "..." search starts here: #include <...> search starts hither: /usr/lib/gcc/x86_64-pc-cygwin/vi.four.0/include
/usr/include
/usr/lib/gcc/x86_64-pc-cygwin/6.iv.0/../../../../lib/../include/w32api
Attempt running the compilation in verbose manner (-v
) to study the library-paths (-L
) and libraries (-l
) used in your system:
> gcc -v -o hello.exe howdy.c ...... -Fifty/usr/lib/gcc/x86_64-pc-cygwin/6.4.0 -L/usr/x86_64-pc-cygwin/lib -L/usr/lib -L/lib -lgcc_s -lgcc -lcygwin -ladvapi32 -lshell32 -luser32 -lkernel32
Eclipse CDT: In Eclipse CDT, you can ready the include paths, library paths and libraries by right-click on the projection ⇒ Backdrop ⇒ C/C++ Full general ⇒ Paths and Symbols ⇒ Under tabs "Includes", "Library Paths" and "Libraries". The settings are applicable to the selected project but.
GCC Surroundings Variables
GCC uses the following environment variables:
-
PATH
: For searching the executables and run-fourth dimension shared libraries (.dll
,.so
). -
CPATH
: For searching the include-paths for headers. Information technology is searched subsequently paths specified in-I<dir>
options.C_INCLUDE_PATH
andCPLUS_INCLUDE_PATH
can be used to specify C and C++ headers if the particular language was indicated in pre-processing. -
LIBRARY_PATH
: For searching library-paths for link libraries. Information technology is searched after paths specified in -Fifty<dir>
options.
Utilities for Examining the Compiled Files
For all the GNU utilities, you tin use " command --assist
" to listing the help menu; or "human being control
" to brandish the human being pages.
"file" Utility - Determine File Type
The utility "file
" can be used to brandish the blazon of object files and executable files. For example,
$ gcc -c hello.c $ gcc -o hello.exe hello.o $ file hullo.c
hello.c: C source, ASCII text, with CRLF line terminators $ file hello.o howdy.o: data > file how-do-you-do.exe hello.exe: PE32 executable (panel) x86-64, for MS Windows
"nm" Utility - List Symbol Table of Object Files
The utility "nm
" lists symbol tabular array of object files. For example,
$ nm hello.o 0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
U __main
0000000000000000 T main
U puts $ nm how-do-you-do.exe | grep main
00000001004080cc I __imp___main
0000000100401120 T __main
00000001004010e0 T main
......
"nm" is commonly-used to bank check if a particular function is divers in an object file. A 'T'
in the second column indicates a function that is defined, while a 'U'
indicates a office which is undefined and should be resolved by the linker.
"ldd" Utility - List Dynamic-Link Libraries
The utility "ldd
" examines an executable and displays a list of the shared libraries that it needs. For example,
> ldd hello.exe ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ff9ba3c0000)
KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ff9b9880000)
KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ff9b6a60000)
SYSFER.DLL => /cygdrive/c/WINDOWS/System32/SYSFER.DLL (0x6ec90000)
ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ff9b79a0000)
msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ff9b9100000)
sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ff9b9000000)
RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ff9b9700000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)
GNU Make
The "brand
" utility automates the mundane aspects of building executable from source lawmaking. "make
" uses a so-chosen makefile
, which contains rules on how to build the executables.
You tin can issue "make --help
" to listing the command-line options; or "human make
" to display the human pages.
Get-go Makefile By Instance
Let'south begin with a elementary case to build the Hi-world program (hello.c
) into executable (hello.exe
) via make utility.
1 two 3 4 5 half dozen 7 | #include <stdio.h> int main() { printf("Hello, earth!\n"); return 0; } |
Create the following file named "makefile" (without whatsoever file extension), which contains rules to build the executable, and save in the same directory as the source file. Use "tab" to indent the command (Not spaces).
all: hello.exe hello.exe: hi.o gcc -o hello.exe hello.o hello.o: hullo.c gcc -c howdy.c clean: rm how-do-you-do.o hello.exe
Run the "make
" utility every bit follows:
> make gcc -c hello.c gcc -o hello.exe hello.o
Running brand
without argument starts the target "all
" in the makefile
. A makefile consists of a set of rules. A rule consists of 3 parts: a target, a listing of pre-requisites and a command, as follows:
target: pre-req-1 pre-req-2 ... command
The target and pre-requisites are separated by a colon (:
). The command must be preceded by a tab (Not spaces).
When brand
is asked to evaluate a rule, it begins past finding the files in the prerequisites. If any of the prerequisites has an associated rule, make attempts to update those first.
In the above example, the rule "all
" has a pre-requisite "hullo.exe
". make
cannot observe the file "hello.exe
", so it looks for a rule to create information technology. The rule "howdy.exe
" has a pre-requisite "hello.o
". Again, information technology does not be, then make
looks for a rule to create it. The dominion "hello.o
" has a pre-requisite "hello.c
". brand
checks that "hello.c
" exists and information technology is newer than the target (which does not exist). Information technology runs the command "gcc -c hi.c
". The rule "hello.exe
" then run its control "gcc -o hello.exe hello.o
". Finally, the rule "all
" does nothing.
More importantly, if the pre-requisite is not newer than than target, the command will non be run. In other words, the control will exist run only if the target is out-dated compared with its pre-requisite. For instance, if we re-run the make command:
> brand make: Zero to be washed for `all'.
You can besides specify the target to be made in the make
command. For instance, the target "clean
" removes the "howdy.o
" and "how-do-you-do.exe
". You can then run the brand
without target, which is the same equally "make all
".
> make clean rm how-do-you-do.o hello.exe > make gcc -c hello.c gcc -o hello.exe how-do-you-do.o
Try modifying the "hello.c
" and run make
.
NOTES:
- If the command is not preceded past a tab, you get an mistake message "makefile:4: *** missing separator. Stop."
- If there is no
makefile
in the current directory, you get an error message "make: *** No targets specified and no makefile found. Finish." - The makefile tin be named "
makefile
", "Makefile
" or "GNUMakefile
", without file extension.
More than on Makefile
Comment & Continuation
A comment begins with a #
and lasts till the end of the line. Long line tin be cleaved and connected in several lines via a back-slash (\
).
Syntax of Rules
A full general syntax for the rules is:
target1 [target2 ...]: [pre-req-i pre-req-2 ...] [command1 command2 ......]
The rules are usually organized in such as manner the more than general rules come beginning. The overall dominion is frequently proper noun "all
", which is the default target for brand
.
Phony Targets (or Artificial Targets)
A target that does not represent a file is called a phony target. For case, the "make clean
" in the above example, which is merely a characterization for a control. If the target is a file, it volition be checked against its pre-requisite for out-of-appointment-ness. Phony target is always out-of-appointment and its command will exist run. The standard phony targets are: all
, make clean
, install
.
Variables
A variable begins with a $
and is enclosed within parentheses (...)
or braces {...}
. Single character variables practice not demand the parentheses. For case, $(CC)
, $(CC_FLAGS)
, $@
, $^
.
Automated Variables
Automated variables are prepare by make after a rule is matched. There include:
-
$@
: the target filename. -
$*
: the target filename without the file extension. -
$<
: the kickoff prerequisite filename. -
$^
: the filenames of all the prerequisites, separated by spaces, discard duplicates. -
$+
: similar to$^
, simply includes duplicates. -
$?
: the names of all prerequisites that are newer than the target, separated past spaces.
For example, we tin rewrite the before makefile as:
all: hello.exe # $@ matches the target; $< matches the first dependent hello.exe: hullo.o gcc -o $@ $< hullo.o: hello.c gcc -c $< make clean: rm hello.o hi.exe
Virtual Path - VPATH & vpath
You can use VPATH
(uppercase) to specify the directory to search for dependencies and target files. For example,
VPATH = src include
You can also use vpath
(lowercase) to be more precise nearly the file type and its search directory. For case,
vpath %.c src vpath %.h include
Pattern Rules
A blueprint rule, which uses pattern matching character '%'
equally the filename, can be practical to create a target, if there is no explicit rule. For example,
%.o: %.c $(COMPILE.c) $(OUTPUT_OPTION) $< %: %.o $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
Implicit Pattern Rules
Make comes with a huge set of implicit blueprint rules. You tin listing all the rule via --print-data-base
option.
A Sample Makefile
This sample makefile is extracted from Eclipse'southward "C/C++ Development Guide -Makefile".
LINK_TARGET = test_me.exe OBJS = \ Test1.o \ Test2.o \ Principal.o REBUILDABLES = $(OBJS) $(LINK_TARGET) clean : rm -f $(REBUILDABLES) echo Clean done all : $(LINK_TARGET) echo All washed $(LINK_TARGET) : $(OBJS) yard++ -grand -o $@ $^ %.o : %.cpp g++ -one thousand -o $@ -c $< Main.o : Main.h Test1.h Test2.h Test1.o : Test1.h Test2.h Test2.o : Test2.h # %.dep : %.cpp # yard++ -M $(FLAGS) $< > $@ # include $(OBJS:.o=.dep)
Brief Summary
I take presented the bones make features here so that you lot can read and sympathize unproblematic makefiles for building C/C++ applications. Brand is actually quite complex, and can be considered equally a programming language past itself!!
REFERENCES & RESOURCES
- GCC Manual "Using the GNU Compiler Drove (GCC)" @ http://gcc.gnu.org/onlinedocs.
- GNU 'make' manual @ http://www.gnu.org/software/make/manual/make.html.
- Robert Mecklenburg, "Managing Projects with GNU Make", 3rd Edition, 2004.
Latest version tested: CygWin GCC 6.iv.0, MinGW-W64 GCC six.iv.0
Last modified: March, 2018
How To Use Gcc Compiler,
Source: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
Posted by: ratliffpeammeak.blogspot.com
0 Response to "How To Use Gcc Compiler"
Post a Comment