qt creator defaults to g++ (4.2) after specifying custom GCC - macos

Problem
I am having an issue implementing the g++48 compiler in QT Creator. I built this compiler using MacPorts. It appears that QT Creator ignores my compiler and defaults xcode g++42. How do I properly setup the compiler to override this?
Troubleshooting
Did you install gcc/g++ correctly and is it the main one selected?
I have ensured that gcc was installed correctly and the path is correct by doing the follows:
:~ which gcc:
/opt/local/bin/g++
:~ g++ --version:
g++ (MacPorts gcc48 4.8.1_3) 4.8.1
What system are you using?
My system: Mac OSX 10.9 Mavericks. QT Creator 2.8.1 Based on QT 5.1.0.
Toolchain setup: In QT Creator I specified the custom GCC compiler by going Compilers_Add_GCC and putting in the compiler path /opt/local/bin/g++. If I hover the mouse over any of the #include lines in *.cpp then it properly displays the path /opt/local/include/gcc48/{headerName}. I suspect the problem is related to the QT Mkspecs, but I really don't understand what this is or how to write a custom one for my custom gcc installation (if necessary). Can this be explained?
Kit updated in QT Creator? The kit was updated by following the directions that here: Qt Creator use another GCC Version located in another Place
Why do you suspect g++42 is being used? This is based on the results I get from inspecting the build log file.
12:30:19: Running steps for project untitled...
12:30:19: Configuration unchanged, skipping qmake step.
12:30:19: Starting: "/usr/bin/make"
/Applications/Xcode.app/Contents/Developer/usr/bin/g++ -c -pipe -std=c++11 -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.6 -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -I/Users/raymondvaldes/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/raymondvaldes/Documents/code/untitled -I. -o main.o /Users/raymondvaldes/Documents/code/untitled/main.cpp
/Users/raymondvaldes/Documents/code/untitled/main.cpp:4:10: fatal error: 'random' file not found
#include <random>
^
1 error generated.
make: *** [main.o] Error 1
12:30:20: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project untitled (kit: gcc48)
When executing step 'Make'
12:30:20: Elapsed time: 00:01.
and
RAYMONDs-MacBook-Air:~ raymondvaldes$ /Applications/Xcode.app/Contents/Developer/usr/bin/g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
Finally, here is my simple working example:
#include <iostream>
#include <complex>
#include <cmath>
#include <random>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
and my pro file.
cache()
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
QMAKE_CXXFLAGS += -std=c++11
SOURCES += main.cpp
Thank you.

I finally solved this issue and I am now able to use my MacPorts GCC48 compiler in QT Creator. As I suspected I needed to create a gcc48 mkspecs folder in the QT Directory and feed the folder name to the QT Creator custom compiler setup screen. I created "macs-g++48" folder that was copied from the generic "macs-g++" folder in "~path~QT/5.1.x/clang_64/mkspecs". I had to modify qmake.conf. I commented out
#include(../common/g++-macx.conf)
#include(g++-base.conf)
and placed their contents within quake.conf while making the following modifications:
QMAKE_CC = gcc-mp-4.8
QMAKE_CXX = g++-mp-4.8

Related

Qtcreator on Mac OS can't see std when using cmake and openmp

If I have a project with this CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(myproject LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(myexe main.cpp)
find_package(OpenMP REQUIRED)
target_link_libraries(myexe PUBLIC OpenMP::OpenMP_CXX)
And the following code in main.cpp
#include <iostream>
int main ()
{
std::cout << "Hi\n";
}
The project actually builds and runs just fine. But the IDE itself is broken and can't find the std headers. And as a result it has red squiggly lines under everything std related. In our example it will look like this:
This issue only happens when I use OpenMP in CMakeLists.txt
In the "General Messages" tab I'm getting the following error:
Compiler feature detection failure!
The command "/usr/bin/clang++ -fPIE -fopenmp -std=gnu++17 -x c++ -E -v - -target x86_64-apple-darwin20.3.0" terminated with exit code 1.
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
clang: error: unsupported option '-fopenmp'
So I'm pretty sure that's related. How do I fix this? Halp!
Ok, I don't understand what causes that issue but I figured out how to solve it.
We'll have to use the llvm clang.
brew install llvm
That will install clang and clang++ compilers at /usr/local/opt/llvm/bin/.
We'll have to make sure that qtcreator uses the llvm compiler instead of the one at /usr/bin/clang++. And we do that by making a new kit that uses the new compiler and configure the project to use that new kit instead.
Qtcreator > Preferences > Kits > Compilers
Add > Clang > C
Give it a some name like: llvm clang, and its path at /usr/local/opt/llvm/bin/clang
Add > Clang > C++
Give it a some name like: llvm clang++, and its path at /usr/local/opt/llvm/bin/clang++
Qtcreator > Preferences > Kits > Kits
Add
Give it a some name like: llvm clang, and choose the llvm clang and llvm clang++ for the C and C++ compilers respectively.
And the last step would be to have your project use the new kit instead.
On the Projects tab on the left side
Build & Run
Choose the llvm clang kit
And that will be all! Hope that helps :)

