c++20 library support for xcode 12 - c++

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

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"
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;
}

Related

How to compile a Linux LTO-enabled library with Premake 5?

SO wisdom, I'm turning to you. I'm trying to build a 64-bit static lib using LTO with Makefiles and Premake 5 on Ubuntu 16.04 LTS.
Here's the premake script i'm using:
-- premake5.lua
workspace "TestApp"
location "TestApp" -- The directory of generated files - .sln, etc.
configurations { "Debug", "Shipping" }
platforms { "Linux_Static", "Linux_DLL" }
targetdir "TestApp/Build/%{cfg.platform}/%{cfg.buildcfg}"
objdir "TestApp/Build/"
language "C++"
architecture "x86_64"
system "linux"
filter "platforms:*Static"
kind "StaticLib"
filter "platforms:*DLL"
kind "SharedLib"
filter "kind:SharedLib"
defines { "TEST_USE_DLL", "TEST_DLL_EXPORT" }
-- Configuration filters
configuration "*"
flags { "ExtraWarnings", "C++14", "MultiProcessorCompile", "ShadowedVariables", "UndefinedIdentifiers" }
configuration { "Debug" }
symbols "On"
defines { "TEST_DEBUG" }
optimize "Debug"
configuration "Shipping"
defines { "TEST_SHIPPING" }
optimize "Full"
flags { "LinkTimeOptimization" }
-- step 1
--buildoptions "--plugin=$$(gcc --print-file-name=liblto_plugin.so)"
-- step 2
--toolset "clang"
-- step 3
--premake.tools.gcc.ar = "gcc-ar"
-- Projects
project "TestCore"
location "TestApp/Core"
files { "TestApp/Core/*.h", "TestApp/Core/*.cpp" }
includedirs { "TestApp/" }
project "UnitTests"
location "TestApp/Tests"
kind "ConsoleApp"
links { "TestCore" }
objdir "TestApp/Tests/Build/"
files { "TestApp/Tests/UnitTests/*.cpp", "TestApp/ThirdParty/Catch/*" }
includedirs { "TestApp/ThirdParty/Catch", "TestApp/" }
removedefines { "TEST_DLL_EXPORT" }
filter { "platforms:*DLL", "system:linux" }
runpathdirs { "Build/%{cfg.platform}/%{cfg.buildcfg}" }
"Shipping" is the faulty configuration. I also bundled the whole test project in a zip for you to try to reproduce the issue.
The errors I have when compiling the TestCore library are first plugin needed to handle lto object, then plugin /usr/lib/gcc/x86_64-linux-gnu/5/liblto_plugin.so is not licensed under a GPL-compatible license.
What can we do about it ? If you have any knowledge to make it work with GCC, please help.
what you would do to reproduce the GCC errors after extracting the zip:
cd testBreaking
premake5 gmake
cd TestApp
make config=shipping_linux_static TestCore (get the "plugin needed to handle lto object" error)
Uncomment line 37 of premake5.lua to get the "not licensed under a GPL-compatible license" error
Uncomment line 43 to use gcc-ar instead of ar, notice it doesn't work either
Using gcc option -fuse-linker-plugin doesn't help
Some more system info:
ubuntu 16.04 LTS
gcc 5.4, make 4.1, ar 2.26.1
premake 5.0.0-alpha11
I got it working with Clang. Using toolset clang for Shipping configuration (using LLVM 3.9), the library seems to compile fine. But I got another error:
error adding symbols: Archive has no index; run ranlib to add one
I managed to work around this issue by calling ranlib Build/Linux_Static/Shipping/libTestCore.a --plugin /usr/lib/llvm-3.9/lib/LLVMgold.so, then make again.
So it painfully works using Clang.
I read that I could create a specific premake toolset for this kind of thing, because it's recommended replacing all gnu utils with their gcc- counterparts (e.g. gcc-ar instead of ar), but having rapidly tinkered with premake.tools.gcc.ar = "gcc-ar" with no result, I'm not so sure it would help.

Compile c++ code in R does not work anymore

