XCode compiler error, with a project built using CMake - c++

I'm trying to build a XCode (version 5.0) project that was generated from CMake 3.x.
clang compiler version:
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
When I try to build the project
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 blah blah -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk blah blah -Wno-unknown-pragmas -F/usr/local/bin/Debug ;**-std=c++0x -stdlib=libc++** -Wall -MMD -MT blah blah
I get this error:
clang: error: no such file or directory: ';-std=c++0x'
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1
How can I fix this error?
-std and -stdlib are coming from these project properties:
Apple LLVM 5.0 - Language C++ ==> Dialect and Standard library
How do I set this from CMAKE?

If I recall correctly there is no c++0x language dialect in Xcode 5, but there is a c++11 dialect.
These properties can be changed in the CMake project in two ways:
(I've never managed to do this) by setting CMAKE_XCODE_ATTRIBUTE_ and then the name of the attribute you see in the Xcode project
By setting the ''generic'' compiler property CMAKE_CXX_FLAGS variable

There is a stray semi-colon in that command line:
-F/usr/local/bin/Debug ;-std=c++0x -stdlib=libc++
^
So the shell is seeing two commands; the first starts with c++ and the second with -std=c++0x. Find and remove that semi-colon and it should start working.

Related

How to find and provide C++ library headers for clang?

I've built LLVM and Clang from sources using following instruction in order to try some of the latest C++ features.
When I try to compile basic C++ program using this clang I get errors about missing basic headers:
% /usr/local/bin/clang++ -std=c++20 main.cpp
In file included from main.cpp:1:
main.cpp:3:10: fatal error: 'array' file not found
#include <array>
I similarly have brew installed clang, which works perfectly.
The instruction mentions providing C++ library headers to clang, but I don't understand:
How to locate those?
How to make sure, that they are also up to date to support latest C++ features?
I have MacOS Monterey 12.4
You should specify a path to the sdk for your target platform.
You can retreive this by relying on xcrun:
/usr/local/bin/clang++ -isysroot $(xcrun --show-sdk-path) -std=c++20 main.cpp
In case you want to target other platforms (in the example bellow, for iphone) you have to specify both the target triple and the path to the appropiate sdk:
/usr/local/bin/clang++ -target arm64-apple-ios -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -std=c++20 main.cpp
There is also an alternative to the -isysroot option. You could set up the SDKROOT environment variable to point to the target sdk path:
export SDKPATH=$(xcrun --show-sdk-path)
/usr/local/bin/clang++ -std=c++20 main.cpp

In XCode clang 11 I get: No template named 'initializer_list' in namespace 'std'

When someone on my team executes:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake .. -G Xcode
using XCode 10, and then opens the xcodeproj folder, Xcode starts and everything compiles.
In his project.pbxproj file are:
OTHER_CPLUSPLUSFLAGS = "-std=c++11 -pthread -fstrict-aliasing -Wall -Wcast-qual -Wextra -Wformat -Wpedantic -Wswitch-default -Wno-unknown-pragmas
OTHER_LDFLAGS = " -Wl,-search_paths_first -Wl,-headerpad_max_install_names $HEAD/sdsp/metrics/test/build/sws-metrics/Release/libsws-metrics.a";
When I do the same thing using:
Apple clang version 11.0.0 (clang-1100.0.33.17)
it does not compile. I get errors:
No template named 'initializer_list' in namespace 'std'
A space is required between consecutive right angle brackets (use '> >')
And my OTHER variables are blank.
I tried setting my OTHER variables to be like his- it didn't help.
In the project I tried all the various options for
Apple Clang - Language
C Language Dialect - Compiler Default, c11, gnu11
Apple Clang - Language - C++
C++ Language Dialect - C++11 [-std=c++11] (and gnu11 and the 14 and 17's)
C++ Standard Library - Compiler Default, libstdc++, libc++
Apple Clang - Warnings - C++
Using C++11 extensions in earlier versions of C++ - was No and I tried that and Yes
Nothing works.
We both create the makefiles from the same folder with:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake .. -G Xcode
Any idea what to do?
====
See a sample project on https://randy.strausses.net/info/be

How to use the available C++17 flags from Xcode in Terminal

My Xcode 9.2 has the option to compile C++ using: -std=c++17 or -std=gnu++17.
My C++ code that has some C++17 features compiles successfully.
However, if I try to compile the same code in my Terminal using the same flags, I get the following errors:
clang++ -std=c++17 test.cpp -o test
error: invalid value 'c++17' in '-std=c++17'
and
clang++ -std=gnu++17 test.cpp -o test
error: invalid value 'gnu++17' in '-std=gnu++17'
My OSX version is 10.13.3 and my Clang++ version is:
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.4.0
I thought that Terminal would use the same compiler from Xcode. That may be the case, but I can't use the same options. At least I couldn't.
I tried using -std=C++1z, but it didn't compile my code because it doesn't understand some new features from C++17.
So my question is: Is there a way to use -std=c++17 or -std=gnu++17 that are currently available in my Xcode 9.2, in my Terminal app?
This questions is not the same as the others available at stackoverflow because all the answers I found the -std=c++17options was not available in Xcode. Now we have this options, but I can't figure out how to use it in Terminal.
The compiler Apple currently ships with Xcode 9.2 (clang-900.0.39.2) does not support the -std=c++17 flag.
Xcode uses the -std=c++1z flag when you enable c++17 support. If you want to use -std=c++17 you need to manually install clang. You could do that using brew install llvm (assuming you have homebrew installed).
You can compile your program with
/usr/local/Cellar/llvm/5.0.0/bin/clang++ -std=c++17 test.cpp -o test
You change the symlinks in usr/bin/clang++ to point at the new destination if you don't want to use the full path to the compiler.
The compiler which is shipped with Xcode 9.3 will be
Apple LLVM version 9.1.0 (clang-902.0.30)
This can handle the -std=c++17 flag.

Undefined symbol when loading LLVM plugin

I am developing an LLVM pass and want to run it as a plugin via the Clang LLVM driver:
clang -Xclang -load -Xclang myPlugin.so ...
At first I got an error similar to that described here
undefined symbol for self-built llvm opt?
After applying the flag -D_GLIBCXX_USE_CXX11_ABI=0 as suggested, I'm getting this error:
error: unable to load plugin 'myPlugin.so': 'myPlugin.so: undefined symbol: _ZNK4llvm12FunctionPass17createPrinterPassERNS_11raw_ostreamERKSs
This page suggests that there may be an ABI compatibility issue (which I don't fully understand)
http://clang-developers.42468.n3.nabble.com/Loading-Static-Analyzer-plugin-fails-with-CommandLine-Errors-td4034194.html
My objective is to compile the pass with either GCC or Clang and run it with the system Clang installation (Ubuntu 16.04, LLVM 3.8) rather than building Clang/LLVM from source.
The problem could come from multiple clang installations. The clang version you have used to compile your plugin may be different from the clang called in
clang -Xclang -load -Xclang myPlugin.so ...
If you use cmake to build your plugin, then
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
will generate the file compile_commands.json that will contain the llvm version you have used. bear make or make -n are alternatives if you don't use cmake for your plugin.
If compile_commands.json contains for example
"command": "c++ -c -I/usr/lib/llvm-4.0/include ..."
and if clang -v is clang version 3.8.0, you are likely to obtain this error message especially if llvm::FunctionPass::createPrinterPass is in llvm-4.0 and not in llvm-3.8.
A solution may be to compile with
clang-xxx -Xclang -load -Xclang myPlugin.so ...
where clang-xxx contains the llvm-xxx that is referenced in compile_commands.json.
I was receiving that error because first argument I passed into the RegisterPass had the same name as the pass itself:
static RegisterPass<MyPass> X("MyPass", "DPVariableNamePass", false, false);
Changing it fixed the issue:
static RegisterPass<MyPass> X("my-pass", "DPVariableNamePass", false, false);
Maybe it helps

The command line clang/clang++ doesn't work on MacBook Air 2012 Mid with OS X 10.8.5

Originally, I used clang++ with -std=c++11 compile my C++11 code, everything was OK. Recently, I updated the Xcode in AppStore, I compiled the SAME program in command line again, it doesn't work anymore, it shows errors like below:
/usr/include/c++/4.2.1/bits/stl_construct.h:81:38: error: no matching
constructor for initialization of
'std::basic_string'
::new(static_cast(__p)) T1(_value);
It seems the clang++ is using gcc's header files, apparently, gcc's version is very old. However, if I compile the same program by using Xcode, everything is fine.
It seems, at command line , the clang++ can't find the correct header files anymore.
More information, running command clang++ --version, following output:
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0 Thread model: posix
which clang++
/usr/bin/clang++
RESOLVED with option -stdlib=libc++