my gcc version is the following:
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
while clang
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
but the following code doesn't compile:
#include <thread>
#include <vector>
int main(){
std::vector<std::jthread> myJThreads;
return 0;
}
I've tried these so far:
g++ -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
clang -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
clang++-12 -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
getting:
error: ‘jthread’ is not a member of ‘std’;
error: no member named 'jthread' in namespace 'std'
how can I fix this?
Related
I have a std::tuple< std::optional<Args> ... >, need to check that every element has a value.
I have implemented it with std::index_sequence. But I'm not sure this is the most efficient solution for fastest compile time.
using data_type = std::tuple< std::optional<Arg_1>,
std::optional<Arg_2>,
//....
std::optional<Arg_n> // where n > 40
>;
// My solution.
template <size_t ... indexes>
bool has_value_all_elements_impl(const data_type& tuple_data, std::index_sequence<indexes ...> ) {
//I assume there O(n) lookup for each index in compile time.
// So total O(n^2) lookup for tuple ?
return (std::get<indexes>(tuple_data).has_value() && ... ) ;
}
bool has_value_all_elements(data_type const& tuple_data)
{
return has_value_all_elements_impl(
tuple_data, std::make_index_sequence<std::tuple_size<data_type>::value>{});
}
Is there a more efficient O(n) algorithm for such problem?
Or is my solution already O(n) ?
I decided answer my own question.
Seems #chronial comment is right.
source code on godbold - Seems, it depends STL implementation. GCC stdlibc++ increased compile time is not linear. libc++ increased compile time is linear.
GCC:
$ g++ --version
g++ (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ time g++ -DARG_SIZE=10 -Wall -o "example" "example.cpp" -std=c++17 -lpthread -Wall -Wextra -pedantic -O2
real 0m0.431s
user 0m0.384s
sys 0m0.047s
$ time g++ -DARG_SIZE=20 -Wall -o "example" "example.cpp" -std=c++17 -lpthread -Wall -Wextra -pedantic -O2
real 0m0.507s
user 0m0.461s
sys 0m0.046s
$ time g++ -DARG_SIZE=40 -Wall -o "example" "example.cpp" -std=c++17 -lpthread -Wall -Wextra -pedantic -O2
real 0m0.737s
user 0m0.666s
sys 0m0.071s
$ time g++ -DARG_SIZE=80 -Wall -o "example" "example.cpp" -std=c++17 -lpthread -Wall -Wextra -pedantic -O2
real 0m2.030s
user 0m1.882s
sys 0m0.148s
$ time g++ -DARG_SIZE=160 -Wall -o "example" "example.cpp" -std=c++17 -lpthread -Wall -Wextra -pedantic -O2
real 0m23.532s
user 0m23.216s
sys 0m0.312s
$ time g++ -DARG_SIZE=320 -Wall -o "example" "example.cpp" -std=c++17 -lpthread -Wall -Wextra -pedantic -O2
real 10m40.392s
user 10m37.992s
sys 0m2.067s
CLang:
$ clang++ --version
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ time clang++ -DARG_SIZE=10 -Wall -o "example" "example.cpp" -std=c++17 -stdlib=libc++ -lpthread -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
data_type d;
^
1 warning generated.
real 0m1.056s
user 0m0.667s
sys 0m0.066s
$ time clang++ -DARG_SIZE=20 -Wall -o "example" "example.cpp" -std=c++17 -stdlib=libc++ -lpthread -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
data_type d;
^
1 warning generated.
real 0m0.816s
user 0m0.764s
sys 0m0.048s
$ time clang++ -DARG_SIZE=40 -Wall -o "example" "example.cpp" -std=c++17 -stdlib=libc++ -lpthread -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
data_type d;
^
1 warning generated.
real 0m1.455s
user 0m0.985s
sys 0m0.095s
$ time clang++ -DARG_SIZE=80 -Wall -o "example" "example.cpp" -std=c++17 -stdlib=libc++ -lpthread -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
data_type d;
^
1 warning generated.
real 0m1.565s
user 0m1.484s
sys 0m0.067s
$ time clang++ -DARG_SIZE=160 -Wall -o "example" "example.cpp" -std=c++17 -stdlib=libc++ -lpthread -Wall -Wextra -pedantic -O2
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
data_type d;
^
1 warning generated.
real 0m3.172s
user 0m2.410s
sys 0m0.080s
$ time clang++ -DARG_SIZE=320 -Wall -o "example" "example.cpp" -std=c++17 -stdlib=libc++ -lpthread -Wall -Wextra -pedantic -O2 -fbracket-depth=400
example.cpp:36:15: warning: unused variable 'd' [-Wunused-variable]
data_type d;
^
1 warning generated.
real 0m4.576s
user 0m4.337s
sys 0m0.238s
I am attempting to compile a program that uses ranged based for loops, and a couple other features available only in c++11 and above. When I attempt to compile the program using a makefile in the terminal, I get this error:
error: range-based ‘for’ loops are not allowed in C++98 mode
and some warnings:
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
What's annoying is that this has happened before, but it just resolved itself. However, this time it has not resolved itself.
Here's my makefile:
main: main.o
g++5 -std=c++11 -Wall -Werror -g *.cpp -o lab4
Here are some examples of things I have tried to change in the makefile, but did not work.
g++-5 -std=c++11 -Wall -Werror -g *.cpp -o lab4
g++5 -std=gnu++11 -Wall -Werror -g *.cpp -o lab4
g++5 -std=c++0x -Wall -Werror -g *.cpp -o lab4
g++ -std=c++11 -Wall -Werror -g *.cpp -o lab4
g++ -std=gnu++11 -Wall -Werror -g *.cpp -o lab4
All of the previous examples result in similar warnings and errors. What can I do to fix the problem?
I am just curious if the position of the standard selection switch (-std=c++11 for my case) is relevant in g++ command line or not. The reason is that the following:
g++ -ftest-coverage -fprofile-arcs -std=c++11
-ansi -fpermissive -finline-functions -Wno-long-long
-fvisibility-inlines-hidden -m64 -Wall -Wextra
-g -o CMakeFiles/common.dir/cryptoclass.cpp.o
-c /home/work/common/cryptoclass.cpp
does not compile, while the following:
g++ -ftest-coverage -fprofile-arcs
-ansi -fpermissive -finline-functions -Wno-long-long
-fvisibility-inlines-hidden -m64 -Wall -Wextra
-g -o CMakeFiles/common.dir/cryptoclass.cpp.o
-std=c++11 -c /home/work/common/cryptoclass.cpp
does compile. The only change is that the -std=c++11 was moved to the end of the switches.
g++ gives the following warning:
error: #error This file requires compiler and
library support for the ISO C++ 2011 standard.
This support is currently experimental, and must
be enabled with the -std=c++11 or -std=gnu++11 compiler options.
Version:
g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
As per documentation, -ansi option enables the c++-98/c++-03 standard.
If you set multiple standard options, the latter option overrides the former. Same applies to other mutually exclusive options such as optimization levels.
What are the compiler/linker requirements for using pthread library with clang in OS X.
With GCC i know that using -pthread set the appropriate compiler/linker options, but i unsure about OS X with clang.
air:~ jose$ clang++ -c test.cpp -pthread
air:~ jose$ clang++ -o test -pthread test.o
clang: warning: argument unused during compilation: '-pthread'
air:~ jose$ g++ -c test.cpp -pthread
air:~ jose$ g++ -o test -pthread test.o
clang requires -pthread when compiling but not when linking. This is annoying, but it is observed behavior:
$ clang -c x.cpp
$ clang -pthread -c x.cpp
$ clang -o x x.o
$ clang -pthread -o x x.o
clang: warning: argument unused during compilation: '-pthread'
$
$ clang --version
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
$
I get a compile error when trying to create an object file from a compiled source file. I am using the header which came with c++11. I am also using a c++ pattern recognition library with several other includes.
All I did was add #include <thread> to my rbm_test.cc source file.
My compile command:
g++ -std=c++11 -O3 -DQUIET -fPIC -pthread -ansi -pedantic -DARCH_INTEL -Wall -W -Wchar-subscripts -Wpointer-arith -Wcast-qual -Wwrite-strings -Wconversion -Wno-old-style-cast -Wctor-dtor-privacy -Wnon-virtual-dtor -I../src -I../.. -DPATREC -D_UNIX_ -o rbm_test.o -c ../src/rbm_test.cc
The compile error I get is:
error: #error This file requires compiler and library support for the
ISO C++ 2011 standard. This support is currently experimental, and
must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
Strangely, when I compile the following code example with
g++ -std=c++11 -pthread -c main.cpp -o main.o
then I have no error.
Here Is main.cpp
#include <iostream>
#include <thread>
void f1()
{
std::cout << "Thread executing\n";
}
int main()
{
std::thread t1(f1);
std::thread t2(f1);
t1.join();
t2.join();
}
Is it possible that some of the compile flags are conflicting when I try to compile rbm_test.cc?
The -ansi flag conflicts with the -std=c++11 flag. -ansi is equivalent to -std=c++98. Removing the -ansi flag solves the problem.