Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239245
  • 博文数量: 91
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-12 09:38
文章分类

全部博文(91)

文章存档

2017年(1)

2011年(1)

2008年(15)

2007年(74)

我的朋友

分类: LINUX

2007-12-28 16:18:33

c++程序的(运行系统linux)编译和运行
编写如下的一个程序,并保存为hello.cpp
#include "iostream.h"
int main()
{
cout<<"hello"<<endl;
}
运行:
g++ -o hello hello.cpp
-o 表示将编译后的二进制文件保存到名为hello的文件中

在类中不能对定义的变量进行初始化,如下:
#include <iostream.h>
class base1
{
private:
int a;//default is private,在类中是不能对变量进行初始化的,如这样是错误的int a=0;
public:
int *p1,*p2;
int a1[4];
int b[2][3];
//int *p3=new int[5]//apply space for pointer,delete use delete []p3这一行有错
//p1=a1;this is wrong, can't initialize in defined class
const char xy;//const variable initial outer class
static int i;//静态成员只能在类外初始化;initialize static members outer class and it is needed;
void variable(base1 &y)
{
cout< }
void print();//function declare
}px;//define class object and this is the first method
int base1::i=0;//对静态成员的初始化和必须在类外定义; initialize the static members and they must outer class
void base1::print()/*fuction detail */
{

for(int k=0;k<4;k++)
cout<                                                                                
}//还需建一个构造函数对里面建立的变量进行初始化; need construction a constructor to initialize the variables
int main()
{
base1 px2;//define a class object and this is the second method
for(int i=0;i<2;i++)
for(int k=0;k<3;k++)
{
cout< }
cout<<'
\n';
return 0;
}

exit and return 的区别 exit是一个函数,而return是一个基本c++操作,所以说,exit()中的常量放在括号中,而return则不放在括号中
exit and return'
s difference exit is a function, return is a c++ base operate; so, exit's const will put in brackets and return don't need;

编译和调试c++程序
compile and dubug c++ programme
要安全地装入各种可能的的警告,可以用如下的命令组合这两个选项
load all probable alarms in secure, you can use the follow parameters.
$g++ -Wall -W test.cpp
如果你想关闭所有的警告,可用的如下的参数
if you want to shut down all alarms, you can use following directive and parameter
$g++ -W test.cpp
你最好不要这样做,这个命令可能错过严重的缺陷
you had better donot do it, the directive may lose some serious defects

将警告转换成错识误 convert alarm to error
下面的命令将所有的警告转换成错误
the following directive will convert all alarms to error
$g++ -Wall -Werror test.c++

当编译时无错误,而运行时无结果时,可以用下面的命令试试
compile a program have not error, but the program can't run , you can use the parameters to compile it:
$g++ -Wall -W test.c++

如果你想将你写的程序能够移植到其它的系统上也能运行,你可以用如下的参数
if you want to your promgram can transplant to other systems, you can use following parameters to test it
$g++ -Wtraditional test.cpp traditional(传统的,惯例的)

参数-ansi对实际的编程不是很需要,但对测试人员在测试GNU与标准的符合性,比较测试GNU c++版本和标识GNU C++与其它ANSI版本的差异时,则需要
there is a parameter -ansi, it didn'
t need for protical programmer, but it will be used for tester, who test the associate between GNU and standard or recognise the difference GNU C++ with other ANSI version

-E stop after preprocess, don't compile预处理后停止,不编译
-S stop after compile, don'
t assemble and suffix is .s编译后停止,不汇编,后缀为.s
-c stop after assemble and the suffix is .o, don't generate binary file 汇编后停止,不生成二进制文件,后缀为.o

检查编译过程中间阶段发出的所有命令,用-v选项如下:
check in issued all the directives in compile terms, use -v parameter as below:
g++ -v test.c++

如果你有两个或少数几个C源文件,也可以方便地利用GCC or g++编译、连接并生成可执行文件。例如,假设你有两个源文件 main.c 和 factorial.c 两个源文件,现在要编译生成一个计算阶乘的程序。

  清单 factorial.c
int factorial (int n)
{
if (n <= 1)
return 1;

else
return factorial (n - 1) * n;
}