I start saying that i am a newbie in programming and then i am not sure i will be able to explain well my problem.
I had some c++ code i wrote, this code are loaded and used by some R functions.
To compile the code i used the following:
R CMD SHLIB MyCode.cpp
and i loaded the library in R with
dyn.load("MyCOde.so")
Sometimes i built also an R package and i was able to load it into R.
If i do all these stuff on a Mac with mountain lion everything work fine, but now that i switched to mavericks, i have some problems. The R CMD SHLIB MyCode.cpp command works but when i used dyn.load("MyCOde.so") i get the following text:
Errore in dyn.load(paste(dir_function, "MyCOde.so", sep = "")) :
unable to load shared object 'MyCOde.so':
dlopen(MyCOde.so, 6): Symbol not found: __ZNSt8ios_base4InitC1Ev
Referenced from: MyCOde.so
Expected in: flat namespace
in MyCOde.so
Moreover if i try to load the package in R, i get the following
ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
ld: library not found for -lquadmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [MyCode.so] Error 1
Can someone helps me?
Based on the helpful website of:
thecoatlessprofessor
Type this into your terminal shell:
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /
This will create what you need to resume compiling as before.
Since it starts to work I can publish the answer for such a cases.
When you change the compiler and standard libraries - please note that different libraries have different implementation and different standard support. Changing the basement of your system might require total rebuild of your system with the new C++ standard library.
Your libraries are not the exception. So if have the errors in your linker like this:
warning: directory not found for option
'-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
apply next algorithm:
Check whether the directory /usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 still exists. I bet it is not.
Check if you still have the libstdc++ from the missed compiler? Usually if you upgrade the same compiler and the C++ standard library ABI does not change everything should continue to work. If the ABI changed or you switch standard C++ library and compiler - you face the massive system rebuild.
Recompile your library and apps with the new C++ standard library and compiler.

Choosing compiler options based on the operating system in boost-build

Currently I can build my program using boost build in different platforms by setting the toolset and parameters in the command line. For example :
Linux
b2
MacOS
b2 toolset=clang cxxflags="-stdlib=libc++" linkflags="-stdlib=libc++"
Is there a way to create a rule in the Jamroot file to decide which compiler to use based on the operating system? I am looking for something along these lines:
import os ;
if [ os.on-macos ] {
using clang : <cxxflags>"-stdlib=libc++" <linkflags>"-stdlib=libc++c ;"
}
in linux it automatically decides to use gcc but in the mac if I don't specify the clang toolset it will try (without success) to compile it with gcc.
Just for reference, here is my current jamroot (any suggestions also appreciated):
# Project requirements (note, if running on a Mac you have to build foghorn with clang with libc++)
project myproject
: requirements <cxxflags>-std=c++11 <linkflags>-std=c++11 ;
# Build binaries in src
lib boost_program_options ;
exe app
: src/main.cpp src/utils src/tools boost_program_options
;
How abou using a Jamroot? I have the following in mine. It selects between two GCC versions on Linux, depending on what's in an environmen variable, and chooses vacpp on AIX.
if [ os.name ] = LINUX
{
switch [ modules.peek : ODSHOME ]
{
case *gcc-4* : using gcc : 4.4 : g++-4.4 ;
case *gcc-3.3* : using gcc : 3.3 : g++-3.3 ;
case * : error Only gcc v4 and gcc v3.3 supported. ;
}
}
else if [ os.name ] = AIX
{
using vacpp ;
}
else
{
error Only Linux and AIX supported at present. ;
}
After a long time I have found out that there is really no way (apart from very hacky) to do this. The goal of Boost.Build is to let the toolset option for the user to define.
The user has several ways to specify the toolset:
in the command line with --toolset=gcc for example
in the user configuration by setting it in the user-config.jam for all projects compiled by the user
in the site configuration by setting it in the site-config.jam for all users
the user-config.jam can be in the user's $HOME or in the boost build path.
the site-config.jam should be in the /etc directory, but could also be in the two locations above.
In summary, setup your site-config or user-config for a pleasant experience, and write a nice README file for users trying to compile your program.
Hope this helps someone else.

Type 'std::default_random_engine' could not be resolved

