I am currently running a MacBook Pro with Mac OS X Version 10.5.8. I downloaded XCode version 2.5 and installed it.
Further, I added /XCode2.5/usr/bin to my PATH.
Here is hello.cc program:
#include <iostream>
int main(void)
{
std::cout << "hello, world" << std:endl;
}
Here is what happens:
$> g++ hello.cc
hello.cc: In function ‘int main()’:
hello.cc:5: error: ‘cout’ is not a member of ‘std’
hello.cc:5: error: ‘endl’ is not a member of ‘std’
Is setting the PATH not sufficient to run the gcc utilities from the command line on a Mac?
Thanks,
Charlie
There is a typo in the code as you've represented it here:
std::cout << "hello, world" << std:endl;
^
|
std::endl
However, once I fixed that it seemed to compile and run fine (g++ 4.2.1 installed with XCode on OS X 10.6.2)
This works:
#include <iostream>
using namespace std;
int main()
{
cout << "hello, world" << endl;
return 0;
}
It has been awhile -- as I remember the installation from the Mac OS install DVD (10.4?, 10.5?), two versions of gcc were provided, one for use with XCode, the other for use from the command line. The version for use from the command line installs to /usr/bin. I don't have /XCode2.5/usr/bin on my PATH, and am able to use gcc, etc. I think that you probably want a different download. The version of gcc that is installed to /usr/bin will install libraries in locations that are automatically searched. No modification to PATH or other settings is necessary.
Alternatively, it is easy to install more recent versions of gcc using MacPorts. These are installed in /opt/local/bin, and the PATH must be modified. The MacPorts versions use modified names so that they won't conflict with the standard Apple-supplied version.
Well after some searching, I found a link to http://connect.apple.com as opposed to (developer.apple.com).
The former site still had a link to XCode 3.1.4 under "Download > Development Tools"
Once I downloaded and installed that (and changed my PATH back to the default), then g++ and gcc work fine.
Thanks all for the quick advice.
Related
I'm working with a very peculiar environment: SH4 architecture, RedHat Linux, kernel 2.4.5.
I'm trying to write a graphic application using Xlib that can run in this environment.
I experimented long time ago with cross-compiling for SH4 architecture and it worked OK. However I never had to compile for a specific outdated kernel.
Set up a VM with linux mint, installed g++, cmake,
#include <iostream>
using namespace std;
int main()
{
//print output
cout << "hello" << endl;
return 0;
}
I run the compiler
g++ hello.cpp
And get a binary file "a.out".
I can execute the binary by typing "./a.out". It prints "hello".
However when I transfer "a.out" to the SH4 machine via FTP and try to run it I get:
bash: a.out: cannot execute binary file
This leads me to believe that the target computer doesn't understand the compiled binary.
UPDATE:
I've written a detailed tutorial that incorporates the top two answers on this question: http://blog.johannesmp.com/2015/09/01/installing-clang-on-windows-pt1/
TL;DR
On Windows, Given the following program:
#include <iostream>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
for(auto el : arr)
{
std::cout << el << std::endl;
}
return 0;
}
I want to be able to do the following:
clang++ hello.cpp -o hello.exe -std=c++14
And get a 64 bit executable that just works. I don't want to have to append a ton of -I includes to tell clang where to find iostream or other standard c++ headers; I don't want to have to link in multiple steps.
I don't care so much about performance, efficiency, what linker is used, etc. I just want to be able to have clang/gcc/whatever set up correctly so that I can use a single, quick and dirty, console command like the one above.
What do I need to install for that to just work?
The Problem
As a predominately mac/linux user I'm used to being able to just use a package manager to install the latest version of clang, which just works.
I'm now trying to set up clang/gnu compiler on windows and it seems to be far more difficult, If only because there is little to no straightforward documentation out there (that I've been able to find)
I've tried to follow this tutorial: https://yongweiwu.wordpress.com/2014/12/24/installing-clang-3-5-for-windows - and was able to use it to get clang to build and link (using gcc 4.8.2), but the resulting binaries were 32 bit.
I've tried installing the latest prebuilt binaries of clang (3.6.2) and the 64 bit version of mingw-w64 (4.9.3 for 64 bit with posix and sjlj for exceptions), and am getting:
hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Which seems to indicate that clang is not seeing gcc's files. It seems that some versions of LLVM/clang are looking for only certain versions of gcc, but that doesn't seem to be documented anywhere?
Similarly someone mentioned to me on the LLVM IRC that you need to modify clang's driver to look for gcc in certain paths?
What I'm looking for
I'm fine with building LLVM/Clang from source if necessary, but I'm really just looking for clear, step-by-step instructions that allow me to use clang++ as easily as I'm able to do with mac/linux
Something like:
Build this version of LLVM/Clang and place it in that directory
Download and install this version of cygwin (or mingw32 or mingw-w64) and install these packages.
etc..
Try installing MSYS2 and then installing the mingw-w64-x86_64-clang (binary) package:
pacman -S mingw-w64-x86_64-clang
It is a bit old at the moment (3.6.2), but it might be good enough for you. Then when you run the Win64 shell provided by MSYS2, clang will be on your path.
If it's not good enough, I have recently been building a 64-bit version of clang with MSYS2 and using it to compile native 64-bit Windows executables. My process was something like:
Use pacman to install base-devel, mingw-w64-x86_64-ninja, mingw-x86_64-cmake and perhaps some other packages that are needed by the build process.
Grab my PKGBUILD script for clang and the files in the same directory. This is based on the mingw-w64-clang-svn package from MSYS2, which is largely the work of Martell Malone. You can find him on the MSYS2 IRC channel and ask him more about it.
In a Win64, shell, go to the directory with my PKGDUILD, run export MINGW_INSTALLS=mingw64 (so you are only compiling the 64-bit version), and then run makepkg-mingw.
It is unlikely you will get everything right on the first try, and some files might need to be edited. Clang may have changed since the last time I did this and some patches might not apply cleanly.
if you use the upcoming clang 3.7.0, simply set PATH to include mingw-w64's bin, clang will work as you expect
You can install llvm pre-release binary for Windows here. MinGW-w64 can be downloaded here. Of course, you should make sure the paths are properly set up.
For the latest version of clang, e.g., clang 6.0.0. The above solution by #user5271266 will not be enough. Now the default target for clang Windows is x86_64-pc-windows-msvc (Assume that you are using 64 bit Windows).
In order to compile C++ source files, according to here, we should change the target:
clang++ -target x86_64-pc-windows-gnu -std=c++14 test.cc -o test.exe
My problem is when debugging from Eclipse a program that uses Boost (even pure header) then the GDB debugger is unable to locate frame base for the function being trace into.
Please note that except this, the program works like a charm in debug and release mode. The problem is only for debugging and inspecting source code refering to Boost.
Please not also that the problem only affects OSX Yosemit.
The issue is that I can't see the value of the local variables. Below is the message I have in the "(x)= Variables" window of Eclipse :
Failed to execute MI command:
-data-evaluate-expression result
Error message from debugger back end:
Could not find the frame base for "main()".
The code is as simple as :
#include <boost/regex.hpp>
#include <iostream>
int main() {
int result = 1;
boost::regex reExpression("[a-z]+");
std::cout << "!!!Hello World !!!" << std::endl;
result ++;
cout << " Result = " << result << "\n";
return result;
}
The program is compiled using the command :
g++ -v -o -g bin/Essai-MACOS-Debug src/Essai.cpp -I/opt/local/include /opt/local/lib/libboost_regex-mt.a
If you remove the reference to Boost.Regex then everything is ok. I can inspect the value of the local variable result.
More interesting: I built a library with a single function relying on Boost, and call that function from main(). It happens that can inspect the code in main() and have the value of main's local variable but when I came inside the library's function, the one now referring to boost then again I can't see the local variables of that function.
So it seems that as soon as a source file is referring to Boost, GDB get confused.
I have installed GCC 4.9, GDB 7.7 and Boost 1.57 using MacPort on OSX Yosemit.
I've compile Boost from source with MacPort in order to use GCC instead of GCC using the command :
sudo port install -ns boost config.compiler=macport-gcc-4.9
I also tried with a version of Boost I compiled myself and I did have the same issue.
Does anyone knows about this problem ?
EDIT:
I've compiled and installed the last GDB version from sources (7.9) and have the same issue described here than with the 7.7.1 provided by MacPorts.
I just upgraded to Mountain Lion OSX 10.8 and along with that I foolishly upgraded to Xcode 4.4.
However, after this upgrade "make" is gone and things like gcc -v also do not work.
This is a big thing since I am writing my phD and I rely on make to compile my LaTeX docs...
Downloading the "command line tools"
http://adcdownload.apple.com/Developer_Tools/xcode_4.4_gm_seed/cltools10_8gmseed6938077a.dmg
Is apparently not allowed for non-paying dev accounts.
What kind of foolishness is this?
They're not gone, they've just been relocated to inside Xcode's app bundle. This is actually nicer as it allows side by side installs of different XCode/SDK versions.
You can find them at: /Applications/Xcode.app/Contents/Developer/usr/bin
Also check out the xcode-select tool to allow you to choose the current active toolchain path.
I did the same thing as you this morning. To fix it, I just added the following to my ~/.bash_profile:
export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin
Interestingly,
The "Downloads" interface inside Xcode 4.4 seems to point to the following location for downloading the "Command line tools":
http://adcdownload.apple.com/Developer_Tools/xcode_4.4_gm_seed/cltools10_8gmseed6938077a.dmg
Judging by the "gm" reference in that URL I think this is an error. Without a paying dev account you cannot download this...
I issued the export PATH command, and then tried to compile hello.c, but compilation fails because it does not find the file stdio.h. So, I changed the include statement to specify the full path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h, but then compilation fails because it does not find 3 other .h files.
export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin
cat hello.c
#include <stdio.h>
int main()
{
printf("Hello World \n");
}
gcc hello.c -o hello
hello.c:1:19: error: stdio.h: No such file or directory
hello.c: In function ‘main’:
hello.c:4: warning: incompatible implicit declaration of built-in function ‘printf’
gcc hello.c -o hello
In file included from hello.c:2:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h:64:23: error: sys/cdefs.h: No such file or directory
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h:65:26: error: Availability.h: No such file or directory
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/stdio.h:67:20: error: _types.h: No such file or directory
Old thread, but I was just looking for this myself in Xcode 4.6:
Install Xcode
Go to Preferences->Downloads->Components and there will be an "Install" button next to "Command Line Tools"
I struggled during the whole afternoon with GMP and Mingw, and after a HUGE number of tries I made to install it. Actually, what I did is: installing a fresh brand new copy of MinGW (32 bit, standard latest version, downloaded from sourceforge), selecting the msys component during installation, and then using msys to install GMP from sources.
1) I downloaded sources from the home page of the gmplib official website (just the standard source release, link in the upper part of the home page).
2) I extracted it into some location reachable from msys.
3) I did the "./configure" step
4) Then the "make" step
5) Then "make install"
6) Then "make check".
No problems, it looked like it was just working. I tried to compile this under gcc:
#include <stdio.h>
#include <gmp.h>
int main(int argc, char *argv[])
{
mpz_t a, b; /* working numbers */
return 0;
}
It compiled without any error. But then, when I try to add:
mpz_init(a);
right under the declaration of a, gcc prints out:
C:\Users\MATTEO~1\AppData\Local\Temp\cc6wXtx9.o:gmptest.c:(.text+0x1c): undefine
d reference to `__gmpz_init'
collect2: ld returned 1 exit status
Do you know anything about this error? Can anybody help me? Thank you very much!
Matteo
Just a guess, but did you include "-lgmp" to tell the compiler to link to the gmp library?