Enable OpenMP support in clang in Mac OS X (sierra & Mojave)

I am using Mac OS X Sierra, and I found that clang (LLVM version 8.1.0 (clang-802.0.38)) does not support OpenMP:
when I run clang -fopenmp program_name.c, I got the following error:
clang: error: unsupported option '-fopenmp'
It seems that clang does not support -fopenmp flag.
I could not find any openmp library in homebrew. According to LLVM website, LLVM already supports OpenMP. But I could not find a way to enable it during compiling.
Does this mean that the default clang in Mac does not support OpenMP?
Could you provide any suggestions?
(When I switch to GCC to compile the same program (gcc is installed using brew install gcc --without-multilib), and the compilation is successful.)
Try using Homebrew's llvm:
brew install llvm
You then have all the llvm binaries in /usr/local/opt/llvm/bin.
Compile the OpenMP Hello World program. Put omp_hello.c
/******************************************************************************
* FILE: omp_hello.c
* DESCRIPTION:
* OpenMP Example - Hello World - C/C++ Version
* In this simple example, the master thread forks a parallel region.
* All threads in the team obtain their unique thread number and print it.
* The master thread only prints the total number of threads. Two OpenMP
* library routines are used to obtain the number of threads and each
* thread's number.
* AUTHOR: Blaise Barney 5/99
* LAST REVISED: 04/06/05
******************************************************************************/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int nthreads, tid;
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
{
/* Obtain thread number */
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}
in a file and use:
/usr/local/opt/llvm/bin/clang -fopenmp -L/usr/local/opt/llvm/lib omp_hello.c -o hello
You might also have to set the CPPFLAGS with -I/usr/local/opt/llvm/include.
The makefile should look like this:
CPP = /usr/local/opt/llvm/bin/clang
CPPFLAGS = -I/usr/local/opt/llvm/include -fopenmp
LDFLAGS = -L/usr/local/opt/llvm/lib
omp_hello: omp_hello.c
$(CPP) $(CPPFLAGS) $^ -o $# $(LDFLAGS)
Update
In macOS 10.14 (Mojave) you might get an error like
/usr/local/Cellar/llvm/7.0.1/lib/clang/7.0.1/include/omp.h:118:13: fatal error: 'stdlib.h' file not found
If this happens, the macOS SDK headers are missing from /usr/include. They moved into the SDK itself with Xcode 10. Install the headers into /usr/include with
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
Other people have given one solution (using Homebrew llvm). You can also use OpenMP with Apple Clang and Homebrew libomp (brew install libomp). Just replace a command like clang -fopenmp test.c with clang -Xpreprocessor -fopenmp test.c -lomp.
MacOS Mojave with CMake
Install LLVM with openmp and libomp with brew
brew update
brew install llvm libomp
add include directories and link directories in CMakeList.txt
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
run CMake with the new compilers
cmake -DCMAKE_C_COMPILER="/usr/local/opt/llvm/bin/clang" -DCMAKE_CXX_COMPILER="/usr/local/opt/llvm/bin/clang++" ..
The clang version is 7.0.1 at time of writing
Conda-Based Compilation Environment
Conda uses clang for OSX compilation (umbrella package cxx-compiler), but I hit similar issues with using llvm-openmp and the -fopenmp flag throwing errors. Solution is rather similar to other answers, but I am including here in case others have more exactly this issue.
Specific solution was to include the Conda environment's include/ directory in the CFLAGS, namely:
CFLAGS="-I${CONDA_PREFIX}/include"
Note, I also needed to add -lstdc++ -Wl,-rpath ${CONDA_PREFIX}/lib -L${CONDA_PREFIX}/lib when linking, similar to this GitHub Issue.

error: <atomic> is not implemented in LLVM version 5.1