清单 main.c
#include //我一般喜欢用g++,这里用的就是c++的头 I like use c++, so, it is c++ header in here
#include "factorial.c"

int factorial (int n);

int main (int argc, char **argv)
{
int n;

if (argc < 2) {
printf ("Usage: %s n ", argv [0]);
return -1;
}
else {
n = atoi (argv[1]);
printf ("Factorial of %d is %d. ", n, factorial (n));
}

return 0;
}
利用如下的命令可编译生成可执行文件,并执行程序:
use follow directive can compile and generate a executable file and execute it
$g++ -c factorial.c //the first step
it can generate factorial.o file
$g++ -o linked main.c factorial.o //the second step
$./linked 8 //the third step

如果你想查看main.c 是否正常加载了factorial.c里的文件,你可以通过下面的命令查看源代码
if you want to check in the main.c had load the factorial.c or not ,you can use the follow directive look:
$g++ -E main.c

优化代码,用参数-O或者加上0,1,2,3
reduce compile time and cut some idle code, can use the parameter -O or plus digit 0,1,2,3
编译test.c++并使用一级优化
such as, compile test.c++ and use 1 optimization:
g++ -O1 -o test test.c++
0代表不优化
0 display don'
t optimization
   -O0 不进行优化处理。
  -O 或 -O1 优化生成代码。
  -O2 进一步优化。
  -O3 比 -O2 更进一步优化,包括 inline 函数。
  -shared 生成共享目标文件。通常用在建立共享库时。
  -static 禁止使用共享连接。
  -Wall 生成所有警告信息。
 
              debug (gdb)
参数-quiet可以省略冗余的版本和其它的信息
parameter -quiet can cancel the verbose version and other informations
$gdb -quiet or -silent
如果你想查看gdb的一些常用命令,你可以输入如下的命令:
if you want to look the directories used ordinary, you can type the follow order:
(gdb)help data
如果你想知道如何使用某一个命令,如break命令,你可以输入如下的格式:
if you want to know how to use some directive, such as: break, you can type follow format:
(gdb)help break
如果你想查看代码,可以输入下面的命令
if you want to look the source code, you can input the follow code
(gdb)list
如果你想在代码15行设置断点,可以输入下面的命令
if you want to at line 15 set a breakpoint, you can type follow order:
(gdb)b 15
b is abbreviate of break
现在运行程序,在断点处结束
now, run programme and stop at the breakpoint of line 15
(gdb)run
run single step
(gdb)next
运行后面所有的代码
run the below all codes
(gdb)continue or cont
cont is only a abbreviate of continue cont仅仅是continue的一个缩写

退出,你可以输入的命令
if you want to quit, you can input follow directive
quit or Ctrl-d


here is a simple illustrate:

assume there is a c++ file debug.c++
list debug.c++
      #include
      int main()
      {
       float flo;
       cout<<"please input the temperature"<      cin>>flo;
      float flo1=(flo-32*5.0)/9.0;
       cout<       }
 $g++ -g -o debug debug.++   //running dubug.c++
 $gdb -quiet debug   //invoking gdb debugger
(gdb) ls
Undefined command: "ls".  Try "help".
(gdb) l
1       #include
2       int main()
3       {
4       float flo;
5       cout<<"please input the temperature"<6       cin>>flo;
7       float flo1=(flo-32*5.0)/9.0;
8       cout<9       }
(gdb) b 7
Breakpoint 1 at 0x80486ea: file debug.c++, line 7.
(gdb) run
Starting program: /home/zhang/c++/debug
please input the temperature
12.3
 
Breakpoint 1, main () at debug.c++:7
7       float flo1=(flo-32*5.0)/9.0;
(gdb) ne
Ambiguous command "ne": next, nexti.
(gdb) next
8       cout<(gdb) p flo
$1 = 12.3000002
(gdb) p flo1
$2 = -16.4111118
(gdb) nexti
0x08048703      8       cout<(gdb) next
-16.4111

(gdb) pt flo1
type = float

9       }
(gdb) p $1
$3 = 12.3000002

(gdb) q
The program is running.  Exit anyway? (y or n) y

 
                    c++ programme
 list main.c++:
  #include <iostream.h>
