Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

c++ - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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 build-xcode-toolchain

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


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.


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.


Make sure to check feature status:

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;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...