clang vs gcc : 代码出错提示功能对比

今天研究c++ const 的问题,顺便做了一下gcc 与 clang 的代码编译错误提示对比,结论是,clang这个晚辈确实比gcc人性化多了。

参考:

http://www.cnblogs.com/Fancyboy2004/archive/2008/12/23/1360810.html

以下是测试代码:

 

 

Cpp代码
  1. #include <cstdlib>  
  2.   
  3. class Apple  
  4. {  
  5.     public:  
  6.         Apple (){};     /* constructor */  
  7.   
  8.         int GetCount()  /* 非const 成员函数 */  
  9.         {  
  10.             return m_iData;  
  11.         };  
  12.     protected:  
  13.         int m_iData;  
  14. }; /* -----  end of class Apple  ----- */  
  15.   
  16. using namespace std;  
  17.   
  18.   
  19. int main ( int argc, char *argv[] )  
  20. {  
  21.     const Apple apple;  
  22.     apple.GetCount();    /* 错误 */  
  23.   
  24.     return EXIT_SUCCESS;  
  25. }       // ----------  end of function main  ----------   
  26. //save to a.cc  
#include <cstdlib>

class Apple
{
    public:
        Apple (){};     /* constructor */

        int GetCount()  /* 非const 成员函数 */
        {
            return m_iData;
        };
    protected:
        int m_iData;
}; /* -----  end of class Apple  ----- */

using namespace std;


int main ( int argc, char *argv[] )
{
    const Apple apple;
    apple.GetCount();    /* 错误 */

	return EXIT_SUCCESS;
}		// ----------  end of function main  ---------- 
//save to a.cc
 

 

 

 

gcc编译命令:

Shell代码
  1. $ g++ a.cc  
  2. a.cc: In function ‘int main(int, char**)’:  
  3. a.cc:22: error: passing ‘const Apple’ as ‘this’ argument of ‘int Apple::GetCount()’ discards qualifiers  

 

clang:

Shell代码
  1. $ clang++ a.cc  
  2. a.cc:22:5: error: member function 'GetCount' not viable: 'this' argument has  
  3.       type 'const Apple', but function is not marked const  
  4.     apple.GetCount();    /* 错误 */  
  5.     ^~~~~  
  6. a.cc:8:13: note: 'GetCount' declared here  
  7.         int GetCount()  /* 非const 成员函数 */  
  8.             ^  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
从引用和引用[2]来看,出现了类似的错误信息 "/usr/bin/ld: crtbegin.o: No such file: No such file or directory"。这个错误通常表示缺少某个必要的库文件或依赖项。根据引用的描述,可能是由于GCC的安装路径未正确选择引起的。由于之前使用的是GCC 4.9.3编译Clang 9,但现在找不到相应的或更高版本的GCC,所以出现了这个错误。 要解决这个问题,可以尝试以下步骤: 1. 确认你的系统中是否已正确安装了GCC。可以使用命令`gcc -v`或`gcc --version`来检查GCC的版本信息。如果没有安装GCC,可以使用包管理器来安装。例如,对于Ubuntu系统,可以使用以下命令安装GCC:`sudo apt-get install build-essential`。 2. 如果已经安装了GCC,但仍然出现错误,可能是因为GCC的安装路径未正确选择。可以尝试使用命令`update-alternatives --config gcc`来选择正确的GCC版本。在提示中选择正确的版本并确认。 3. 如果以上步骤都没有解决问题,可能需要更新或重新安装Clang。可以使用包管理器来更新或重新安装Clang。例如,对于Ubuntu系统,可以使用以下命令更新或重新安装Clang:`sudo apt-get install --reinstall clang`。 总结一下,当出现类似错误 "/usr/bin/ld: crtbegin.o: No such file: No such file or directory" 时,可以尝试确认是否正确安装了GCC,并选择正确的GCC版本。如果问题仍然存在,可以考虑更新或重新安装Clang。希望这些步骤可以帮助你解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值