Hands on std::variant, does it exist? - c++

Considering http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0088r2.html I tried:
#include <variant>
int main (){}
Compiling on macOS I get:
$ g++ -std=c++1z main.cpp
main.cpp:3:10: fatal error: 'variant' file not found
#include <variant>
^
1 error generated.
Compiler used:
$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.6.0
Where is it?

Related

clang++ missing header <format> in mac os 12 with clang 13

> g++ -g -Wall -std=c++20 -stdlib=libc++ -o nes src/main.cpp
src/main.cpp:3:10: fatal error: 'format' file not found
#include <format>
^~~~~~~~
1 error generated.
> clang++ --version ~/Documents/GitHub/nes
Apple clang version 13.0.0 (clang-1300.0.27.3)
Target: arm64-apple-darwin21.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I tried to update xcode developer tools
How to fix that ?
Where is the std::format header in macos ?

How do I compile with C++98 on MacOS?

I need to use C++98 for university programs, however even when passing the -std=c++98 flag to clang++ or to g++ it still seems to compile with c++11 and does not give errors if I use c++11 features. Here is a simple example:
#include <string>
#include <sstream>
using namespace std;
int main()
{
int i;
string number = "12";
i = stoi(number);
}
My makefile:
all:
clang++ -std=c++98 -c *.cpp
clang++ -o main *.o
clean:
rm -f *.o main
run: clean all
./main
Then I run the command make from Terminal (I tried using clang++ instead of g++ but it yields the same result) and receive the following output:
➜ cppversion make
g++ -std=c++98 -c *.cpp
g++ -o main *.o
➜ cppversion make
clang++ -std=c++98 -c *.cpp
clang++ -o main *.o
➜ cppversion
I believe this code should not have compiled if the -std=c++98 flag was working. How do I force code to compile with c++98?
Here is the version of clang:
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin\
Here is the version of g++:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I have also tried adding the flag -pedantic but it does not fix the problem.
Using the flag -stdlib=libc++ yields the following:
➜ cppversion make
clang++ -stdlib=libstdc++ -std=c++98 -c *.cpp
clang: warning: include path for libstdc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
main.cpp:1:10: fatal error: 'string' file not found
#include <string>
^~~~~~~~
1 error generated.
make: *** [all] Error 1
If I change it to just -stdlib=libc++ then it still compiles:
➜ cppversion make
clang++ -stdlib=libc++ -std=c++98 -c *.cpp
clang++ -o main *.o
➜ cppversion
I found an easy solution: Use homebrew to install gcc and use g++-11 to compile.
Try using -std=c++98 -pedantic.
This should strictly enforce the specific standard.
Disclaimer: This is partly guesswork since I don't have a Mac
From my understanding, clang++ is the default compiler on Mac and I would therefore not be surprised if even g++ uses LLVM:s libc++ and headers by default. std::stoi is unconditionaly declared in the libc++ headers.
If you instead useg++:s libstdc++ toolchain, you will probably get the error you want:
clang++ -stdlib=libstdc++ -std=c++98 -o main main.cpp
I found an easy solution: Use homebrew to install gcc and use g++-11 to compile.

/usr/local/include/string.h:25:10: fatal error: 'plist/Node.h' file not found

Im trying to run a hello world program in C++ in MacOS. Trying to run it using the terminal using command g++ -o test.cpp test
Its giving me the above error. Do i need to reinstall my compiler somehow and if yes then how do i do that?
clang++ and g++ and gcc.
I have xcode already setup.
I've also installed xcode-select --install as i read somewhere.
I've tried all of these to run the program. They have their versions like this
clang++ -v
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
g++ -v
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Is that g++ command a typo or did you try to set the output file as test.cpp?
How did you install g++?
is this the only error you get?
/usr/local/include/string.h:25:10: fatal error: 'plist/Node.h' file not found
What happens if you run
g++ -o test -Wall test.cpp

How to compile #include <experimental/any> for clang on OSX

I am trying to get the #include <experimental/any> to compile in my C++ program on clang OSX
// test.cpp
#include <experimental/any>
int main() {
return 0;
}
Tried following commands/options as learnt from here
clang++ -std=c++14 test.cpp -o test -std=c++1z -stdlib=libc++
clang++ -std=c++1x test.cpp -o test -std=c++1z -stdlib=libc++
clang++ -std=c++1y test.cpp -o test -std=c++1z -stdlib=libc++
clang++ -std=c++1z test.cpp -o test -std=c++1z -stdlib=libc++
But it doesn't compile & complains of the following error:
fatal error: 'experimental/any' file not found
clang++ --version yields following:
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
How can I get #include <experimental/any> to compile?
Should I upgrade clang on my machine?
Is C++17 supported on clang currently as of today? If yes how can get the support for it?
For OSX 10.12.5 (using Xcode Developer tools), we get
> clang++ -v
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
and there is no any in /Library/Developer/CommandLineTools/usr/include/c++/v1/experimental, but only
chrono
optional
string_view
tuple
utility
algorithm
dynarray
ratio
system_error
type_traits
So, it appears that Apple's libc++ does not provide any (there is no any in /Library/Developer/CommandLineTools/usr/include/c++/v1/ either). You must either use GCC or your own clang or boost/any.hpp, all of which you can install via homebrew.
You misspelt it. It's "experimental", not "experimentals".
However, since Clang 4.0, you should just be using <any>.

LLVM Clang 5.0 explicit in copy-initialization error

I'm trying to compile an open source project on OSX that has only been tested on Linux.
$: g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-da
I'm trying to compile with the following command line options
g++ -MMD -Wall -std=c++0x -stdlib=libc++ -Wno-sign-compare -Wno-unused-variable -ftemplate-depth=1024 -I /usr/local/Cellar/boost/1.55.0/include/boost/ -g -O3 -c level.cpp -o obj-opt/level.o
I am seeing several errors that look like this:
./square.h:39:70: error: chosen constructor is explicit in copy-initialization
int strength = 0, double flamability = 0, map<SquareType, int> constructions = {}, bool ticking = false);
The project states the following are requirements for the Linux setup. How can I confirm I'm making that?
gcc-4.8.2
git
libboost 1.5+ with libboost-serialize
libsfml-dev 2+ (Ubuntu ppa that contains libsfml 2: )
freeglut-dev
libglew-dev
The project requires you to compile with gcc-4.8.2 , but your g++ is using clang, which is a different compiler.
Install gcc-4.8.2 or above, and use it to compile your project
$ sudo port install gcc49