2010-03-04 150 views
2

我知道C++代码应该由g ++编译和链接,而不是gcc。 但是为什么gcc仍然可以编译C++源代码,尽管源代码中有很多C++关键字。为什么gcc可以编译C++代码但不能链接?

顺便说一下,我发现我甚至可以通过gcc与所有C++代码构建一个共享库。为什么?

回答

9

g ++是gcc,它只是自动链接到标准的C++库。

如果你的G ++代码依赖于标准库(东西在std命名空间),你可以

  1. 使用g ++命令,它的所有自动
  2. 使用GCC命令,并指定C++标准(-lstdc++
2

您可以链接-lstdC++。

+0

不起作用。当我链接 – solotim 2010-03-04 07:07:00

+0

@ solotim时,错误会发生,它确实有效。也许你错了什么?或者,也许你应该编辑你的问题,并指定错误。 – 2010-03-04 07:12:26

4

从GCC手册页:

For any given input file, the file name suffix determines what kind of 
    compilation is done: 

    file.c 
     C source code which must be preprocessed. 

    . 
    . 
    . 

    file.h 
     C, C++, Objective-C or Objective-C++ header file to be turned into 
     a precompiled header. 

    file.cc 
    file.cp 
    file.cxx 
    file.cpp 
    file.CPP 
    file.c++ 
    file.C 
     C++ source code which must be preprocessed. Note that in .cxx, the 
     last two letters must both be literally x. Likewise, .C refers to 
     a literal capital C. 

什么也没有做自动链接到C++标准库。在那时使用g++是最简单的。

+1

如果您要降级,请发表评论。 – Xorlev 2010-03-04 07:11:13

相关问题