gcc使用--编译c++程序 - webdancer's Blog

gcc使用--编译c++程序

webdancer posted @ 2010年9月10日 11:18 in 工具 with tags os GCC , 1699 阅读

    gcc的c++编译器可以产生可执行的目标代码,而且其作为补充加入到gcc中的。直接使用gcc命令來编译c++程序,会出现错误。例如 程序1 exam.cc:

#include<iostream>
int main(int argc,char * argv[]){
	std::cout<<"hello world"<<std::endl;
	return 0;
}

命令1: gcc exam.cc (错误) 。程序编译错误,“ld returned 1 exit status“,无法编译通过。

命令2: gcc -lstdc++ exam.cc 。程序编译通过。-l:链接库文件。

命令3: g++ exam.cc 。程序编译通过。

g++ is a program that calls GCC and treats .c, .h and .i files as C++ source files instead of C source files unless -x is used, and automatically specifies linking against the C++ library.

(g++ 会自动调用GCC,而且会将.c,.h,.i作为c++源文件而不是c的源文件,除非-x 选项被使用,而且自动链接c++库.)。也就是将语言默认为c++了。

后缀:

头文件:C++文件的头文件较多,可以使用.hh, .hpp, .H, 或者.tcc。

源文件: .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx。

汇编语言文件:.ii。

注意:

库文件:c++的库文件名为stdc++.a,包含几乎所有的c++例程。

其他的命令与前面GCC的简单使用的差不多。所以就不说了。当然,编译程序肯定会遇到各种的问题,一定要注意查资料。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee