Thijmen
Thijmen

Reputation: 458

C++ header file 'wchar.h' not found using g++ (MacOS)

Disclaimer - I am completely new to C++ and the way this language works regarding compiling / linking. Using MacOS Mojave.

For a school course we are obliged to use the g++ compiler to compile our c++ projects. G++ seems to be successfully installed; g++ -v results in the following output:

COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin17.5.0/8.1.0/lto-wrapper
Target: x86_64-apple-darwin17.5.0
Configured with: ../gcc-8.1.0/configure --enable-languages=c++,fortran
Thread model: posix
gcc versie 8.1.0 (GCC) 

I created my first "hello world" program:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

When I tried to compile this code (using 'g++ file.cpp), the following terminal output prompted:

In file included from /usr/local/include/c++/8.1.0/bits/postypes.h:40,
                 from /usr/local/include/c++/8.1.0/iosfwd:40,
                 from /usr/local/include/c++/8.1.0/ios:38,
                 from /usr/local/include/c++/8.1.0/ostream:38,
                 from /usr/local/include/c++/8.1.0/iostream:39,
                 from ex1.cpp:1:
/usr/local/include/c++/8.1.0/cwchar:44:10: fatale fout: wchar.h: No such file or directory
 #include <wchar.h>
          ^~~~~~~~~
compilation ended.

The whcar.h file is indeed not present in the above folder. After some digging, I found the whcar file in the following folder: /Library/Developer/CommandLineTools/usr/include/c++/v1

Unfortunately I am a bit lost and don't really know what I am talking about, consindering everything is really new for me.

It is greatly appreciated if anyone can guide me in the right direction.

Kind regards,

Thijmen.

Upvotes: 5

Views: 4950

Answers (3)

CapSel
CapSel

Reputation: 48

I've been experiencing similar behavior recently. It's about missing <wchar.h> or missing <array>, or some other mysterious problem.

I'm using CMakeFiles.txt for my project configuration. This answer is more of the hint then actual solution, and only for CMake users.

Running cmake itself does not report an error for me but compilation (ninja,gnu make) itself does.

I found solutions to each problem on stackoverflow. Each required some change outside my project - like running some xcode command, adding a flag. They all worked "temporarily" - until I did something... like removing build directory, etc. I haven't really focused on what I did that broke my project again.

Every time I get such compilation error running cmake command again fixes it.

So in conclusion - when you get this kind of error on compilation and you're using CMake try to run CMake again before you start applying other solutions you find on SO or anywhere.

Upvotes: 0

Shiyu  Xia
Shiyu Xia

Reputation: 1

I just found out the easiest way of this mess is just download the missing header file to your compiler folder. It will fix everything.

Upvotes: 0

Kevlar
Kevlar

Reputation: 396

For me, reinstalling Xcode Command Line Tools and g++ solved the same issue:

xcode-select --install
brew reinstall gcc

The last line is assuming you have installed homebrew.

Upvotes: 3

Related Questions