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.
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.
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.
Related
I've been trying to run simple c++ program
#include<iostream.h>
void main(){
cout<<"helloworld";
}
and the error I get is :
In file included from d:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\postypes.h:40:0,
from d:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iosfwd:40,
from d:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ios:38,
from d:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:38,
from d:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:39,
from my.cpp:1:
d:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cwchar:44:19: fatal error: wchar.h: No such file or directory
#include <wchar.h>
^
compilation terminated."
anyone know how to fix it ,I'm using the mingw to compile it.
Probably your MinGW is broken. Also, GCC 6.3.0 is very old.
Try a more recent MinGW or even better MinGW-w64.
A standalone build is available from http://winlibs.com/ with GCC 10.2.0 (both for Windows 32-bit and 64-bit).
System OSX 10.14.6, macbook pro
Trying to compile this simple C++ program:
#include <iostream>
int main() {
sdt::cout << "hello world" << std::endl;
return 0;
}
Have gcc 9 installed via homebrew, as well as XCode command line tools.
Both of the following work
c++ main.cpp
g++-9 main.cpp
Trying out the latest Intel Parallel Studio, the following fails:
> icpc main.cpp
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/iosfwd(90),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios(215),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream(38),
from main.cpp(1):
/Library/Developer/CommandLineTools/usr/include/c++/v1/wchar.h(119): catastrophic error: cannot open source file "wchar.h"
#include_next <wchar.h>
^
compilation aborted for main.cpp (code 4)
Can someone tell me what I am missing here?
EDIT RESPONSE TO COMMENT ABOUT find
searching from / for wchar.h yields many wchar.h files, including the one from the error:
./usr/local/Cellar/gcc/9.2.0_3/include/c++/9.2.0/tr1/wchar.h
./Library/Developer/CommandLineTools/usr/include/c++/v1/wchar.h
./Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/DriverKit19.0.sdk/System/DriverKit/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/wchar.h
./opt/intel/intelpython3/include/c++/v1/support/solaris/wchar.h
./opt/intel/intelpython3/include/c++/v1/wchar.h
./opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/support/solaris/wchar.h
./opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/DriverKit19.0.sdk/System/DriverKit/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/wchar.h
./dev/fd/3/opt/intel/intelpython3/include/c++/v1/support/solaris/wchar.h
./dev/fd/3/opt/intel/intelpython3/include/c++/v1/wchar.h
./dev/fd/3/opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/support/solaris/wchar.h
./dev/fd/3/opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/wchar.h
As per the comments, this solved the problem:
icpc main.cpp -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk
Intel C++ compiler on macOS is highly sensitive to the version of Xcode being used. For example, I receive the same errors as above when using icpc 2020 Initial Release and Xcode 11.3.1, but the code compiles and executes just fine with "icpc main.cpp" when using Xcode 11.2.1.
This has been the situation with Intel C++ compilers for some time on macOS... check your Xcode version and try again.
In Eclipse I am using mingw-w64 V5.3.0 as the compiler and I have enabled C++11 (which was the solution in the following related post here and here). My compilation command looks as follows (from eclipse console window):
g++ -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0 -o "src\Launch.o" "..\src\Launch.cpp"
The simple code I am attempting to run is as follows:
#include <stdio.h>
#include <iostream>
#include <string>
int main()
{
std::string test = std::to_string(0);
std::cout <<"HI" << std::endl;
return 0;
}
The program compiles but for an error stated in the question. Running it without the to_string line works fine. There have been suggestions back in 2012 that MingW has a problem with to_string(), but was resolved in later versions has shown here.
Screen shot below:
And the console output is as follows:
The following is the error I receive when running the code from the .exe directly:
As mentioned in the comments, the issue is a linking issue, however it is linking correctly to iostream which is in the same directory as string.
Eclipse was looking in System32 for the library and driver files, despite PATH and Eclipse pointing to the MingW64 compiler on the computer. Eclipse is also showing it is linked to the MingW64 libraries correctly as the path to the headers when right-clicking and opening deceleration is shown to be correct. Why then it looks in System32 for the library at run-time I don't understand.
The problem was 'solved' by copying the entire MingW64 compiler driver folder into the System32 folder on Windows.
I was working a project in C++ and all of the sudden my Mac said that Xcode was finished updating, so I just exited out of the popup and didn't think anything of it. I went back to my project and tried to run my makefile and it said I need to be root to accept Xcode's terms. So, I just booted up Xcode, accepted the terms, and quit. Now, when I work on my project in Vim, I am getting all kinds of errors. E.g.
#include<iostream> // 'iostream' file not found
#include<string>
int main()
{
std::cout << "hello" << std::endl; // use of undeclared identifier 'std'
return 0;
}
I have no idea what has happened. Can anybody help me?
I use g++ to compile and this is the output of g++ --version:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx- include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
My Xcode version is now 6.0.1.
All this being said, I still seem to be able to compile my code. I am just getting all types of errors in Vim, related somehow to this Xcode update.
I use YouCompleteMe for error highlighting.
I believe I have found the solution. I am assuming that when Xcode updated, its folder structure changed.
I fixed this issue by changing my .ycm_extra_conf.py. I changed the flag line
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1'
to
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1',
My profile is updated to point to what i think is the right location:
PATH=${PATH}:/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}
Compilers are found
[11:39:32] ~: $ which g++
/Applications/Xcode.app/Contents/Developer/usr/bin//g++
[11:39:35] ~: $ which gcc
/Applications/Xcode.app/Contents/Developer/usr/bin//gcc
Hello world looks as follows
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Fails to execute both from command line and TextMate with
[11:40:15] Desktop: $ g++ untitled.cpp
untitled.cpp:1:20: error: iostream: No such file or directory
untitled.cpp: In function ‘int main()’:
untitled.cpp:6: error: ‘cout’ was not declared in this scope
What else should i have in my path and am missing please?
The answer is, as always, obvious
Install Xcode Command Line Tools
Check your /usr/bin and find there:
Try again .. Everything works automagically
On a side note, it is odd that upgrade from OSX10.7 to OSX10.8 removed these links
This answer is for GCC, but other non-vcc compilers may be the same. When I used GCC iostream was non existent, however iostream.h was, I suggest you try iostream.h and if you run into this problem again with STL headers add the .h.