std::integral not found in clang13 c++20 error - c++

i try to learn c++20 concepts my compiler version is "Clang 13" i try to compile very simple code block but i got errors following.
"error: no type named 'floating_point' in namespace 'std'"
"error: no type named 'integral' in namespace 'std'"
i try gcc 11.2 version also but i got same error.
my build commands is following;
clang++ -Wall -Wextra -std=c++20 -g -Iinclude -Llib src/main.cpp -o bin/main
clang++ -Wall -Wextra -std=c++2a -g -Iinclude -Llib src/main.cpp -o bin/main
Code
#include <iostream>
#include <concepts>
#include <vector>
auto addUnconstrained = [](auto fir, auto sec){ return fir + sec; };
std::floating_point auto addConstrained(std::integral auto fir,
std::floating_point auto sec){
return fir + sec;
}
int main() {
std::cout << "Hello Easy C++ project!" << std::endl;
std::cout << addUnconstrained(2000, 11.5); // 2011.5
std::cout << addConstrained(2000, 11.5); // 2011.5
}
i will be very appreciate if anyone help me.
thanks

output of compiling with "-v" following link. gist.github.com/RamazanDemirci/c43db2743792a5afa5b297456c03a423
Apple clang version 13.0.0 (clang-1300.0.29.30)
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
You were using the Apple Clang. Its implementation for concepts is not completed.
after brew install llvm command i got following output "Warning: llvm 13.0.1_1 is already installed and up-to-date."
If you have installed llvm-13 from Homebrew, run brew info llvm to find where it is installed. For my case, the command outputs
If you need to have llvm first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc
Then I can use /opt/homebrew/opt/llvm/bin/clang++. I also prepended the path into $PATH so that I can use clang++ directly to run it instead of Apple Clang.
Run with --version to check if it is regular/upstream Clang(the one from homebrew) or Apple Clang.
❯ /opt/homebrew/opt/llvm/bin/clang++ --version
Homebrew clang version 13.0.1
Target: arm64-apple-darwin21.4.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

Related

C++ 20 modules in Xcode 14.0.1

I would like to experiment with newer C++ features from (at least) C++20 and I'm new to C++ development on macOS in particular.
How do I enable importing of library headers in the 'module way'?
import <iostream>;
using namespace std;
int main(int argc, const char * argv[]) {
cout << "Hello, World!\n";
return 0;
}
The build errors are
Use of undeclared identifier 'iostream'
Use of undeclared identifier 'std'
I was looking at this answer and tried installing llvm and I suppose that I could run the provided commands from the terminal, but I have not tried. Is there a way to make Xcode issue the commands provided in that answer (see below)? Where would I enter them in XCode? And what do they do? I'd like to avoid thinking about terminal commands and just have something that works.
/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -c -Xclang -emit-module-interface mathlib.cpp -o mathlib.pcm
/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -fmodules -c -fprebuilt-module-path=. main.cpp -o main.o
/opt/homebrew/opt/llvm/bin/clang++ -std=c++2a -fmodules -o main main.o *.pcm
I saw that when making a new C++ project, Xcode defaults to the Apple Clang compiler and GNU++20 [-std=gnu++20] C++ Language dialect. There is also another option C++20 [-std=c++20] (what's the difference?) which I also tried, resulting in the same errors.
FYI, when I do clang -v I get
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
And a general question about other build systems such as CMake, which I discovered during my research for C++ compilation on mac. After making a CMake project, is it possible to use the regular IDE tools such as code suggestions and debugging?

How to resolve `clang: error: unsupported option '-fsanitize=leak'` on apple devices

I am trying to run a simple leak check program.
#include <iostream>
int main()
{
double *ptr = new double(3.14);
}
using the command
g++ -g -fsanitize=leak -o main main.cpp
and I get the following error:
clang: error: unsupported option '-fsanitize=leak' for target 'x86_64-apple-darwin20.1.0'
I stopped using the clang that comes with Xcode and installed clang/LLVM using homebrew.
$ which clang++
/usr/local/opt/llvm/bin/clang++
clang++ --version
clang version 11.0.0
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
EDIT: When I was using apple clang, g++ used to default to clang++. Apparently that changed when I installed llvm/clang. Thanks to #cigien for pointing it out. g++ still uses default to the compiler that that comes with Apple clang.
g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
According to this answer you should use:
g++-10 -g -fsanitize=leak -o main main.cpp (in this cases (with flag leak) there is no leak message for me ./main, but it compile) then it should be:
g++-10 -fsanitize=address -g main.cpp ; ASAN_OPTIONS=detect_leaks=1 ./a.out and it detect leak well.
Note, that the correct path of brew installed g++ in MacOS is:
$ which g++-10
> /usr/local/bin/g++-10
--
$ which g++
> /usr/bin/g++ //this is pseudonym of clang
The same for gcc-10 (10 is my current version. You should use your version instead of that)
If you use CMakeLists.txt file you will configure it like this:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]")
# leak sanitizer_name not works for me. should be address
And should execute cmake command like this:
cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc-10 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-10 ..
And then ASAN_OPTIONS=detect_leaks=1 ./a.out
Note, that if you open */CMakeFiles/3.18.4/CMakeCXXCompiler.cmake file you will observe the compiled info, and now it will be g++.

Linking silicon webserver with libmicrohttpd backend

