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
Related
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 followed this tutorial : http://tilomitra.com/opencv-on-mac-osx/ to compile OpenCV for Mac OSX in order to use it in XCode 3.2.3
I had no error when compiling openCV. But in XCode I get file was built for unsupported file format which is not the architecture being linked (i386) for each dylib and Symbols not found after.
Any clue ?
steps to compile and run c++ opencv 2.4.4 on mac os x lion 10.7.5 with cmake 2.8.10 and xcode 4.6.1
Having the right tools
download opencv-unix from http://sourceforge.net/projects/opencvlibrary/files/ and untar it wherever
download cmake .dmg from http://www.cmake.org/cmake/resources/software.html and install it
i am assuming you have xcode 4.6 on os x lion which includes the ios sdk 6.1
go to xcode preferences to download and install the Command Line Tools so you have g++ etc.
Use cmake to compile opencv
go to the extracted opencv folder
create a build directory
mkdir build
cd build
cmake -D WITH_TBB=OFF -D BUILD_NEW_PYTHON_SUPPORT=OFF -D BUILD_FAT_JAVA_LIB=OFF -D BUILD_TBB=OFF -D BUILD_EXAMPLES=ON -D CMAKE_CXX_COMPILER=g++ CMAKE_CC_COMPILER=gcc -D CMAKE_OSX_ARCHITECTURES=x86_64 -D BUILD_opencv_java=OFF -G "Unix Makefiles" ..
make -j8
sudo make install
from the build folder, go to bin/ and run one of the tests
./opencv_test_stitching
Create your own c++ opencv xcode project
fire up xcode and create a new xcode project
select Command Line Tool for the type of project under os x
open your project's build settings
under Architectures, set Architecture to 64-bit intel. also set Valid Architectures to x86_64
under Build Options, set Compiler for C/C++ to Default Compiler
under Search Paths, set Header Search Paths to /usr/local/include
also under Search Paths, set Library Search Paths to /usr/local/lib
under Apple LLVM compiler 4.2 - Language set C++ Standard Library to libstd++
Add the compiled opencv libraries to your project
go to the Build Phases tab next to Build Settings tab you were in
inside Link Binary With Libraries, click on the + sign and choose Add Other
hit the front slash / on your keyboard and enter /usr/local/lib
hit enter and select the libraries you want to use in your project
make sure you always select libopencv_core.2.4.4.dylib
hit enter and you will see the selected dylibs under your project
write some code
first lets organize the files, right click on your project blueprint icon and select New Group
name the new group opencv or whatever
drag the dylibs and drop them in that group
open main.cpp
copy code from any of the sample tests that came with opencv and paste it here
make sure all the required dylibs are added, for example, if you copied the opencv_test_stitching.cpp code into main.cpp, you will need to add the following libraries in the previous steps
libopencv_core.2.4.4.dylib
libopencv_highgui.2.4.4.dylib
libopencv_stitching.2.4.4.dylib
Cheers.
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 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'
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.