#include "type.h" //need displaytype()
//void displaytype();
int main()
{
displaytype();
return 0;
}

list type.h
void displaytype();

list type.c++
#include <iostream.h>
void displaytype()
{
 int i;
 char c;
 short s;

 bool b;
 long l;
double d;
long double ld;
cout<<"sizeof int == "<<sizeof(i)<<endl;
cout<<"sizeof char == "<<sizeof(c)<<endl;
cout<<"sizeof double == "<<sizeof(d)<<endl;
cout<<"sizeof short == "<<sizeof(s)<<endl;

cout<<"sizeof bool == "<
cout<<"sizeof long == "<<sizeof(l)<<endl;
cout<<"sizeof long double == "<<sizeof(ld)<<endl;
}

type.h 和type.c++的名字可以不同
 type.h and type.c++'s name can different
 运行他们就像如下这样:
 runnint like this:
 $g++ -c type.c++
 $g++ -o main main.c++ type.o
 $./main
 结果如下:
 the result as follow:
sizeof int == 4
sizeof char == 1
sizeof double == 8
sizeof short == 2

sizeof bool ==1

sizeof long == 4
sizeof long double == 12

unsign只能是正数和零
unsign is only positive and 0

全局变量自动初始化为0,而局部变量在赋值时才初始化
grobal variable auto initialize 0, and local variable initialize in assignment
在类中调用一个全局变量,而又有一个同名的局部变量时,此时应在全局变量前加上类名和两个冒号
invoking a grobal variable in class, must add class_name and ::
如果在一个函数中,则不须类名,只需两个冒号就可以了
if in a fuction, don't need class_name, need only two colons
如:
#include
int a;
void display()
{
int a;
cout<<"print the grobal variable"<<::a<}

enum(枚举)
#define red 0
#define yellow 1
#define green 2
#define blue 3
#define orange 4
#define indigo 5
enum(red, yellow,green,blue,orange,indigo)
这个枚举与上面的宏定义是等效的
the enum is equl to the above the #define
 在枚举中,各个符号代表的是数值
in enum, the red is 0, and yellow is 1,and so on
such as:
list bnum.c++
#include
typedef enum{red,yellow,green,blue,orange,indigo}bcolor;
int main()
{
bcolor color=blue;
cout<return 0;
                                                                               
}

the result is 3;

you can also set the value in enum
such as:typedef enum{red,yellow,green,blue=9,orange,indigo}bcolor;

bool variable: true and false
illustrate: bool xy; and the size of bool is 1 byte

如果给一个变量指定为16进制,或者是8进制,输出默认为十进制
if assignment a hex to a value, and the default output value is decima

                 String
list string.c++
#include
#include
const int sizes=128;
void string1(char *s)
{
char *p;
p=new char[sizes];
//strcpy(p,s);//duplicat string s to string p
//cout<<"the size of pointer == "<//cout<<"the length of after copy == "<//cout<<"the duplicate string == "<strncpy可以限制复制的字符数
strncpy limit the copy mount
只将s 的前10个字符复制,后面的抛弃
strncpy(p,s,10);//it only can copy the former
//ten characters and the below will be discard
cout<<"the size of pointer == "<cout<<"the length of after copy == "<cout<<"the duplicate string == "<delete []p;//must detele the apply's space
}
这个文件的调用由先前的main.c++来调用运行,后面的就不特别说明了。具体的调用方法如下:
list main.c++

#include
#include "type.h"
void string1(char *s);
//void displaytype();
int main()
{
displaytype();
char *p1;
p1=new char[35];
cout<<"enter a string and the size less than 34"<cin>>p1;
string1(p1);
delete []p1;
return 0;
}

strncat, strncmp的用法与strncpy相同
strncat, strncmp's use method like strncpy
strcasecmp的作用是不区分大小写
strcasecmp's function is distinguish the bigcase and lowcase
such as:

char *p="hello";
cout<
这输出结果是0
the output result is 0

search string

strstr()函数可以在一个字符中匹配一个子串,返回为字符指针,返回值为匹配串开始到原串的结尾
 strstr() can search a substring in a string, and return a char pointer, which value is start match string and end the string
 
 strstr()通常用于检查文件的扩展名,如strstr(p,".txt")//p是一个字符指针
