准备工作
如果你还没装编译环境或自己不确定装没装,不妨先执行
sudo apt-get install build-essential
如果你不了解vim,选择gedit、kate或mousepad来编辑源代码就好了
编译简单的 C 程序
Hello World,下面是一示例代码:
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
gcc
$ gcc -Wall hello.c -o hello
-o
如果当前目录中与可执行文件重名的文件已经存在,它将被复盖。
-Wall 开启编译器几乎所有常用的警告──强烈建议你始终使用该选项。编译器有很多其他的警告选项,但 -Wall 是最常用的。默认情况下GCC
-Wall
要运行该程序,输入可执行文件的路径如下:
$ ./hello Hello, world!
./ 指代当前目录,因此 ./hello
捕捉错误
如上所述,当用 C 或 C++ 编程时,编译器警告是非常重要的助手。为了说明这一点,下面的例子包含一个微妙的错误:为一个整数值错误地指定了一浮点数控制符‘%f’。
#include <stdio.h>
int main (void)
{
printf ("Two plus two is %f\n", 4);
return 0;
}
-Wall。
编译上面的程序‘bad.c’,将得到如下的消息:
$ gcc -Wall bad.c -o bad
bad.c: In function 'main':
bad.c:6: warning: double format, different type arg (arg 2)
GCC 的消息总是具有下面的格式 文件名:行号:消息。编译器对错误与警告区别对待,前者将阻止编译,后者表明可能存在的问题但并不阻止程序编译。
%d。
-Wall,程序表面看起来编译正常,但是会产生不正确的结果:
$ gcc bad.c -o bad
$ ./bad
Two plus two is 2.585495
-Wall
编译多个源文件
一个源程序可以分成几个文件。这样便于编辑与理解,尤其是程序非常大的时候。这也使各部分独立编译成为可能。
Hello World
#include "hello.h"
int main(void)
{
hello ("world");
return 0;
}
printf,本例中我们用一个定义在文件‘hello_fn.c’中的函数 hello
hello 的声明。我们不需要在‘main.c’文件中包含系统头文件‘stdio.h’来声明函数 printf,因为‘main.c’没有直接调用 printf。
hello
void hello (const char * name);
hello
#include <stdio.h>
#include "hello.h"
void hello (const char * name)
{
printf ("Hello, %s!\n", name);
}
#include "FILE.h" 与 #include <FILE.h>
gcc编译以上源文件,使用下面的命令:
$ gcc -Wall main.c hello_fn.c -o newhello
-o 为可执行文件指定了一个不同的名字 newhello。注意到头文件‘hello.h’并未在命令行中指定。源文件中的的 #include "hello.h"
要运行本程序,输入可执行文件的路径名:
$ ./newhello
Hello, world!
源程序各部分被编译为单一的可执行文件,它与我们先前的例子产生的结果相同。
简单的 Makefile 文件
GNU Make
makefile(默认是当前目录下的名为‘Makefile’的文件)中读取项目的描述。makefile指定了一系列目标(比如可执行文件)和依赖(比如对象文件和源文件)的编译规则,其格式如下:
目标: 依赖 命令
命令行必须以单个的 TAB
隐含规则)来简化 makefile 的构建。比如说,它们指定‘.o’文件可以通过编译‘.c’文件得到,可执行文件可以通过将‘.o’链接到一起获得。隐含规则通过被叫做make变量的东西所指定,比如 CC(C 语言编译器)和 CFLAGS(C程序的编译选项);在makefile文件中它们通过独占一行的 变量=值 的形式被设置。对 C++ ,其等价的变量是CXX和CXXFLAGS,而变量CPPFLAGS则是编译预处理选项。
现在我们为上一节的项目写一个简单的 makefile 文件:
CC=gcc
CFLAGS=-Wall
hello: hello.o hello_fn.o
clean:
rm -f hello hello.o hello_fn.o
gcc,和编译选项‘-Wall’,从对象文件‘hello.o’和‘hello_fn.o’生成目标可执行文件 hello(文件‘hello.o’和‘hello_fn.o’通过隐含规则分别由‘hello.c’和‘hello_fn.c’生成)。目标clean没有依赖文件,它只是简单地移除所有编译生成的文件。rm命令的选项 ‘-f’(force) 抑制文件不存在时产生的错误消息。
make。不加参数调用make时,makefile文件中的第一个目标被建立,从而生成可执行文件‘hello’:
$ make
gcc -Wall -c -o hello.o hello.c
gcc -Wall -c -o hello_fn.o hello_fn.c
gcc hello.o hello_fn.o -o hello
$ ./hello
Hello, world!
make
$ vim hello.c (打开编辑器修改一下文件)
$ make
gcc -Wall -c -o hello.o hello.c
gcc hello.o hello_fn.o -o hello
$ ./hello
Hello, world!
最后,我们移除 make 生成的文件,输入 make clean:
$ make clean rm -f hello hello.o hello_fn.o
一个专业的 makefile文件通常包含用于安装(make install)和测试(make check)等额外的目标。
本文中涉及到的例子都足够简单以至于可以完全不需要makefile,但是对任何大些的程序都使用 make 是很有必要的。
链接外部库
存档文件(archive file)存储。
/usr/lib 与 /lib 中找到。比如,在类 Unix 系统中 C 语言的数学库一般存储为文件 /usr/lib/libm.a。该库中函数的原型声明在头文件 /usr/include/math.h 中。C 标准库本身存储为 /usr/lib/libc.a,它包含 ANSI/ISO C 标准指定的函数,比如‘printf’。对每一个 C 程序来说,libc.a 都默认被链接。
libm.a 中 sin 函数的的例子,创建文件calc.c:
#include <math.h>
#include <stdio.h>
int main (void)
{
double x = sin (2.0);
printf ("The value of sin(2.0) is %f\n", x);
return 0;
}
尝试单独从该文件生成一个可执行文件将导致一个链接阶段的错误:
$ gcc -Wall calc.c -o calc
/tmp/ccbR6Ojm.o: In function 'main':
/tmp/ccbR6Ojm.o(.text+0x19): undefined reference to ‘sin’
sin,未在本程序中定义也不在默认库‘libc.a’中;除非被指定,编译器也不会链接‘libm.a’。
sin
$ gcc -Wall calc.c /usr/lib/libm.a -o calc
sin,cos,exp,log及sqrt。链接器将搜索所有文件来找到包含 sin
sin
$ ./calc
The value of sin(2.0) is 0.909297
sin
为避免在命令行中指定长长的路径,编译器为链接函数库提供了快捷的选项‘-l’。例如,下面的命令
$ gcc -Wall calc.c -lm -o calc
与我们上面指定库全路径‘/usr/lib/libm.a’的命令等价。
-lNAME使链接器尝试链接系统库目录中的函数库文件 libNAME.a。一个大型的程序通常要使用很多 -l
编译C++
Hello world:
#include <iostream>
int main(int argc,char *argv[])
{
std::cout << "hello, world\n";
return 0;
}
gcc
$ gcc -Wall hello.cpp -o hello
/tmp/cch6oUy9.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cch6oUy9.o: In function `__tcf_0':
hello.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cch6oUy9.o: In function `main':
hello.cpp:(.text+0x8e): undefined reference to `std::cout'
hello.cpp:(.text+0x93): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<<
<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cch6oUy9.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
出错了!!而且错误还很多,很难看懂,这可怎么办呢?在解释之前,我们先试试下面的命令:
$ gcc -Wall hello.cpp -o hello -lstdc++
噫,加上-lstdc++选项后,编译竟然通过了,而且没有任何警告。运行程序,结果如下:
$ ./hello
hello, world
通过上节,我们可以知道,-lstdc++ 选项用来通知链接器链接静态库 libstdc++.a。而从字面上可以看出,libstdc++.a 是C++ 的标准库,这样一来,上面的问题我们就不难理解了──编译 C++ 程序,需要链接 C++ 的函数库 libstdc++.a。
gcc 是指 GNU 的 C 语言编译器(GNU C Compiler),随着 C++,Fortran 等语言的加入,gcc的含义才变化成了 GNU 编译器集合(GNU Compiler Collection)。C作为 gcc
GCC
$ g++ -Wall hello.cpp -o hello
实例,编译运行多个C++ 程序。
square.cpp
double square(double n)
{
return n * n;
}
main.cpp
#include <cstdlib>
#include <iostream>
//using namespace std ;
double square(double);
int main(int argc, char *argv[])
{
if(argc != 2)
{
std :: cerr << "Usage : square <number> "<< std :: endl;
return 1;
}
double n = strtod(argv[1], 0);
std :: cout << "The square of " << argv[1] << "is"
<< square(n) << std :: endl;
return 0;
}
运行结果:
ok ,that's all .