I'm trying to compile libcxxabi with cmake, and running into issues. I believe that this is because I don't have a separate copy of llvm installed that has llvm-config. So I've checked out llvm, and am trying to build that with cmake. I get the error:
CMake Error at cmake/modules/HandleLLVMOptions.cmake:37 (message):
Host Clang must be able to find libstdc++4.7 or newer!
And cmake/modules/HandleLLVMOptions.cmake:37 is literally a block like:
check_cxx_source_compiles("
#include <atomic>
std::atomic<float> x(0.0f);
int main() { return (float)x; }"
LLVM_NO_OLD_LIBSTDCXX)
if(NOT LLVM_NO_OLD_LIBSTDCXX)
message(FATAL_ERROR "Host Clang must be able to find libstdc++4.7 or newer!")
endif()
and if I try to compile the 3 liner with the compiler flag -std=c++0x (which is higher up in the cmake file) I get the error:
atomic.cpp:1:10: fatal error: 'atomic' file not found
#include <atomic>
^
1 error generated.
How is it that I don't have support for c++11 atomics? I'm running OSX 10.8.5, upgraded from 10.8.4, and XCode version 5.1.1, and clang++ 5.1 (clang-503.0.40) (based on LLVM 3.4svn). I have no options to upgrade xcode in the app store, nor the developer tools. Do I need to reinstall XCode, or upgrade to Mavericks?
I needed to check out all of LLVM and pass an additonal command line argument to CMake.
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd ../projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ..
mkdir build
cd build
cmake .. -DLLVM_ENABLE_LIBCXX=ON
make
make install
Had the same issue with xCode 6.4. When using atomic in a simple helloWorld program it worked, but when using a project generated by CMake, I had the "#error is not implemented"
It appears CMake needs special flags to enable c++11 on mac... So, did exactly (almost... replaced if(UNIX) by if(APPLE)) as kitware indicates here:
https://cmake.org/Wiki/CMake/Tutorials/C%2B%2B11Flags
In case the link stops working one day....
cmake_minimum_required(VERSION 2.6)
PROJECT(Test)
if(UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
endif()
# MSVC does not require any special flags

C++11 in Qt5 / QtCreator 3.0.1 on OSX

Just downloaded and installed Qt5 on OSX 10.9.2.
When trying to compile some C++11 code using the clang run kit, I get a failure when I try to include cstdint;
fatal error: 'cstdint' file not found
After a lot of googling, it seems that c++11 is not 'enabled' by default which triggers this error. It further seems that the way to enable c++11 has changed in Qt5. It should be as simple as adding CONFIG+=c++11 in "Projects -> Build & Run -> Build Steps -> Additional arguments" if I understand it correctly.
However this makes no difference whatsoever.
So; anyone knows how to enable c++11 using QtCreator with the clang kit?
About QtCreator gives me the following information:
QtCreator 3.0.1
Based on Qt 5.2.1 (Clang 5.0 (Apple), 64 bit)
The compiler output is as follows:
14:25:23: Running steps for project shelly...
14:25:23: Starting: "/Users/m/Qt5.2.1/5.2.1/clang_64/bin/qmake" /Volumes/files/Programmering/shelly/shelly.pro -r -spec macx-clang CONFIG+=debug CONFIG+=x86_64 CONFIG+=declarative_debug CONFIG+=qml_debug CONFIG+=c++11
14:25:23: The process "/Users/m/Qt5.2.1/5.2.1/clang_64/bin/qmake" exited normally.
14:25:23: Starting: "/usr/bin/make"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -c -pipe -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.7 -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_OPENGL_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/m/Qt5.2.1/5.2.1/clang_64/mkspecs/macx-clang -I../shelly -I/Users/m/Qt5.2.1/5.2.1/clang_64/lib/QtOpenGL.framework/Versions/5/Headers -I/Users/m/Qt5.2.1/5.2.1/clang_64/lib/QtWidgets.framework/Versions/5/Headers -I/Users/m/Qt5.2.1/5.2.1/clang_64/lib/QtGui.framework/Versions/5/Headers -I/Users/m/Qt5.2.1/5.2.1/clang_64/lib/QtCore.framework/Versions/5/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/m/Qt5.2.1/5.2.1/clang_64/lib -o datainterval.o ../shelly/core/datainterval.c++
In file included from ../shelly/core/datainterval.c++:5:
In file included from ../shelly/core/datainterval.h++:9:
../shelly/core/datatime.h++:9:10: fatal error: 'cstdint' file not found
#include <cstdint> // GPL w/runtime exception
^
1 error generated.
make: *** [datainterval.o] Error 1
14:25:24: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project shelly (kit: Desktop Qt 5.2.1 clang 64bit)
When executing step 'Make'
14:25:24: Elapsed time: 00:01.
Update:
Have made an 'minimal recreate' of my problem;
Create a new project, a “Qt Widgets Application”.
Leave everything at its defaults except for the path.
In the created file mainwindow.c++, add the cstdint include statement:
Try to build it (it fails as above)
Add in “Projects” -> “Build & Run” -> “Build Steps” -> “Additional arguments”
CONFIG+=c++11
The “Effective qmake call” line then reads
qmake /Users/m/Documents/ProgrammeringOSX/testC11/testC11.pro -r -spec macx-clang CONFIG+=debug CONFIG+=x86_64 CONFIG+=declarative_debug CONFIG+=qml_debug CONFIG+=c++11
…and the error is still there.
:(
I have the same setup as you, and have used c++11 features after updating the project .pro file with the following line:
CONFIG += c++11
However, I think your solution should accomplish the same thing, so it may be an external configuration issue. I wasn't able to reproduce the error when I included the header file.
Ok,
I seem to have “fixed” it… I got to the point that I was randomly changing parameters just to see what effect it had and all of a sudden this did it:
QMAKE_MACOSX_DEPLOYMENT_TARGET=10.9
(doesn’t matter if I set it in the .pro file or in the GUI)
If I added CONFIG+=c++11, the target was set to 10.7, without it 10.6, so this forces it to 10.9 (tried 10.8 as well, but nope; 10.9 it is).
A funny thing is that I do not need to add CONFIG+=c++11 in the “minimum recreation” version… I guess I will get to that point if I actually add any C++11 code. But baby steps, baby steps!
Another funny thing is that when I changed the target to 10.9, THEN my bogus QMAKE_CXXFLAGS parameters were added and the clang call failed (I guess it heeded my ‘pleaseFailNow’ parameter).
Thank you for your help!
Cheers
/ Daniel

compiling openmp, macports gcc, and eclipse cdt

I am newbie in openmp. The following is the environment.
OS : Mac OSX Mavericks
Compiler : gcc (MacPorts gcc48 4.8.2_0) 4.8.2
IDE : Eclipse Kepler CDT plugin
I wrote the following openmp program
#include < stdio.h>
#include < omp.h>
int main()
{
#pragma omp parallel
{
int i=omp_get_thread_num();
printf("hello (%d)",i);
printf("world (%d)",i);
}
}
I compiled the above program and got the error that omp.h is not found and lgomp not found. Hence I added in the project properties an include path with /opt/local/lib/gcc48/gcc/x86_64-apple-darwin13/4.8.2/include and a library path /opt/local/lib/gcc48. The include path had the omp.h file and the library path had the file libomp.o.
I include the -fopenmp option in both the linker and the compiler option through project properties. It is compiling with gcc -I/opt/local/lib/gcc48/gcc/x86_64-apple-darwin13/4.8.2/include -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -MMD -MP -MF"src/OpenMPCourseExamples.d" -MT"src/OpenMPCourseExamples.d" -o "src/OpenMPCourseExamples.o" "../src/OpenMPCourseExamples.c" and linking with the command "gcc -L/opt/local/lib/gcc48 -fopenmp -o "OpenMPCourseExamples" ./src/OpenMPCourseExamples.o".
With the above command it compiles without an error but with a warning - "warning: unknown pragma ignored [-Wunknown-pragmas] #pragma omp parallel".
Also, I set an environment variable in the launch properties with OMP_NUM_THREADS=4. I ran the program that compiled with the above warning. I am getting only "hello (0)world (0)". I was under the impression that I should start four threads and should see the other outputs of "hello(1)world(1)hello(2)world(2)hello(3)world(3)" in some ordering as well. Now, here are my following questions.
Why am I getting the #pragma warning?
Is the compiler really detecting the openmp and building with openmp?
If everything is correct, why am I not seeing four different threads getting started?
The final steps that worked for openmp, macports gcc compiler, eclipse CDT in mac osx mavericks are.
Enable "Make ToolChain(s) Preferred" in Eclipse->Preference->C/C++->New C/C++ Project Wizard.
sudo port select --list gcc and set it sudo port select --set gcc with mp-gcc.
File->New Project->C Project (not C++) and create a hello world project.
In Project->Properties->C/C++ Build->Settings->Tool Settings set the following. (a) GCC C Compiler to /opt/local/bin/gcc-mp-4.8 (b)MAC OSX Linker to /opt/local/bin/gcc-mp-4.8
Build the hello world project and make sure, it compiles and runs successfully.
Include the open mp code. The code asked in the question of this page.
Go to again Project->Properties->C/C++ Build->Settings->Tool Settings set the following. (a) GCC Compiler ->Miscellaneous add -fopenmp (b) MacOSx Linker->Miscellaneous set -fopenmp
Build the code again.
The above steps worked good for me.
MacPorts configures the GCC build process with --program-suffix=-mp-${major} and therefore all compiler executables have the -mp-4.8 suffix. When you call gcc, you end up using Apple's Clang compiler, which does not support OpenMP and therefore does not recognise the -fopenmp option and #pragma omp ....
You have to do the following changes to the project settings:
Change the compiler command to gcc-mp-4.8
Change the linker command to gcc-mp-4.8
Remove the explicit specification of the include and library paths since the presence of -fopenmp adds them automatically.

Resources