Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++20 library support for xcode 12

can i use the c++20 library with xcode 12? (xcode 12 beta 5, with clang version 10.0.0).

so in xcode build settings, instead of

// in xcode build settings, "c++ standard library"
CLANG_CXX_LIBRARY = libc++

maybe use something like (does not work => clang err: invalid library name)

CLANG_CXX_LIBRARY = libc++20 // eg. libc++20 & libc++2a are invalid names

i have already set (works by providing c++20 language support, but does not provide c++20 library support)

// in xcode build settings, "c++ language dialect"
CLANG_CXX_LANGUAGE_STANDARD = c++2a // ok but does not provide c++20 library

im aware that the c++20 library is not yet complete/officially released.

question:

do you know of any (easy) way to use the (preliminary) c++20 library with xcode 12?

thanks

like image 353
mrchance Avatar asked Sep 01 '20 13:09

mrchance


1 Answers

There can be easier ways, or I might have done something redundant (let me know what can be removed), but here's a surefire way.

If you download the LLVM binaries from LLVM releases, do Step 1 - a and not Step 1 - b.

Step 1 - a

Download the LLVM + Clang binaries of your choice.

  • https://releases.llvm.org/
  • https://github.com/llvm/llvm-project/releases

Copy the toolchain from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain and paste it in ~/Library/Developer/Toolchains.

Right click -> Show package Contents.

  • Change the identifier in ToolchainInfo.plist file to what you want "MyAmazingToolchain".
  • Replace all the bin include lib libexec folders with what you got from LLVM.

Move on to Step 2.

Step 1 - b

Build llvm with

cmake -G "Sublime Text 2 - Ninja" -DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_PROJECTS=“clang;libcxx;libcxxabi” -DCMAKE_CXX_STANDARD=17 -DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_CCACHE_BUILD=ON -DLLVM_CREATE_XCODE_TOOLCHAIN=ON \
-DCMAKE_INSTALL_PREFIX="easy to clean location"\
-DLLVM_ENABLE_RTTI=OFF \
../llvm
  • http://clang.llvm.org/get_started.html
  • https://llvm.org/docs/CMake.html

libcxxabi is required or linking libcxx will fail with:

ld: library not found for -lcxxabi_shared clang: error: linker command failed with exit code 1 (use -v to see invocation)

DLLVM_CCACHE_BUILD requires https://ccache.dev (use brew if you wish). First build will be very slow. Rebuilds will be faster.


After the above is done and ninja compiles around 3000 files, run

 ninja install all 
 ninja install-xcode-toolchain

Find the created toolchain in location you chose above/Toolchains. Copy it to ~/Library/Developer/Toolchains/


Step 2

If Xcode is open, close it and reopen. In Xcode app menu > Toolchains > choose the new one, llvm12git.

Create a new c++ project normally and go to its project's build settings.

Search for COMPILER_INDEX_STORE_ENABLE or Enable index-while-building functionality and set it to "No". Otherwise, build fails with "unrecognised option" about indexing.


Step 3

Change "C++ language dialect" to "c++20" or "c++2a"

enter image description here

Build the project normally. However, warnings may not go away while the code successfully builds due to indexing disabled. :( Adding header search path helps with warnings.

  • Adding system header search path to Xcode

Make sure to check feature status:

  • http://clang.llvm.org/cxx_status.html
  • https://en.cppreference.com/w/cpp/20

Code I tested:

#include <compare>
#include <concepts>

struct Aggr {
  int i;
  char c;

  auto operator<=>(Aggr const &) const = default;
};
struct A {
  int x;
  int y;
  int z;
};
int main()
{

  // A a{.y = 2,.x = 1};  // error; designator order does not match declaration
  // order
  A b{.x = 1, .z = 2};  // ok, b.y initialized to 0
  return 0;

}
like image 75
puio Avatar answered Sep 21 '22 07:09

puio