Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building C++ not working in OSX 10.9

Update:

I solved the problem in the end by reinstalling command line tools, as shown in this link. Sorry for the trouble!

Initial question

I have just upgraded to OSX Mavericks and I cannot make the compiler work anymore. I've been using gcc48 from macports with vim/sb2 and everything was working just fine. Now any library that I include gives tons of errors. I am not very experienced and I do not know what to do, or what to search for. I just want it to work like before. Here is what is says when compiling a basic "hello world" program:

In file included from /opt/local/include/gcc48/c++/bits/postypes.h:40:0,
             from /opt/local/include/gcc48/c++/iosfwd:40,
             from /opt/local/include/gcc48/c++/ios:38,
             from /opt/local/include/gcc48/c++/ostream:38,
             from /opt/local/include/gcc48/c++/iostream:39,
             from ceva.cpp:1:
/opt/local/include/gcc48/c++/cwchar:44:19: fatal error: wchar.h: No such file or directory
#include <wchar.h>

If you need any information, tell me and I will provide it to you. Thank you for your patience!

like image 909
vladb Avatar asked Oct 24 '13 20:10

vladb


People also ask

How do I create a .C file on a Mac?

To create a new source file, select "New File" from the "File" menu: Type the source code for a simple C program into the new file. In this example, the program will just print a short message. Then, save the file as "hello.

Does MacOS use C?

Mac OS X comes with C built into it, and Apple has used C while making every aspect of OS X and iOS. Because C is such a popular language, it even forms the basis of many other programming languages, including two big names: C++ and Objective-C.

Do I need gcc on Mac?

Often times, you need c or gcc compiler to compile open source projects in Mac OS X. The problem is Mac OS X doesn't install the gcc compiler by default. In terminal, type “ gcc “, you will get message “command not found”.


1 Answers

[Added 2019 - Mojave/Catalina] XCode 10 has removed the placement of headers in /usr/include, even with the command line tools. If you want headers, and you're on you should be able to install a transitional package, which is available on Mojave. This package is not present on the Catalina. The headers are within the SDK for the appropriate release.

XCode 5/Mavericks have changed the defaults for where headers are located.

On prior versions of Mac OS X / XCode you would have found headers in /usr/include.

When you built gcc on the older release it picked up the headers from that location, and looks for them there now when you try to build code.

The problem is that the files are not there; they're somewhere under /Applications/Xcode.app/Contents/Developer.

You could futz about with a spec file to get it working consistently again, but the sanest thing to do is rebuild gcc.

As has been mentioned as well, you can install the command line tools using xcode-select --install, which will reinstall the developer tools, including the headers in /usr/include.

I tend to reinstall things like gcc when I upgrade my operating system as I encounter other errors due to changes in the environment. This is just a habit I've formed. It may not be the best habit, but it's saved me pain on numerous occasions.

[added 2017] As an addendum, if you install the command line tools, then the headers will be placed in /usr/include as well. The command to install the command line tools is xcode-select --install - this will allow you to use most compilers without having to specify the location of the headers manually.

like image 167
Petesh Avatar answered Oct 26 '22 13:10

Petesh