clang not recognize GUARDED_BY - c++

clang version
Debian clang version 11.0.1-2
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;#m64
Selected multilib: .;#m64
my code here:
std::mutex m_;
int s GUARDED_BY(m_);
int main(int argc, char** argv){
s = 0;
std::cout << s << std::endl;
return 0;
}
compile command:clang -c -Wthread-safety -std=c++14 main.cpp
compile error:
main.cpp:67:6: error: expected ';' after top level declarator
int s GUARDED_BY(m_);
^
;
1 error generated.
is my clang version too low?

Related

std::ifstream eof difference between libc++ and libstdc++11

I have got this code:
#include <fstream>
#include <vector>
int main() {
const std::string path = "/tmp/some_random_path"; // this file do not exist before running of program
std::ofstream file_output(path);
std::ifstream file_input(path, std::ios::in | std::ios::binary);
file_input.peek();
if (!file_input.eof()) {
return 1;
}
std::vector<char> data = {'a', 'b', 'c'};
file_output.write(data.data(), static_cast<long>(data.size()));
file_output.flush();
file_input.clear();
file_input.peek();
if (file_input.eof()) {
return 2; // libc++
}
return 0; // libstdc++11
}
My specs:
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal
clang++ -v
Ubuntu clang version 15.0.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;#m64
Selected multilib: .;#m64
libstdc++11 compile command: clang++ main.cpp
libc++ compile command: clang++ -stdlib=libc++ main.cpp
I expect success return code for both libc++ and libstdc++11, but libc++ eof flag is not updated. Is it valid?

Apple's clang can't use <=> with std::tuple

The following compiles fine with GCC and clang on on godbolt, but on my MacBook, in Xcode 14 it dies:
#include <iostream>
#include <compare>
#include <tuple>
using std::cout; using std::tuple; using std::endl;
int main() {
tuple<float, float> tuplee = {1.0,2.0};
tuple<float, float> tuploo = {3.0,4.0};
cout << (tuplee < tuploo) << endl;
auto res = (tuplee <=> tuploo);
cout << (res < 0) << endl;
return 0;
}
The error is:
invalid operands to binary expression ('std::tuple<float, float>' and 'std::tuple<float, float>')
It points to the <=> on the tuples. Do you think it's a bug in Apple's clang, or am I missing something?
Command line on my MacBook:
% clang++ --version
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
% clang++ -std=c++2b main.cpp
main.cpp:11:21: error: invalid operands to binary expression ('tuple<float, float>' and 'tuple<float, float>')
cout << (tuplee <=> tuploo) << endl;
~~~~~~ ^ ~~~~~~
1 error generated.
I think it is a bug. The bug was fixed in llvm (relevant change). But by checking the tuple header in Macos SDK, one can find apple do not implement <=> for tuple.
The bug also affects arm64 variants of Macos. Clang version on my mac:
➜ test clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
PS. Homebrew llvm#14 compiles fine. Just do not use apple clang

unable to compile c++ code in macbook due to __GLIBC__ issue

g++ -std=c++11 -Wall -g threads.cpp -o threads.out
In file included from threads.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:401:32: error: use of undeclared identifier '_ISspace'
static const mask space = _ISspace;
^
Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:402:32: error: use of undeclared identifier '_ISprint'
static const mask print = _ISprint;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:403:32: error: use of undeclared identifier '_IScntrl'
static const mask cntrl = _IScntrl;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:404:32: error: use of undeclared identifier '_ISupper'
static const mask upper = _ISupper;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:405:32: error: use of undeclared identifier '_ISlower'
static const mask lower = _ISlower;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:406:32: error: use of undeclared identifier '_ISalpha'
static const mask alpha = _ISalpha;
can you please help me to resolve this __locale issue while compiling the c++ code.
#include <iostream>
#include <thread>
using namespace std;
void fun(void)
{
cout << "Vaule " << 10 << endl;
}
int main()
{
thread t1(fun);
thread t2(fun);
return 0;
}
compiling command:
g++ -std=c++11 -Wall -g thread.cpp -o thread.out
Two things fix the problem you're having.
Thing the first, add the compiler option -pthread. My compile command: clang++ -Wall -Wextra -std=c++11 -pthread main.cpp
Thing the second, join your threads before ending the program.
t1.join();
t2.join();
Something it does not fix, is that your std::cout statement will likely be jumbled because the threads simply dump their data into the single stream as they please. For an example, my output was the following:
Vaule Vaule 10
10
In order to fix that, you'll likely need to place a lock(mutex) around the std::cout statement.
As I said in my comment, I do not recommend using g++ unless you installed it yourself. The command you're using is an alias that doesn't behave, because of some text you left out.
❯ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Contrast with clang++
❯ clang++ --version
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Of note in the g++ section is the 'Configured with' line. It uses a Standard Library from gcc 4.2.1, which is pre-C++11. You should not have left that information out.

Linking issues with {fmt} 6.2.1-3 + clang 10.0.0 when trying to use ostream<< operator objects

