C++开发环境搭建

Windows下

MinGW(Minimalist GNU for Windows)

A native Windows port of the GNU Compiler Collection (GCC), with freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality.

gdb


Makefile


参考了 http://www.iteye.com/topic/560534

 

Linux下

os:

执行 gcc -v

guxu@guxu-Lenovo-B460:/$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6.1/lto-wrapper
Target: i686-linux-gnu   #当前使用的gcc是由i686微处理器编写
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++,go
--prefix=/usr         #安装路径
--program-suffix=-4.6
--enable-shared
--enable-linker-build-id
--with-system-zlib
--libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix     #线程类型为posix
--with-gxx-include-dir=/usr/include/c++/4.6
--libdir=/usr/lib
--enable-nls
--with-sysroot=/
--enable-clocale=gnu
--enable-libstdcxx-debug
--enable-libstdcxx-time=yes
--enable-plugin
--enable-objc-gc
--enable-targets=all
--disable-werror
--with-arch-32=i686
--with-tune=generic
--enable-checking=release
--build=i686-linux-gnu
--host=i686-linux-gnu   #主机类型
--target=i686-linux-gnu
Thread model: posix
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

C++ HelloWorld程序Test.cpp

执行gcc Test.cpp -o Test1

guxu@guxu-Lenovo-B460:~/Workspace/C++/Test$ gcc Test.cpp -o Test1
gcc: error trying to exec 'cc1plus': execvp: No such file or directory

执行sudo apt-get install build-essential 后再执行上面gcc命令,失败执行 g++ Test.cpp -o Test1成功


一般来说,编译器通过源文件的后缀名来判断是C程序还是C++程序。在Linux中,C源文件的后缀名为.c(小写),而C++源文件的后缀名为.C(大写)、.cc或.cpp。

gcc可用来编译c和c++程序,但是却不能继续对c++程序进行库链接。g++可以完成对c++程序的编译和链接。

下面来验证一下:

$ ll
Test.cpp
$ gcc -c Test.cpp //利用gcc编译.cpp
$ ll
Test.cpp Test.o
$ gcc -o Test Test.o //gcc链接.o失败
Test.o: In function `main':
Test.cpp:(.text+0x14): undefined reference to `std::cout
'
Test.cpp:(.text+0x19): 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*)'
Test.cpp:(.text+0x21): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
'
Test.cpp:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
Test.o: In function `__static_initialization_and_destruction_0(int, int)
':
Test.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::Init()'
Test.cpp:(.text+0x56): undefined reference to `std::ios_base::Init::~Init()
'
collect2: ld returned 1 exit status
$ ll
Test.cpp Test.o
$ rm Test.o
$ ls
Test.cpp
$ g++ -c Test.cpp //利用g++编译.cpp
$ ls
Test.cpp Test.o
$ g++ -o Test Test.o //g++链接.o成功
$ ls
Test Test.cpp Test.o
$ ./Test //运行可执行程序
Hello Cpp
$ rm Test Test.o
$ ls
Test.cpp
$ g++ -o Test Test.cpp                               //编译+链接
$ ls
Test  Test.cpp

Linux下c/c++编译器将程序编译成可执行文件要经过如下4个步骤:

1.预处理:命令GCC首先调用cpp进行预处理,在预处理过程中,对源代码文件中的文件包含、预编译语句进行分析。

2.编译:调用cc进行编译,这个阶段根据输入文件生成以.o为后缀的目标文件。

3.汇编:汇编过程是针对汇编语言的步骤,调用as进行工作,将.S和.s为后缀的汇编语言文件经过预编译和汇编成以.o为后缀的目标文件。

4.链接:当所有的目标文件都生成之后,调用ld来完成最后的关键性工作,这个阶段就是连接。在连接阶段,所有的目标文件被安排到可执行程序中恰当的位置上,同时,该程序所调用到的库函数也从各自所在的档案库中链接到合适的地方。

 

其他信息参见:http://book.51cto.com/art/200811/96913.htm

 http://www.oschina.net/p/anjuta/

http://www.linuxidc.com/Linux/2010-11/29543p2.htm



posted @ 2012-01-15 23:14  NemoGu  阅读(1576)  评论(0)    收藏  举报