C++11 not Working with Macports gcc47 - c++

I recently installed Macports on my Macbook Pro, and installed the gcc47 port. After adding alias g++="g++-mp-4.7" to my .profile and ensuring that I was using g++-4.7, I attempted to build one of my projects that makes use of C++11. However, I receive this error:
cc1plus: error: unrecognized command line option "-std=c++11"
I find this strange, since Macports built gcc with support for C++, so I would expect that C++11 should work without any problems. I have pasted the output from g++ -v below. Do you have any ideas as to why g++-mp-4.7 is not recognizing -std=c++11 as a valid option?
COLLECT_GCC=g++-mp-4.7
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.7.1/lto-wrapper
Target: x86_64-apple-darwin12
Configured with: ../gcc-4.7.1/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc47 --includedir=/opt/local/include/gcc47 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.7 --with-libiconv-prefix=/opt/local --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.7 --with-gxx-include-dir=/opt/local/include/gcc47/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-ppl=/opt/local --with-cloog=/opt/local --enable-cloog-backend=isl --enable-stage1-checking --disable-multilib --enable-lto --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --disable-ppl-version-check --with-pkgversion='MacPorts gcc47 4.7.1_2'
Thread model: posix
gcc version 4.7.1 (MacPorts gcc47 4.7.1_2)

For future reference, if you install gcc_select you can do:
sudo port select gcc mp-gcc47
hash gcc
This should setup your Macports installed gcc; if you need to reset to Xcode's default, just rerun it with 'none'
EDIT: and Xcode 4.4.x comes with a decent build of Clang; if you install it and then from Preferences->Downloads: Command Line Tools click "Install"
Run clang as:
clang++ -std=c++11 -stdlib=libc++ ...
Note that if mp-gcc47 is not installed, you'll need to do this first:
sudo port install mp-gcc47
To get a full list of available gcc ports:
sudo port list | grep gcc | less

My problem was that since GNU Make executes commands in a different shell, alias g++="g++-mp-4.7" will have no effect on the commands executed by make.

Related

gcc on macOS is running from /opt/local instead of /usr/bin

I was trying to upgrade my gcc version using brew and copying the new version file to usr/bin by using:
sudo ln -s /usr/local/Cellar/gcc/9.3.0/bin/c++-9 /usr/bin/c++
sudo ln -s /usr/local/Cellar/gcc/9.3.0/bin/g++-9 /usr/bin/g++
sudo ln -s /usr/local/Cellar/gcc/9.3.0/bin/gcc-9 /usr/bin/gcc
sudo ln -s /usr/local/Cellar/gcc/9.3.0/bin/gcc-9 /usr/bin/cc
then I add the path to .bash_profile as below
PATH="/usr/local/Cellar/gcc/9.3.0/bin:/usr/local/Cellar/gcc/9.3.0/lib:${PATH}"
export PATH
after all these done, I ran gcc -v, there is no change on version and I found the gcc it gave me was from /opt/local instead of usr/bin. Any idea about this situation?
The terminal below is the gcc -v which computer gives me.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin16/5.4.0/lto-wrapper
Target: x86_64-apple-darwin16
Configured with: /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_lang_gcc5/gcc5/work/gcc-5.4.0/configure --prefix=/opt/local --build=x86_64-apple-darwin16 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc5 --includedir=/opt/local/include/gcc5 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-5 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-5 --with-gxx-include-dir=/opt/local/include/gcc5/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-build-config=bootstrap-debug --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc5 5.4.0_0'
Thread model: posix
gcc version 5.4.0 (MacPorts gcc5 5.4.0_0)

Target requires the language dialect "CXX17" (with compiler extensions), but CMake does not know the compile flags to use to enable it

