警告:在 C++ 模式下将“c-header”输入视为“c++-header”,此行为已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23534362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:28:49  来源:igfitidea点击:

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

c++clang

提问by antimatter

What does this clang++ error message mean, and why am I getting it? I can't seem to find anythingon the internet about it...
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated

这个 clang++ 错误消息是什么意思,为什么我会得到它?我似乎无法在互联网上找到有关它的任何信息...
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated

Here's my code,

这是我的代码,

MergeSort.h:

合并排序.h:

#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:

合并排序.cpp:

#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;
}

And the output:

和输出:

[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)

回答by Some programmer dude

You should not compile the header files, only the source files.

你不应该编译头文件,只编译源文件。

回答by jayatubi

Inspire from @JoachimPileborg. The actually meaning of this error message tells is:

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

You're compiling an Cheader file via a CXXcompiler. It DOES NOTmean you CAN NOTcompile the headerfiles. you CANcompile the headerfile if you choose the right compiler for the right files. In simple words:

您正在C通过CXX编译器编译头文件。这DOES NOT意味着您CAN NOT编译文件。你CAN编译的文件,如果你选择了正确的文件正确的编译器。简单来说:

clang   with *.h    works
clang   with *.hpp  not works
clang++ with *.h    not works
clang++ with *.hpp  works

So to fix the issue about compiling the header files, for example the pre-compile headers, just change the suffix from .hto .hppwill do the trick.

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