首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >警告:在C++模式下,将“c-头”输入处理为“c++-头”,此行为是不可取的。

警告:在C++模式下,将“c-头”输入处理为“c++-头”,此行为是不可取的。
EN

Stack Overflow用户
提问于 2014-05-08 06:31:08
回答 3查看 35K关注 0票数 35

这个clang++错误消息意味着什么,为什么我要得到它?我似乎在网上找不到任何关于它的东西..。

clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated

这是我的密码

MergeSort.h

代码语言:javascript
复制
#ifndef __MERGESORT_H__
#define __MERGESORT_H__

#include <iostream>
using namespace std;

class MergeSort;

class MergeSort
{
    public:

    MergeSort();
    MergeSort(const int* list, int length);
    ~MergeSort();

    friend ostream& operator<< (ostream& os, const MergeSort& ms);

    private:

    int* list;
    int len;
};

ostream& operator<< (ostream& os, const MergeSort& ms);
#endif

MergeSort.cpp

代码语言:javascript
复制
#include <iostream>
#include "MergeSort.h"

using namespace std;

MergeSort::MergeSort()
{
    list = new int[1];
    list[0] = 0;
    len = 0;
}

MergeSort::MergeSort (const int* t, int length)
{
    if (t) {
        len = length;
        list = new int[len];
        for (int i = 0; i < length; i++)
            list[i] = t[i];
    }
    else {
        list = new int[1];
        list[0] = 0;
        len = 0;
    }
}

MergeSort::~MergeSort()
{
    delete[] list;
}

ostream& operator<<(ostream& os, const MergeSort& ms)
{
    for (int i = 0; i < ms.len; i++)
        os << ms.list[i];
    return os;
}


int
main()
{
    int list[] = {1,2,3,4};
    int list_len = sizeof(list)/sizeof(int);
    MergeSort ms = MergeSort(list, list_len);
    cout << ms << endl;
    cout << "hello world" << endl;
}

以及产出:

代码语言:javascript
复制
[gyeh@gyeh mergesort]$ clang++ -c -g -Wall MergeSort.cpp MergeSort.h
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated
[gyeh@gyeh mergesort]$ clang++ MergeSort.o -o MergeSort
[gyeh@gyeh mergesort]$ valgrind --leak-check=yes ./MergeSort
==25774== Memcheck, a memory error detector
==25774== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==25774== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==25774== Command: ./MergeSort
==25774== 
1234
hello world
==25774== 
==25774== HEAP SUMMARY:
==25774==     in use at exit: 0 bytes in 0 blocks
==25774==   total heap usage: 1 allocs, 1 frees, 16 bytes allocated
==25774== 
==25774== All heap blocks were freed -- no leaks are possible
==25774== 
==25774== For counts of detected and suppressed errors, rerun with: -v
==25774== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-08 06:33:46

不应只编译源文件,而不应编译头文件。

票数 44
EN

Stack Overflow用户

发布于 2016-04-12 16:14:34

来自@JoachimPileborg的启发。此错误消息的实际含义是:

您正在通过C编译器编译CXX头文件。DOES NOT意味着您可以编译头文件。如果为正确的文件选择正确的编译器,则CAN编译头文件。简单地说:

代码语言:javascript
复制
clang   with *.h    works
clang   with *.hpp  not works
clang++ with *.h    not works
clang++ with *.hpp  works

因此,要解决编译头文件的问题,例如预编译头,只需将后缀从.h更改为.hpp即可。

票数 17
EN

Stack Overflow用户

发布于 2021-09-16 21:44:21

在非常罕见的情况下,您实际上希望编译/解析/tokenise一个C++头文件,您可以将-x c++-header命令行标志传递给clang++,如下所示:

代码语言:javascript
复制
clang++ -x c++-header header.h

要使用clang编译或至少解析tokenise/解析头文件,有许多罕见但有效的原因,这将使弃用警告保持沉默。

这个信息可以通过clang++ --help文档找到。

代码语言:javascript
复制
$ clang++ --help
#
# ... other help details
#
  -x <language>           Treat subsequent input files as having type <language>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23534362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档