So I've been trying to include the <filesystem> into my project, which seem to be a bigger problem than I thought. <filesystem> should be part of c++17, I need to add that definition into my CMakeList.
My root CmakeLists look like this:
MESSAGE(“In src CMAKELIST”)
#
# Build everything in include/ directory
add_subdirectory(include)
#
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
## Main executable target
add_executable(cmakeDemo main.cpp)
# These libraries get built in include/*/, CMake will auto-set required
# compiler flags and include paths from their definitions
target_link_libraries(cmakeDemo record ${portaudio})
target_link_libraries(cmakeDemo database)
target_link_libraries(cmakeDemo match)
target_link_libraries(cmakeDemo spectogram)
In which I added the c++17 definition, but when I compile my system, I get this error:
make
“InsrcCMAKELIST”
“InincludeCMAKELIST”
“IndatabaseCMAKELIST”
“InmatchCMAKELIST”
“InrecordCMAKELIST”
“InspectogramCMAKELIST”
/home/lamda/soundcloud/src/include/spectogram/base/base.h
“outspectogramCMAKELIST”
-- Configuring done
CMake Error in src/CMakeLists.txt:
Target "cmakeDemo" requires the language dialect "CXX17" (with compiler
extensions), but CMake does not know the compile flags to use to enable it.
-- Generating done
-- Build files have been written to: /home/lamda/soundcloud/build
make: *** [cmake_check_build_system] Error 1
But somehow isn't it willing to use c++17, so I can use the filesystem library? why?
As mentioned is c++17 only supported by cmake version > 3.8, so I had to update it.
But my problem was my gcc and g++ didn't support it, so I had to update those, which I then did.
I followed this guide.
I was facing to the same issue, but if the answer was a good start, it wasn't enough (at least for me).
So here how I fix it (on a centos7 distro)
1. CMAKE > 3.8
On centos 'sudo yum info cmake' says '2.8.12'
so I have had to follow those instructions: https://cmake.org/download/ to actually ends with a '3.14.5' version
2. GCC/C++17 > 5.1.0
As mentioned by #Lamda, the tools chain need to be updated,
otherwise you will still stay stuck on the exact same error message.
Here is how CMAKE checks supported dialect:
https://github.com/Kitware/CMake/blob/master/Modules/Compiler/GNU-CXX.cmake#L45
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std=c++17")
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std=gnu++17")
elseif (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.1)
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std=c++1z")
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std=gnu++1z")
endif()
and again, no luck with the centos, 'sudo yum info gcc' says '4.8.5'
I've decide to compile GCC from the source code directly, with something like this:
wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-7.2.0/gcc-7.2.0.tar.gz
tar -xvzf gcc-7.2.0.tar.gz
cd gcc-7.2.0
./contrib/download_prerequisites
./configure \
--enable-bootstrap \
--enable-languages=c,c++,fortran,lto \
--with-bugurl=http://bugzilla.redhat.com/bugzilla \
--enable-shared \
--enable-threads=posix \
--enable-checking=release \
--disable-multilib \
--with-system-zlib \
--enable-__cxa_atexit \
--disable-libunwind-exceptions \
--enable-gnu-unique-object \
--enable-linker-build-id \
--with-gcc-major-version-only \
--enable-plugin \
--with-linker-hash-style=gnu \
--enable-initfini-array \
--enable-libmpx \
--enable-gnu-indirect-function \
--with-tune=generic \
--build=x86_64-redhat-linux
make -j4
sudo make install
sudo sh -c 'echo /usr/local/lib > /etc/ld.so.conf.d/1-gcc.conf'
sudo sh -c 'echo /usr/local/lib64 >> /etc/ld.so.conf.d/1-gcc.conf'
sudo ldconfig -v
Hence I ends with a GCC 7.2.0.
if succeed, the following test should returns 201402L
g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
3. Still the same "dialect "CXX17" error ?
In my case, something else was needed to make it works:
sudo ln -s /usr/local/bin/gcc /usr/local/bin/cc
Why? You may ask...
GCC.7.2.0 actually don't seems to come with 'cc' (which should be trivial symblink to 'gcc')
On the other hand, CMAKE determine 'g++' path, by using 'cc' path (as a hint)
In my case I still have a /bin/cc #4.8.5 and /bin/g++ #4.8.5
so even if a /usr/local/bin/g++ #7.2.0 now exists (which should by used in prior)
CMAKE will unfortunately used /bin/g++ #4.8.5 instead
But, obviously the best practices would be to remove the whole old GCC tool Chain.
Update about gcc
Building our own GCC from the sources is actually a bad idea...
In my case I get some errors by trying to link with an external library (even made with the same c++ standart /GCC version)
The best practice seems to use devtoolset yum package instead
https://www.softwarecollections.org/en/scls/rhscl/devtoolset-7/
It seems that this package has been made by including some Redhat patches an configure switch for you. So as far we don't known the magic recipe to make it work properly, it make sens to use something more official.
Conlusion:
use devtoolset yum package instead or trying to compile GCC from sources

Homebrew gcc can't find omp.h on OS X

