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

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 :)

Related

How to use clang to create a windows binary

I have installed clang on my Windows 10 machine.
$ clang --version
clang version 6.0.1 (tags/RELEASE_601/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
With it, I can compile C and C++ sources and generate object files, and use lib.exe to create libraries of them.
When it's time to link it into a binary, to my surprise, clang is creating a lib/exp output, not an executable?
Why does clang think it should be creating a library, not an executable?
clang++ -oarmor XWin/main.o -lSDL2main -lpi -ldblunt -lsino -lbase -L. -L ../../src/sino -L MSWin/libs/SDL/lib ../../src/Chipmunk2D/build.release/x64/Release/chipmunk.lib MSWin/libs/OpenAL/lib/OpenAL32.lib -lSDL2 -lOpenGL32
Creating library armor.lib and object armor.exp
LINK : fatal error LNK1561: entry point must be defined
clang++.exe: error: linker command failed with exit code 1561 (use -v to see invocation)
In UNIX, clang will by default create binaries, not libraries?
Check this link -
https://metricpanda.com/rival-fortress-update-27-compiling-with-clang-on-windows
There are currently two flavors of Clang that work on Windows: vanilla LLVM Clang and Clang/C2 with Microsoft Codegen.

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

Default compiler from llvm-clang to llvm-gcc42 (command line)

I'm trying to compile a project from the command line. The default compiler LLVM-clang and i need to compile with LLVM-gcc42. Switching a compiler in Xcode is really easy. However, on command line it seems to be more problematic. To my understanding "/usr/bin/cc" and "/use/bin/gcc" are used for pointing c/c++ compilers. I noticed that "cc" was linked to "clang" in same directory. So, i changed "cc" to point at "gcc". Did not helped. When i run
$ gcc -v
i get response
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.5.0
How do i change the clang to gcc?
My setup is:
MAC OS X 10.8.5
Xcode 4.6.2
Command line tools
EDIT 13.02.2014
The project is a QT project which uses mixture of third party c/c++ libraries. My task is to write a new objective C lib to it. The project is build with -spec macx-g++ parameter. The configuration scripts can be found /QTROOT/mkspecs/macx-g++ and /QTROOT/mkspecs/common. In g++-base.conf key variables are defined as:
QMAKE_CC = gcc
QMAKE_CXX = g++
Instead of changing links which can affect other programs use the full path to the llvm executables in your build script
e.g. if you have installed the command line tools
QMAKE_CC = /usr/bin/llvm-gcc-4.2
QMAKE_CXX = /usr/bin/llvm-g++-4.2
Okay, nailed the problem. Solution was simple. I deleted the old links from /usr/bin/gcc and /usr/bin/g++ and created new ones pointing to the proper compilers.
gcc -> /Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
and
g++ -> /Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/bin/llvm-g++-4.2

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.

Clang Code Coverage - Mac OS X - Linker Error

I could successfully get code coverage information on our C++ code base on Linux using the GCC features of GCOV and the LCOV tool.
But I am having trouble in Mac OS X.
As Apple does not have the classic GCC compiler anymore, and we fear that the LLVM-GCC compiler would one day disappear too (LLVM-GCC is not even available as an option in Xcode 5.0) - we have decided to use Clang to compile our code.
While using the Clang compiler I am passing in these flags -->
-g -fprofile-arcs -ftest-coverage to generate the Code Coverage information.
I can see the .gcno files getting generated along with the object files.
When it comes to linking - "-lgcov” linker flag which works with GCC is not supported.
The code coverage on Clang / LLVM is now supported by the “profile_rt” library.
Unfortunately it’s a bit tricky to find this library because Apple for whatever reason decided not to include it in the default library path. Instead you’ll have to manually navigate to /usr/lib/ to link against it:
And as specified am linking against libprofile_rt.a library.
But i have linker issues.
But i keep getting these linker errors
Undefined symbols for architecture x86_64:
"_llvm_gcov_init", referenced from:
___llvm_gcov_init in Iso9660Reader.o
___llvm_gcov_init in AutoExtractCreator.o
___llvm_gcov_init in TempFilePath.o
___llvm_gcov_init in TempPath.o
___llvm_gcov_init in ReadDirectory.o
___llvm_gcov_init in OpenDirectory.o
___llvm_gcov_init in SpltPath.o
...
ld: symbol(s) not found for architecture x86_64
I also tried linking against the dynamic library - libprofile_rt.dylib found in
/usr/lib folder - But i still get the same issue.
This is Clang Version running on Mountain Lion.
clang --version
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
I also have Xcode 5.0 and Developer Tools installed.
I solved this.
I was missing the following Linker Flags
-Wall -fprofile-arcs -ftest-coverage
Other Linker Flag -fprofile-arcs fixes the issue for me.
Build Settings > Other Linker Flags > -fprofile-arcs
The above answer did not work for me on OSX Yosemite (10.10.3) with Xcode 6.3.1. It seems that Apple moved these libraries around. I was able to get it to work with the following compile options:
-lclang_rt.profile_osx
-L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/6.1.0/lib/darwin

Resources