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

StackOverflow https://stackoverflow.com/questions/23534362

  •  17-07-2023
  •  | 
  •  

Question

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

Here's my code,

MergeSort.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:

#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)
Was it helpful?

Solution

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

OTHER TIPS

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

You're compiling an C header file via a CXX compiler. It DOES NOT mean you CAN NOT compile the header files. you CAN compile the header file if you choose the right compiler for the right files. In simple words:

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 .h to .hpp will do the trick.

In the very rare case that you actually want to compile/parse/tokenise a C++ header file, you can pass the -x c++-header command line flag to clang++, like so:

clang++ -x c++-header header.h

There's a number of rare but valid reasons why you'd want to compile or at least tokenise/parse header files using clang, and this will silence the deprecation warning.

This info can be found via the clang++ --help docs.

$ clang++ --help
#
# ... other help details
#
  -x <language>           Treat subsequent input files as having type <language>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top