I've spent hours trying different solutions but nothing has worked so far.
I've installed gcc via Homebrew, and linked gcc to the Homebrew gcc. gcc -v returns this:
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/5.3.0/libexec/gcc/x86_64-apple-darwin14.5.0/5.3.0/lto-wrapper
Target: x86_64-apple-darwin14.5.0
Configured with: ../configure --build=x86_64-apple-darwin14.5.0 --prefix=/usr/local/Cellar/gcc/5.3.0 --libdir=/usr/local/Cellar/gcc/5.3.0/lib/gcc/5 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-5 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --with-build-config=bootstrap-debug --disable-werror --with-pkgversion='Homebrew gcc 5.3.0 --without-multilib' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --disable-multilib
Thread model: posix
gcc version 5.3.0 (Homebrew gcc 5.3.0 --without-multilib)
However, when I try to compile I get
This is make_MWA_Tools.sh
using $CFITSIO =
building LFILE & read_mwac utilities
cc -g -O -Wall -D_FILE_OFFSET_BITS=64 -fopenmp -c mwac_utils.c
mwac_utils.c:3:10: fatal error: 'omp.h' file not found
#include <omp.h>
^
1 error generated.
make: *** [mwac_utils.o] Error 1
!!!!!!!!!!!!!!!!!!!!!!
build_lfiles/read_mwac make failed !
I've looked in gcc located under Cellar and found mop.h, but for some reason its not being included. Any help would be greatly appreciated.
Edit: I don't believe this is a duplicate question because I could not find another question that Installed gcc from Homebrew, linked 'gcc' to the homebrew gcc installation, and still couldn't find omp.h

GCC 4.9.2 cross-compiler build for ARM

I am trying to build a cross-compiler for ARM. When I add c++ to my "enable-languages" list in the configure (see) below, the gmake errors out with:
checking for exception model to use...
configure: error: unable to detect exception model
gmake[1]: *** [configure-target-libstdc++-v3]
but when I take c++ out of the "enable-languages", as in my configure script below, the GCC cross-compiler builds just fine.
../gcc-4.9.2/configure --prefix=/home/me/ANDROID21 --target=arm-linux-androideabi
--with-gmp=/home/me/ANDROID21 --with-mpfr=/home/me/ANDROID21
--with-mpc=/home/me/ANDROID21
--with-gnu-as--with-gnu-ld --enable-languages=c,fortran --disable-nls
--disable-tls
--disable-bootstrap --disable-libgomp --disable-shared --disable-libssp
-disable-libquadmath --enable-threads --enable-target-optspace --disable-libatomic
--with-sysroot=/home/me/android-ndk-r10c/platforms/android-21/arch-arm
For reference, here is my machine info:
$ uname -a
Linux (system name) 2.6.32-504.el6.x86_64 #1 SMP Tue Sep 16 01:56:35 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
And the GCC I'm building off of:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/me/GCC4.9.2/libexec/gcc/x86_64-unknown-linux-gnu/4.9.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /home/me/gcc-4.9.2/configure --prefix=/home/me/GCC4.9.2 --disable-multilib
--enable-libgomp --enable-languages=c,c++,objc,obj-c++,java,fortran
Thread model: posix
gcc version 4.9.2 (GCC)
Any ideas on possible causes?
Haven't found anything definitive in the archives yet. Would really like to get the c++ portion to cross-compile too.
TIA,
Steve

MingW c++ compile Error Win 7 x64

I'm trying to compile a simple C++ HelloWorld example on my Win 7 x64 machine with the mingw compiler.
I downloaded the automated installer here: http://sourceforge.net/project/showfiles.php?group_id=2435
After setting the required paths in the system environment variables i was trying to get the compiler to work with a simple HelloWorld example. Sadly, it always crashes with the following error dialog (i am using a german windows, so the translation may not be perfect):
cc1plus.exe - Application error
The application could not be started correctly (0xc000003b).
Click "OK" to close the application.
Below the verbose compile output
W:\Timo\workspace\c++\HelloWorld>gcc HelloWorld.cpp -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=d:/workspace/mingw/bin/../libexec/gcc/mingw32/4.5.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.5.2/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --ena
ble-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --disable-werror -
-build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.5.2 (GCC)
COLLECT_GCC_OPTIONS='-v' '-mtune=i386' '-march=i386'
d:/workspace/mingw/bin/../libexec/gcc/mingw32/4.5.2/cc1plus.exe -quiet -v -iprefix d:\workspace\mingw\bin\../lib/gcc/mingw32/4.5.2/ He
lloWorld.cpp -quiet -dumpbase HelloWorld.cpp -mtune=i386 -march=i386 -auxbase HelloWorld -version -o C:\Users\afro\AppData\Local\Temp\c
czfrXM3.s
Thanks in advance.
Timo
I've found the problem. The file i tried to compile was saved on my NAS-Server which requires a login to access the files. I was able to access the file via console, the compiler obviously did not. After moving the source files on my local hdd, the compiler started working.