安装两个RPM包便可搞定html
[root@localhost Desktop]# rpm -ivh /home/weiwei/Desktop/libstdc++-devel-4.4.5-6.el6.i686.rpm [root@localhost Desktop]# rpm -ivh /home/weiwei/Desktop/gcc-c++-4.4.5-6.el6.i686.rpm
查看g++是否安装成功java
[root@localhost Desktop]# g++ -v Using built-in specs. Target: i686-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch=i686 --build=i686-redhat-linux Thread model: posix gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
gcc与g++的区别
linux
gcc能够用来编译C或者C++,但他只能编译c++源文件,不能自动和C++程序使用的库链接,g++能够实现C++程序的编译和连接,其实他也是调用gcc来编译的,要编译c++代码生成可执行文件要用 g++ios
编写一个简单的c++程序c++
// myfirst.cpp--displays a message #include <iostream> // make definitions visible using namespace std; int main() // function header { // start of function body cout << "Come up and C++ me some time."; // message cout << endl; // start a new line cout << "You won't regret it!" << endl; // more output return 10; // terminate main() 返回值为0表明成功,非0返回值的含义由系统自定义 } // end of function body
打开命令窗口进行编译redis
[root@localhost Desktop]# g++ -o test1 test.cpp
-o test1 test.cpp 从test.cpp编译生成test1文件,test1为可执行文件,没有后缀shell
若是不写-o test1 会默认生成一个a.out可执行文件express
执行可执行文件bootstrap
[root@localhost Desktop]# ./a.out Come up and C++ me some time. You won't regret it!
获取main函数返回值
eclipse
[root@localhost Desktop]# echo $? 10
对c源文件预处理生成中间文件e.i
[root@localhost c]# g++ -E funcuse.c -o e.i
对预处理文件进行处理生成汇编语言文件e.s
[root@localhost c]# g++ -S e.i -o e.s
上述两部能够直接合并为
[root@localhost c]# g++ -s e.i -o e.s
生成目标文件,目标文件是机器代码,但不能执行,必须将目标文件与其余目标文件或库文件链接生成可执行的二进制文件才能执行
[root@localhost c]# g++ -c e.s -o e.o
[root@localhost c]# g++ e.o -o result
[root@localhost c]# ./result
在linux操做系统中运行程序必须指定程序所在的目录,除非程序的目录已经列在PATH环境变量中,因此程序前必须加./
注:echo $? 显示main函数的返回值(int型)
若是想让编译和运行同时进行能够采用以下命令:
gcc funcuse.c -o result && ./result
&&表示若是成功就。。。若是编译成功,会直接运行程序
能够将上述全部步骤合并写为
g++ funcuse.c -o result
或
g++ -o result funcuse.c
直接生成可执行文件
程序若是复杂的话,程序的各个部分会分别存储在不一样的文件中,按照逻辑进行划分。
来自:http://www.cnblogs.com/lidabo/archive/2012/04/17/2454568.html
头文件的做用就是被其余的.cpp包含,自己并不参与编译,但实际上它们的内容却在多个.cpp文件中获得了 编译.
头文件中应该只放变量和函数的声明,而不能放它们的定义
这个规则是有三个例外的
头文件中能够写const对象的定义
头文件中可 以写内联函数(inline)的定义
头文件中能够写类 (class)的定义
若是将程序分红若干子程序,怎样在linux下进行编译呢?
下面以求圆的面积为例来讲明
Circle.h
#ifndef CIRCLE_H #define CIRCLE_H class Circle { private: double r; public: Circle(); Circle(double R); double Area(); }; #endif
Circle.cpp
#include "Circle.h" #include <iostream> using namespace std; Circle::Circle() { this->r=5.0; } Circle::Circle(double R) { this->r=R; } double Circle:: Area() { return 3.14*r*r; }
main.cpp
#include "Circle.h" #include <iostream> using namespace std; int main(int argc, char *argv[]) { Circle c(3); cout<<"Area="<<c.Area()<<endl; return 0; }
编译
[root@localhost cpp]# g++ -c Circle.cpp -o Circle.o [root@localhost cpp]# g++ -c main.cpp -o main.o [root@localhost cpp]# g++ main.o Circle.o -o main [root@localhost cpp]# ./main Area=28.26
-c命令表示编译,头文件不准显式编译,但实际已经编译。若是只修改了一个源文件,只须要编译改动的文件
但若是咱们的程序有几百个源程序的时候,怎么办?难道也要编译器从新一个一个的编译?
makefile关系到了整个工程的编译规则。一个工程中的源文件不计数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件须要先编译,哪些文件须要后编译,哪些文件须要从新编译,甚至于进行更复杂的功能操做,由于makefile就像一个Shell脚本同样,其中也能够执行操做系统的命令。
makefile带来的好处就是——“自动化编译”,一旦写好,只须要一个make命令,整个工程彻底自动编译,极大的提升了软件开发的效率
#此行为注释 main: main.o Circle.o g++ main.o Circle.o -o main Circle.o:Circle.cpp g++ -c Circle.cpp -o Circle.o main.o:main.cpp g++ -c main.cpp -o main.o
注意:g++命令开头的行前面必须有tab空格,否则会报错: *** missing separator. Stop
若是将名字命名为Makefile或makefile,只须要在命令行下敲入make就能够进行自动化编译
[root@localhost cpp]# make g++ -c main.cpp -o main.o g++ -c Circle.cpp -o Circle.o g++ main.o Circle.o -o main [root@localhost cpp]# ./main Area=28.26
参考:
http://blog.163.com/dong_box/blog/static/2625977820103310933870/
[精华] 跟我一块儿写 Makefile - ChinaUnix.net
编写Makefile - 学习,思考,记录,分享。 - 博客频道 - CSDN.NET
启动gdb
[root@localhost c]# gdb GNU gdb (GDB) Red Hat Enterprise Linux (7.2-48.el6) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i686-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>.
调试前要先进性编译链接
[root@localhost c]# g++ -g funcuse.c -o dbug
进行调试
[root@localhost c]# gdb dbug Reading symbols from /home/weiwei/Desktop/c/dbug...done.
列出代码
(gdb) list 1 #include<stdio.h> 2 3 int main(){ 4 for(int i=0 ; i<5; i++){ 5 printf("this is %d\n",i); 6 } 7 return 0; 8 }
(gdb) list 3,5 3 int main(){ 4 for(int i=0 ; i<5; i++){ 5 printf("this is %d\n",i);
执行程序
(gdb) run Starting program: /home/weiwei/Desktop/c/dbug this is 0 this is 1 this is 2 this is 3 this is 4
设置断点
(gdb) break 5 Breakpoint 1 at 0x8048487: file funcuse.c, line 5.
运行
(gdb) r Starting program: /home/weiwei/Desktop/c/dbug Breakpoint 1, main () at funcuse.c:5 5 printf("this is %d\n",i); Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.25.el6.i686 libgcc-4.4.5-6.el6.i686 libstdc++-4.4.5-6.el6.i686
到断点处程序中止,继续运行输入continue
(gdb) c Continuing. this is 0 Breakpoint 1, main () at funcuse.c:5 5 printf("this is %d\n",i); (gdb) c Continuing. this is 1 Breakpoint 1, main () at funcuse.c:5 5 printf("this is %d\n",i); (gdb) c Continuing. this is 2
监测某一个值
(gdb) watch i Hardware watchpoint 2: i
(gdb) c Continuing. this is 3 Hardware watchpoint 2: i Old value = 3 New value = 4 0x080484a0 in main () at funcuse.c:4 4 for(int i=0 ; i<5; i++){
查看某一个特定的变量
(gdb) print i
自动显示变量的值,每次运行到断点处都会自动显示
(gdb) display i
查看当前自动显示的全部变量
(gdb) info display Auto-display expressions now in effect: Num Enb Expression 1: y i
查看变量类型
(gdb) whatis i type = int
单步执行,step进入函数内部( 使用return命令跳出来 ,跳出前可使用finish执行完函数体),next把函数当成一条语句不进入函数内部
(gdb) step (gdb) next
删除编号为1的断点
(gdb) delete 1