I want to generate some random double numbers using default_random_engine and uniform_real_distribution in header random.
I use Eclipse for C/C++ & MinGW to build my project.
Eclipse version: 4.2.1
Eclipse CDT C/C++ Development Tools version: 8.1.1.201209170703
Eclipse CDT GCC Cross Compiler Support version: 1.1.0.201209170703
MinGW version: 4.6.2(checked using "gcc -v")
When I type std::default_random_engine in the editor, Eclipse prompts me that "Type 'std::default_random_engine' could not be resolved".
I have already configured my project to support C++11 features
Open Project Properties->C/C++ Build ->Settings->Tool Settings->GCC C++ Compiler->Miscellaneous->Other Flags. Put "-std=c++0x" at the end
Project Properties->C/C++ General->Preprocessor Include Paths, Macros->[Providers] tab->your Built-in Compiler Settings provider (toolchain dependent). Click on "Workspace Settings" link which gets you to "Settings" property page, select [Discovery] tab and your provider again. There is "Command to get compiler specs", add "-std=c++0x" in there.
Then I wrote a list initialized vector and a range for to test the support of the C++11, the code work fine.
vector<int> ivec = {1, 2, 3};
for (int i : ivec)
cout << i << " ";
cout << endl;
What's wrong with the "std::default_random_engine", what should I do to fix this?
UPDATE: It's been a long time since I posted the original answer and it has become outdated. I double-checked today (Mar 15, 2014): in Eclipse Kepler (Build id 20130614-0229) it is sufficient to
add under Project > Properties > C/C++ Build > Settings then on the Tool Settings tab GCC C++ Compiler > Miscellaneous the -std=c++11 flag,
then under Window > Preferences > C/C++ > Build > Settings on the Discovery tab chose CDT GCC Built-in Compiler Settings and add the -std=c++11 flag to Command to get compiler specs. On my machine it looks like this after the change:
${COMMAND} -E -P -v -dD -std=c++11 "${INPUTS}"
clean and rebuild both your project and your index (Project > C/C++ Index > Rebuild) as Eclipse tends to cache error messages and show them even though they are gone after changing the settings.
This works on my machine for sure. If it doesn't on yours, then you might want to give a shot to this: C++11 full support on Eclipse although I am neither sure about the correctness of this approach nor was it necessary to do it on my machine. As of March 7, 2014 users claim that it helped them whereas the above approach didn't.
The original post, now outdated:
It seems to be a false error from the IDE.
Click on the project properties, then C/C++ General > Code Analysis > Syntax and Semantic Errors and deselect Type cannot be resolved.
I also had to disable a bunch of other Syntax and Semantic Errors, such as Invalid arguments, Invalid overload, Symbol is not resolved, etc. in my own projects. These bogus errors come from Codan.
(You might have to add __GXX_EXPERIMENTAL_CXX0X__ to your defines / preprocessor macros, not sure about this one though.)

playing with GCC 4.6 on windows

I am very pleased to find out that GCC 4.6 supports the range-based for loop. I found an experimental release of MinGW 4.6 on xvidvideo.ru, is that a well-known, reliable website? What other options do I have (besides compiling myself from source code)?
I wanted to try out GCC 4.7 using the latest Code::Blocks under Windows 7.
Here's how I did it for myself, YMMV:
I downloaded the latest Equation GCC file at:
ftp://ftp.equation.com/gcc/ and installed it under the directory C:\gcc\ on my local machine. The installer makes the necessary changes to the path environment variable. Logging off and on will pick them up.
I downloaded the Code::Blocks latest nightly build at: http://forums.codeblocks.org/index.php?board=20.0
and followed the setup instructions.
After following the setup instructions (including about the needed DLL files), and starting C::B for the first time;
I chose 'GNU GCC Compiler', and 'Set as default' for the 'Compilers auto-detection' window.
Under the 'Settings > Compiler... > Compiler settings' tab:
I ticked the 'Have g++ follow the coming C++0x ISO C++ language standard [-std=c++0x]' checkbox on.
Under the 'Settings > Compiler... > Toolchain executables' tab:
I changed the 'Compiler's installation directory' entry field to C:\gcc\bin\.
I changed the names of these files physically located in the C:\gcc\bin\ directory
i686-pc-mingw32-gcc.exe -=to=- mingw32-gcc.exe
i686-pc-mingw32-g++.exe -=to=- mingw32-g++.exe
make.exe -=to=- mingw32-make.exe
to match the listed name requirements in Code::Blocks. You can simply browse to set the correct files (I just personally preferred renaming to match C::B's entries).
If everything went correctly, you should be able to create this program:
#include <iostream>
#include <vector>
int main() {
using namespace std;
vector<int> my_vec = { 1, 2, 3, 4, 5 };
for (auto x : my_vec) {
cout << x << endl;
}
}
and run it OK under Code::Blocks with F9.
Thanks to everyone for all the excellent work put into bringing this great new language to us. Happy C++0x computing!
Bud Alverson
(sorry for the very basic nature of this post) :)
I'm not really familiar with the site you linked as it's in Russian. The only other place I've found that offers current snapshots of GCC's build is from Equation Solution. I downloaded gcc4.5.1 from there and it's been working fairly well for me. I haven't tried the 4.6.x release yet however. Rumor has it that gcc 4.6.x is slower than its predecessors.
Please do report back what kind of results you're seeing if you do decide experimenting. I'm curious about what improvements they've done in the 4.6.x series.