I want to compile a c file in OSX mountain lion. In Xcode 4.4, Preferences -> Downloads I can install command line tools to do that. However on that pane it advises me that I can use xcrun instead:
Before installing, note that from within Terminal you can use the XCRUN tool to launch compilers and other tools embedded within the Xcode application. Use the XCODE-SELECT tool to define which version of Xcode is active. Type "man xcrun" from within Terminal to find out more.
I'd be happy to do it that way, but I'm having trouble getting it to find stdio.h.
$ xcrun gcc hello.c
hello.c:1:19: error: stdio.h: No such file or directory
I definitely have this file on my system after a standard install of Xcode via the App Store:
/Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h
I tried specifying the SDK but got the same error:
$ xcrun -sdk /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk gcc hello.c
Some googling led me to try running xcode-select before the commands above, but no luck. I only have the one version of Xcode installed anyway.
$ sudo xcode-select -switch /Applications/Xcode.app
How can I get it to find the headers?
Ultimately I gave up and installed command line tools, but it would be nice to know how to do it with the built-in Xcode tools.
Note: here is the file I was trying to compile:
#include <stdio.h>
int main() {
printf("hello world!\n");
return 0;
}
You will have to specify the non-standard sysroot for the compiler.
Using clang/llvm (which is the new standard) this would do the trick:
xcrun clang --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -o hello hello.c
If you want to stick with gcc just replace "clang" with "gcc".
Probably not of any direct help with this, but you can set up aliases to commonly used xcrun commands so that when other processes call gcc, make, gdb, git and so on that the Xcode versions get used:
alias gcc='xcrun gcc'
You can put the alias into the .bashrc file if you like so that it persists, and source from .bash_profile as necessary.
The advantage of all this is that you don't need to install the Xcode Command Line Tools and can also avoid using package managers, saving space, reducing complexity and taking advantage of Xcode automatically updating these tools for you.
Using xcrun --sysroot was not working for me using 10.8.
Looking in to the clang documentation I found that -isysroot is the option to use in this case.
using:
xcrun gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
or:
xcrun clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
For me it was useful to create the following aliases:
alias xcrungcc='xcrun gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk'
alias xcrunclang='xcrun clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk'
Related
It seems like my gcc is broken.
When I try to install package from PyPI I have this error:
./uwsgi.h:165:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
In file included from core/protocol.c:1:
./uwsgi.h:165:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
XCode command line tools are installed:
➜ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
And gcc from brew installed:
➜ brew install gcc
Warning: gcc 8.2.0 is already installed and up-to-date
To reinstall 8.2.0, run `brew reinstall gcc`
When I try to recognize which gcc is used I have /usr/bin/gcc. I think it's XCode version.
But there is no gcc binary in brew:
➜ ls /usr/local/bin/gcc-*
/usr/local/bin/gcc-8 /usr/local/bin/gcc-ar-8 /usr/local/bin/gcc-nm-8 /usr/local/bin/gcc-ranlib-8
What can I do in this situation? How can I fix gcc?
The correct answer was in this thread: https://stackoverflow.com/a/52530212/1377912
The new Xcode doesn't provide headers by default. You should install it manually:
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
As I wrote this answer, Mojave is deprecated and now is the age of Big Sur! but this problem still exists!
I don't know that following command will work on Mojave or not, but is working on Big Sur! if you still have this problem, just simply examine that.
Just set CFLAGS(for C compilers) or CXXFLAGS (for C++ compilers ) something like this:
export CFLAGS="-isysroot $(xcrun --sdk macosx --show-sdk-path) -I/usr/include -L/usr/lib -I$(xcrun --sdk macosx --show-sdk-path)/usr/include"
or
export CXXFLAGS="-isysroot $(xcrun --sdk macosx --show-sdk-path) -I/usr/include -L/usr/lib -I$(xcrun --sdk macosx --show-sdk-path)/usr/include"
It probably removes your fatal error: 'stdio.h' file not found error. If this error been removed, it might that you face to new problems, don't count them as this problem and loose your success by manipulating this! In this situation just be care about your new problems and try to fix them, one by one!
I hope this answer be useful for you
Maybe it's a stupid answer but have you tried to create and compile a very simple c file? However in my mojave system i've tried the gcc -v command and i have this output:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-
dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin18.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I'm on a mac and I used homebrew to install gmp.
Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx
In file included from main.cpp:2:
./gcjlib.hpp:4:10: fatal error: 'gmpxx.h' file not found
#include <gmpxx.h>
^
1 error generated.
So then I explicitly told g++ to use /usr/local/include
Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include
ld: library not found for -lgmp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So then I explicitly told g++ to use /usr/local/lib
Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include -L/usr/local/lib
Kyumins-iMac:gcjlib math4tots$ ./a.out
sum is -4444
absolute value is 4444
So the only issue seems to be that g++ fails to acknowledge /usr/local.
But it is tedious to type all this out all the time, especially when I'm just writing small single file programs.
Is there a way for me to get g++ to acknowledge the stuff in /usr/local by default? Is there a standard way homebrew users do this?
I'm on OS X 10.9.3 with Xcode 5.1.1 in case it is relevant.
I also use Homebrew and had a similar problem on Mac OSX Maverick 10.9.5 and Xcode 6.0.1, but it was solved by running:
xcode-select --install
Note that it doesn't work without the double hyphens given by the previous answer. This installs the command-line tools that also create /usr/lib/ and /usr/include/. I don't know why Homebrew doesn't automatically check this upon installation, since it does check for Xcode...
If you want to check exactly what folders your compiler is looking through for header files you can write:
cpp -v
A workaround would be to:
export C_INCLUDE_PATH=/usr/local/include
export CPLUS_INCLUDE_PATH=/usr/local/include
At least this tricked the pre-processor to behave here :)
Try running xcode-select --install
At least on Mavericks, I've found that if I install the Xcode application without installing the command-line tools, then the tools are sort of available, but normal unix-ey builds don't work correctly. One symptom is that /usr/local/include is not on the include search path. The command-line tools seem to resolve this issue.
I have Yosemite 10.10.5 and running xcode-select --install didn't fix the problem for me. The command returned with xcode-select: error: command line tools are already installed, use "Software Update" to install updates.
When I ran xcode-select -p, it showed /Applications/Xcode.app/Contents/Developer. I ended up deleting Xcode from the Applications directory, which resulted in xcode-select -p returning /Library/Developer/CommandLineTools. This fixed compiler error for me.
That was helpful for me:
Use the latest version. 1.0.2o_1 just a current build.
brew install openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/include/openssl /usr/local/include/openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/lib /usr/local/lib/openssl
There are a few questions around this topic with answers that suggest putting a symlink in /usr/local/include. However I'm running macOS Monterey 12.3 (on an M1 MacBook) and that directory doesn't exist.
I had installed the Xcode command line tools by downloading the package from Apple, so xcode-select --install just tells me it's already installed and doesn't create any directories.
I ran cpp -v to see which directories are searched for #include <...>:
/Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
/Library/Developer/CommandLineTools/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
I picked /Library/Developer/CommandLineTools/usr/include for the symlink. In that directory, I ran the following command (note the new location for Homebrew installations, under /opt/homebrew - some old answers are also out of date on this point):
sudo ln -s /opt/homebrew/opt/openssl#3/include/openssl .
clang was then able to find the OpenSSL files.
apk add --no-cache build-base
it works fine !!!!
go build -tags musl -o main main.go
I installed the newest version of Xcode.
Neither of the executables gcc, cc, or clang exist in the terminal.
How can I find the C compiler in the terminal that Xcode uses?
Type in Terminal:
xcodebuild -find make
xcodebuild -find gcc
xcodebuild -find g++
xcodebuild -find clang
xcodebuild -find clang++
Each command prints the full path to the corresponding tool. They are the ones that are used by Xcode.
You can also install Xcode Command Line Tools which will place tools and libraries under standard Unix locations (/usr/bin, /usr/include, etc.) if you want to be able to just type make or gcc without any paths. Note that these tools will be unrelated to the ones that Xcode application uses and may be of different versions.
Such commands have been made into a separate "command-line tools" package as of Xcode 4.3 or thereabouts.
They have been moved into the Xcode.app bundle as part of making Xcode a more standard App Store install. The separate CLI tools is also useful for projects like Homebrew which have no need for the full Xcode download.
To install (from here):
Open Xcode
Go to Preferences
Select Downloads and click Install for Command Line Tools (about 171 megabyte)
It seems to have moved:
xcodebuild -find clang
Output:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
It's in Xcode app bundle:
/Applications/Xcode.app/Contents/Developer/usr/bin/
If executables gcc, cc, and clang are available on the command line, you can get information about the installation and the directory by the -v tag.
gcc -v
cc -v
clang -v
The issue I'm having is that gcc (and family) don't appear to be properly setup. I have a 10.7.4 machine that I just installed Xcode on (from the app store). I've done no prior development on this machine.
Working w/in Xcode seems to work fine. I can build and compile no problem. However, trying to execute gcc command line fails.
First, I gcc wasn't on my path ... no big deal. I located it and ran as:
/Applications/Xcode.app/Contents/Developer/usr/bin/gcc -dynamiclib -fno-common -o s.dylib s.c
(I'm working on a lib w/some functions...). Anyways, it fails.
s.c:1:19: error: stdio.h: No such file or directory
s.c:2:20: error: stdlib.h: No such file or directory
s.c:3:20: error: string.h: No such file or directory
Surprise! hah, well I searched my machine for stdio.h and I can't find it anywhere. Since I've been out of the OSX game for a bit, I'm assuming I'm missing something -
Basically I want to be able to continue using Xcode but I want to be able to build C/C++/etc on the command line with all the dependencies (.h) in the correct place.
Any thoughts?
There are two main ways to run the compiler from the command line: the Command Line Tools package, and xcrun.
xcrun is particularly good if you just need this occasionally. Just stick "xcrun" at the start, like you'd do with sudo:
xcrun gcc -dynamiclib -fno-common -o s.dylib s.c
This will find the correct version of gcc and set the needed directories, etc. You can specify a specific SDK with --sdk.
If you do this a lot, download and install the Command Line Tools package (Xcode>Open Developer Tool>More Tools...; it also may be available in Preferences>Downloads). This installs a full copy of everything in /usr.
Probably xcrun is not enough if you are using 10.8.
Looking in to the clang documentation I found that you need to include the system root because you do not have your libraries in the standard place but inside Xcode.
using:
xcrun gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
or:
xcrun clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
I've recently installed gcc 4.6.3 on my MAC (with OSX Lion, XCode 4.3, gcc 4.2.1). I've made a folder named my_gcc463, which contains all files after compiling gcc with make command, etc.
I've also installed MacPorts to select between all versions of gcc that I have. The problem is when I run this command:
port select --list gcc
I can't see the new gcc 4.6.3 in the list so that I can select that. Also when I run this command:
sudo ln -s -f ~/my_gcc463/gcc-4.6.3 /usr/bin/gcc
to link gcc to the one I want, It's not working. For example after creating that symlink, when I run:
gcc -v
It doesn't give me the version. What should I do to make my new gcc appear in the gcc list? Did I do anything wrong? Should I install it in other way?
Thank you in advance for helping me. :)
port select is a tool to maintain the gcc installations from MacPorts and, if available, the versions provided by Apple. Although you could hack it up by dropping your own descriptions into /opt/local/etc/select/gcc, I would recommend a different approach for this.
To achieve what you want you should modify your PATH environment variable to include the new path to your gcc version at the front, such that gcc resolved to the correct absolute path.
Assuming your new gcc binary is at ~/my_gcc463/gcc, you should add the following to your .profile/.bash_profile or .bashrc:
export PATH=~/my_gcc463:$PATH
After setting this up, run gcc -v in a new terminal window to check if it is working as expected.