strstr()used to check the externed name of a file, such as strstr(p,".txt")//p is a char pointer
 
illustrate:
在上面的string.c++中添加如下语句:
append follow sentences in above string.c++ :
//search substring in a string
//the strstr() return a char pointer
//the strstr() usually used to extern name such as .txt and so on
if(strstr(p,"you"))
{
cout<<"there are substring \"you\" in string pointer p"<cout<}

假设输入串为:yuiyouyuuyuii
assume the input string is :yuiyouyuuyuii

这输出结果是:
the output result:
there are substring "you" in string pointer p
youyuuy

strfry()打乱源串的顺序
strfry() chang the source string's sequence
仍然在string.c++中实现,在其中添加如下的代码:

it will practice in string.c++, we append following sentences in it:
//这是一个打乱字符串顺序的函数,strfry()需要包括文件头
//this is a function which change the characters sequences in string
//strfry()  which need includce string.h header file
cout<<"the source string== "<strfry(p);
cout<<"the string after changed == "<
这输出结果是:
the output result:
the source string== todayisago
the string after changed == aoadgstyoi



             convert   string
there are some function can use ,such as atol(ASCII to long) atof(ASCII to float) atoi(ASCII to integer) strtol(string to long) strtod(string to double) strtoul(string to unsigned long) 
所有这些函数都需要头文件stdlib.h
all the functions need the header file stdlib.h

null通常定义为0
null define 0 


the below is string.c++ soure code

#include
#include
#include
const int sizes=128;
void string1(char *s)
{
char *p;
p=new char[sizes];
//strcpy(p,s);//duplicat string s to string p
//cout<<"the size of pointer == "<//cout<<"the length of after copy == "<//cout<<"the duplicate string == "<//limit the copy mount
strncpy(p,s,10);//it only can copy the former
//ten characters and the below will be discard
cout<<"the size of pointer == "<cout<<"the length of after copy == "<cout<<"the duplicate string == "<delete []p;
char *cmp1="hello";
char *cmp2="HEllo";
//strcasecmp don't distinguish the bigcase and lowcase
char *cmp2="HEllo";
//strcasecmp don't distinguish the bigcase and lowcase
cout<<"this is compare result of \"hello and HEllo \"== "<//cout<<"this is compare result of \"hello and HEllo \"== "<//search substring in a string
//the strstr() return a char pointer
//the strstr() usually used to extern name such as .txt and so on
if(strstr(p,"you"))
{
cout<<"there are substring \"you\" in string pointer p"<cout<}
else cout<<"have not match sting found"<//this is a function which change the characters sequences in string
//strfry()  which need includce string.h header file
cout<<"the source string== "<//if run the follow function have some alarms, append the follow sentences
//the reason is didn't declaration
//extern "C"
//{

//char *strfry(char *string);
//}
//note:the strfry() used only in linux and GNU C Standard libary
strfry(p);
cout<<"the string after changed == "<//the are convert string functions
//such as atol(ASCII to long), atoi(integer),strtod(double),strtof,strtoul
//strtoul(string to unsigned long) atof()
char *con;
con=new char[sizes];
double dou;
float fl;
cout<<"please input a ASCII value"<cin>>con;
cout<<"the is the source string"<cout<<" the atof(con) == "<cout<<" the atoi(con) == "<//cout<<" the strtof(con) == "<//cout<<" the strtod(con) == "<//cout<<" the strtol(con) == "<

delete []con;

}


以下是找工作时,考试前必懂的问题

#include
#include
#include
#define Pi 3.1415926
void main()
{
 char  a[4];
 //int *p=(int *)malloc(5*sizeof(int));
 char *p=a;
 printf("%d,%d",sizeof(a),sizeof(*p));
 char ch=128;
 printf("%c\n",ch);
 int x=3;
 printf("%f\n",pow(2,4));//指数函数
 char b[3]="ab";
    printf("%c\n",b[2]);
 int i=2,h=1;
    i+=i+++h;
 printf("%d,%d",h,i);
 short c;
 printf("%d",sizeof(c));

看看自己做的结果,再到机子上实际运行一下,看看是否全做正确了.

                                                                    

阅读(1436) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~