after having an issue with std::thread's get_id() and printing via fmt (even though I include fmt/ostream.h) I figured I'd put together this simple fmt_test.cpp file (based off of this):
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <iostream>
class date {
int m_year, m_month, m_day;
public:
date( int year, int month, int day ):
m_year ( year ),
m_month ( month ),
m_day ( day )
{}
friend std::ostream & operator<<( std::ostream &out, date const &d ) {
return out << d.m_year << '-' << d.m_month << '-' << d.m_day;
}
};
int main() {
auto s = fmt::format( "The date is {}", date(2012, 12, 9) ); // s == "The date is 2012-12-9"
fmt::print( "{}", s );
return 0;
}
The version of fmt available in pacman is 6.2.1-3 which is the one I'm using. I'm compiling with clang 10.0.0 with the following arguments:
clang++ fmt_test.cpp -std=c++20 -stdlib=libc++ -O0 -g -o testbin -lfmt -v
And the output I get is:
clang version 10.0.0
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /sbin
Found candidate GCC installation: /sbin/../lib/gcc/x86_64-pc-linux-gnu/10.1.0
Found candidate GCC installation: /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/10.1.0
Selected GCC installation: /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0
Candidate multilib: .;#m64
Candidate multilib: 32;#m32
Selected multilib: .;#m64
"/usr/bin/clang-10" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name fmt_test.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -resource-dir /usr/lib/clang/10.0.0 -internal-isystem /usr/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /usr/lib/clang/10.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -std=c++20 -fdeprecated-macro -fdebug-compilation-dir /home/falk/Code/Sandbox -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -faddrsig -o /tmp/fmt_test-befb6c.o -x c++ fmt_test.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../include/c++/v1
/usr/local/include
/usr/lib/clang/10.0.0/include
/usr/include
End of search list.
"/sbin/ld" -pie --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o testbin /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64/Scrt1.o /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64/crti.o /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/crtbeginS.o -L/sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0 -L/sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64 -L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib /tmp/fmt_test-befb6c.o -lfmt -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/crtendS.o /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64/crtn.o
/sbin/ld: /tmp/fmt_test-befb6c.o: in function `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > fmt::v6::internal::grouping<char>(fmt::v6::internal::locale_ref)':
/usr/include/fmt/format.h:855: undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > fmt::v6::internal::grouping_impl<char>(fmt::v6::internal::locale_ref)'
/sbin/ld: /tmp/fmt_test-befb6c+.o: in function `void fmt::v6::internal::format_value<char, date>(fmt::v6::internal::buffer<char>&, date const&, fmt::v6::internal::locale_ref)':
/usr/include/fmt/ostream.h:102: undefined reference to `std::__1::locale fmt::v6::internal::locale_ref::get<std::__1::locale>() const'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea what might be wrong? I've tried using other arguments like std=c++17 and moving around the argument position of -lfmt, but to no avail. :(
Another example, ´fmt_test2.cpp`:
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <iostream>
#include <thread>
void foo() { return; }
int main()
{
std::thread t1( foo );
std::thread t2( foo );
// these two lines work without problems:
// std::cout << "t1's id: " << t1.get_id() << '\n'
// << "t2's id: " << t2.get_id() << '\n';
// these two lines result in linking errors:
fmt::print( "t1's id: {}\n", t1.get_id() );
fmt::print( "t2's id: {}\n", t2.get_id() );
t1.join();
t2.join();
}
The build output of clang++ -pthread -std=c++20 -stdlib=libc++ -lfmt -O0 -g fmt_test2.cpp -o testbin is:
/sbin/ld: /tmp/fmt_test-d2bca4.o: in function `void fmt::v6::internal::format_value<char, std::__1::__thread_id>(fmt::v6::internal::buffer<char>&, std::__1::__thread_id const&, fmt::v6::internal::locale_ref)':
/usr/include/fmt/ostream.h:102: undefined reference to `std::__1::locale fmt::v6::internal::locale_ref::get<std::__1::locale>() const'
/sbin/ld: /tmp/fmt_test-d2bca4.o: in function `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > fmt::v6::internal::grouping<char>(fmt::v6::internal::locale_ref)':
/usr/include/fmt/format.h:855: undefined reference to `std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > fmt::v6::internal::grouping_impl<char>(fmt::v6::internal::locale_ref)'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
This looks like a problem with the pacman package because your example works with the stock version of {fmt} 6.2.1: https://godbolt.org/z/14dEfx. I recommend checking which symbols the pacman version of libfmt exports.

Threads in c++, cannot use parameters

I am trying to start a thread t:
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
void function(int p1, int p2, int p3){
std::cout<<p1<<p2<<p3<<std::endl;
}
int main(int argc, char const *argv[]) {
std::cout<<"starting"<<std::endl;
std::thread t(function, 1, 2, 3);
std::cout<<"created thread"<<std::endl;
t.join();
std::cout<<"end"<<std::endl;
return 0;
}
My compiler tells me this:
doesntwork.cpp:12:15: error: no matching constructor for
initialization of 'std::thread'
std::thread t(function, 1, 2, 3);
^ ~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:408:9: note:
candidate constructor template not viable: requires single
argument '__f', but 4 arguments were provided
thread::thread(_Fp __f)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:289:5: note:
candidate constructor not viable: requires 1 argument, but 4
were provided
thread(const thread&);
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:296:5: note:
candidate constructor not viable: requires 0 arguments, but
4 were provided
thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
^
1 error generated.
In the first case it tells me that for the thread t, there is no constructor that can use more than 1 parameter, while if I just remove the arguments (p1, p2, p3) it doesn't work either because I am not passing any argmuent....
Compiler information:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
built command used: g++ doesntwork.cpp -o doesntwork.out
Is there something different you have to do when compiling with threads? Am I missing something very obvious?
On macOS, g++ (from Xcode: Version 10.0 (10A255)) is aliased to clang which by default does not work with c++11 threads. To solve the problem you have to use the -std=c++11 switch.
Example:
g++ -std=c++11 fileToCompile.cpp -o outputFile.out
This should let you compile c++ code using c++11 threads.
Thank you to #M.M for providing the answer above in the comments.