I'm trying to compile the silicon webserver hello world example on FreeBSD 10.2 RELEASE using clang++38. The framework uses c++14. I have installed libmicrohttpd.
When I try to compile the program using
clang++38 -O2 -Wall -std=c++14 -I/usr/local/include -L/usr/local/lib -lmicrohttpd -o sws01 sws01.cpp
I get the error
In file included from sws01.cpp:2:
/usr/local/include/silicon/backends/mhd.hh:158:22: error: use of undeclared identifier 'MHD_http_unescape'
value.resize(MHD_http_unescape(&value[0]));
The sws01.cpp:
#include <silicon/api.hh>
#include <silicon/backends/mhd.hh>
#include "symbols.hpp"
using namespace sl;
using namespace s;
auto hello_api = http_api(
GET / _hello = [](){ return D(_message = "Hello from Silicon Webserver!"); }
);
int main() {
sl::mhd_json_serve(hello_api, 9876);
}
I tried to apply this SO thread answer but -Wl and specifying /usr/local/lib/libmicrohttpd.a like
clang++38 -O2 -Wall -std=c++14 -I/usr/local/include /usr/local/lib/libmicrohttpd.a -o sws01 sws01.cpp
did not work either.
Works on os x using xcode 7.3.
Turns out the libmicrohttpd-server that ships with the FreeBSD ports-system is ver. 0.9.37 and libmicrohttpd.so does not have MHD_http_unescape() but libmicrohttpd.a does. The newest is currently 0.9.48. Replacing the ports-version with this solves my compilation issue.

GCC Segmentation Fault Mac

I have been having some trouble getting my gcc and g++ compiler to work on my
mac (OSX Yosemite 10.10.2).
I have written up a simple "Hello World" program and even these seem to not
work. The two program that I tried to run are
hello.c
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
hello.cpp
#include <iostream>
int main()
{
std::cout << "Hello World";
}
I can compile the C program using cc hello.c and everything works fine, but
when I do gcc hello.c I get this error
[1] 38508 segmentation fault gcc hello.c
I get a similar error attempting to compile my C++ code
[1] 38596 segmentation fault g++ hello.cpp
I did which gcc and I get /opt/local/bin/gcc and that directory is in my
path.
( /usr/texbin /opt/local/bin /opt/local/sbin /bin /usr/sbin /sbin /usr/local/bin/usr/bin )
So I am confused as to what is happening. I thought I downloaded all of the
Xcode things that I needed. I would like to get gcc and g++ running
properly. I hope that you can help.
Thanks!
It seems that gcc and g++ have to be installed/added to the MAC os.
From your description, I would expect that the wrong version of those tools was installed.
This answer should help.
Be sure to read all the answers to the question before proceeding with a gcc installation.
I had a similar problem where even gcc --versionwas giving me a "Segmentation fault: 11". This is on OSX 10.10.5 with XCode 6.4. After much googling and no solution, I found that clang (Apple's LLVM-based C compiler) is intended to be a compatible replacement for gcc, so I just sym-linked gcc to clang as follows:
whence gcc #=> /usr/local/bin/gcc
whence clang #=> /usr/bin/clang
cd /usr/local/bin
sudo mv gcc gcc_OLD
sudo ln -s /usr/bin/clang /usr/local/bin/gcc
gcc -v
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
Now I am able to successfully compile c-language stuff, like my ruby extensions.

clang -cc1 and system includes

I have the following file foo.cpp:
#include <vector>
struct MyClass
{
std::vector<int> v;
};
It can be successfully compiled with clang (I'm using clang 3.3 on Ubuntu 13.04 32bit):
clang++ -c foo.cpp
Now I want to print AST:
clang++ -cc1 -ast-print foo.cpp
and I've got the following error
foo.cpp:1:10: fatal error: 'vector' file not found
#include <vector>
^
struct MyClass {
};
1 error generated.
It looks like clang++ -cc1 doesn't know about system include files etc.
I'm wondering how to set up includes for clang++ -cc1?
You need to set up the right include paths.
on my system I added
-I/usr/include/i386-linux-gnu/c++/4.8 -I/usr/include/c++/4.8
to the compiler flags. The first one was so that it could find
bits/c++config.h
Of course the 4.8 is due to the fact I am using a compiler compatible with g++-4.8
I also added
-std=c++11 -stdlib=libstdc++
as compiler options.
Hope this helps
It's a Frequently Asked Question
#john is correct. For posterity, the relevant portions of the FAQ are (with names tweaked to match the question) :
clang -cc1 is the frontend, clang is the driver. The driver invokes the frontend with options appropriate for your system. To see these options, run:
$ clang++ -### -c foo.cpp
Some clang command line options are driver-only options, some are frontend-only options. Frontend-only options are intended to be used only by clang developers. Users should not run clang -cc1 directly, because -cc1 options are not guaranteed to be stable.
If you want to use a frontend-only option (“a -cc1 option”), for example -ast-dump, then you need to take the clang -cc1 line generated by the driver and add the option you need. Alternatively, you can run clang -Xclang <option> ... to force the driver [to] pass <option> to clang -cc1.
I did the latter (-Xclang) for emitting precompiled headers:
/usr/bin/clang++ -x c++-header foo.hpp -Xclang -emit-pch -o foo.hpp.pch <other options>
^^^^^^^
Without the -Xclang, clang++ ignored the -emit-pch. When I tried -cc1, I had the same problem as the OP — clang++ accepted -emit-pch but didn't have the other options the driver normally provides.