Compiling with
g++
g++
Executive Summary: This document is a brief description of how to compile basic C++ programs using g++
. It provides sample command lines for invoking the g++
compiler and a list of some common compiler options.
内容提要:本文档简要说明了如何使用 g++
编译基本的 C++ 程序。它提供了用于调用 g++
编译器的示例命令行以及一些常用编译器选项的列表。
1. What is g++
?
g++
is your friendly Gnu C++ compiler. g++
does not handle templates well, but you can use them. This document serves as a very simple bunch of hints to start using g++
, and is not meant to be complete. For all the gory details about g++
's options, check out its man page.
g++
是您友好的 Gnu C++ 编译器。g++
不能很好地处理模板,但是您可以使用它们。该文档只是开始使用 g++
的非常简单的提示,并不意味着它是完整的。有关 g++
选项的所有详细信息,请查看其手册页。
2. Compiling hello_world.c
Say you have a file hello_world.c
as follows :
/*
============================================================================
Name : hello_world.c
Author : Yongqiang Cheng
Version : Version 1.0.0
Copyright : Copyright (c) 2019 Yongqiang Cheng
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
You can compile and run it from the Linux prompt as follows :
strong@foreverstrong:~/Desktop/makefile_work$ g++ hello_world.c
This creates an executable called a.out
. You can run it by typing
strong@foreverstrong:~/Desktop/makefile_work$ ./a.out
Since no executable name was specified to g++
, a.out
is chosen by default. Use the -o
option to change the name:
strong@foreverstrong:~/Desktop/makefile_work$ g++ -o hello_world hello_world.c
creates an executable called hello_world
.
strong@foreverstrong:~/Desktop/makefile_work$ pwd
/home/strong/Desktop/makefile_work
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 4
-rw-rw-r-- 1 strong strong 491 Feb 6 12:22 hello_world.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ g++ hello_world.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rwxrwxr-x 1 strong strong 8608 Feb 6 12:25 a.out
-rw-rw-r-- 1 strong strong 491 Feb 6 12:22 hello_world.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./a.out
!!!Hello World!!!
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ g++ -o hello_world hello_world.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 28
-rwxrwxr-x 1 strong strong 8608 Feb 6 12:25 a.out
-rwxrwxr-x 1 strong strong 8608 Feb 6 12:26 hello_world
-rw-rw-r-- 1 strong strong 491 Feb 6 12:22 hello_world.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./a.out
!!!Hello World!!!
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./hello_world
!!!Hello World!!!
strong@foreverstrong:~/Desktop/makefile_work$ g++ -o hello_world hello_world.c -print-search-dirs
install: /usr/lib/gcc/x86_64-linux-gnu/5/
programs: =/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/bin/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/bin/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/bin/
libraries: =/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/lib/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/lib/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/5/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/5/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:/usr/lib/
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$
3. Include Directories
Sometimes the header files that you write are not in the same directory as the .c
file that #include
's it. For example you might have a a file foo.h
that resides in /homes/me/randomplace/include
. If you want to include that file in hello_world.c
, you could just give the full path name in the #include
, OR you can do the following:
Add
#include <foo.h>
to hello_world.c
and compile it with the -I
option :
% g++ -o helloworld -I/homes/me/randomplace/include hello_world.c
This basically tells g++
to look for #include
's in /homes/me/include
in addition to other directories you specify with -I
4. Compiling multiple files
Most likely, you will be compiling separate modules and linking them into a single executable. Here’s the basic idea: compile each .c
file into a .o
file, then link the .o
files (along with any libraries) into an executable. Of course, one of these .c
files has to define the main()
or else the linker will complain. Suppose we have main.c
, foo.c
and bar.c
and want to create an executable fubar
, and suppose further that we need the math library:
您很可能将编译单独的模块,并将它们链接到单个可执行文件中。这是基本思想:将每个 .c
文件编译为一个 .o
文件,然后将 .o
文件 (以及所有库) 链接到一个可执行文件中。当然,这些 .c
文件之一必须定义 main()
,否则链接器会抱怨。 假设我们有 main.c
, foo.c
and bar.c
,并想创建可执行文件 fubar
,并进一步假设我们需要数学库:
g++ -c -o foo.o foo.c
g++ -c -o main.o main.c
g++ -c -o bar.o bar.c
g++ -o fubar foo.o main.o bar.o -lm
The first three commands generate foo.o
, main.o
and bar.o
respectively. The last line links them together along with the math library, libm.a
.
编译器优先考虑动态库,如果希望编译器只链接静态库,可以指定 -static
选项。
5. Some options
-g
- turn on debugging (so GDB gives more friendly output)
-Wall
- turns on most warnings
-O
or -O2
- turn on optimizations
-o <name>
- name of the output file
-c
- output an object file (.o
)
-I<include path>
- specify an include directory
-L<library path>
- specify a lib directory
-l<library>
- link with library lib<library>.a
/ lib<library>.so
References
Data Structures and Algorithms
https://courses.cs.washington.edu/courses/cse373/99au/index.html
Computing
https://courses.cs.washington.edu/courses/cse373/99au/computing.html
Using UNIX
https://courses.cs.washington.edu/courses/cse373/99au/unix/unix.html
Using Emacs
https://courses.cs.washington.edu/courses/cse373/99au/unix/emacs.html
Compiling with g++
https://courses.cs.washington.edu/courses/cse373/99au/unix/g++.html
Using GDB
https://courses.cs.washington.edu/courses/cse373/99au/unix/gdb.html
Using make
https://courses.cs.washington.edu/courses/cse373/99au/unix/make.html