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?
Related
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
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?
My system: macOS Catalina(10.15.4)
Brew config
Clang: 11.0 build 1103
Git: 2.21.0 => /usr/local/bin/git
Curl: 7.64.1 => /usr/bin/curl
Java: 1.8.0_101
macOS: 10.15.4-x86_64
CLT: 11.4.1.0.1.1586360307
Xcode: N/A
Issue: can't include some C++17 headers: execution, filesystem
while can include the other like: any, variant.
Most of C++17 features like structured binding, template parameters deduction etc works.
Example. Trying to build a file with the following code snippet:
#include <numeric>
#include <vector>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <utility> //std::pair
#include <tuple> // std::tie
#include <algorithm> // std::clamp
#include <optional> // std::optional
#include <map>
// ... other stuff
#include <execution>
namespace parallel_algoritmhs {
void show() {
std::vector<int> v = {1,2,3,4,5,6};
std::for_each(std::execution::par, v.begin(), v.end(), [](auto& e) {e+=100;});
int main() {
//... use
}
I am building with:
g++ -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
Update:
I have tried suggested:
clang -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
clang++ -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
And getting the same result.
If I comment out code snippet related to different execution strategies, while leaving other code snippets like:
#include <any>
namespace any {
void show() {
std::any v = 42;
v = 4.2;
v = std::string{"hello"};
std::cout << std::any_cast<std::string>(v) << '\n';
}
}
namespace map_cpp17 {
void show() {
std::map<int, std::string> myMap{ { 1, "Gennady" }, { 2, "Petr" }, { 3, "Makoto" } };
auto node = myMap.extract(2);
node.key() = 42;
myMap.insert(std::move(node));
std::map<int, std::string> m1{ { 1, "aa" }, { 2, "bb" }, { 3, "cc" } };
std::map<int, std::string> m2{ { 4, "dd" }, { 5, "ee" }, { 6, "ff" } };
m1.merge(m2);
std::map<int, std::string> m; m.emplace(1, "aaa"); m.emplace(2, "bbb"); m.emplace(3, "ccc");
auto [it1, inserted1] = m.insert_or_assign(3, "ddd"); std::cout << inserted1; // 0
auto [it2, inserted2] = m.insert_or_assign(4, "eee"); std::cout << inserted2; // 1
}
}
Everything compiles
With
#include <execution>
getting:
fatal error: 'execution' file not found
#include <execution>
I wonder what might be the problem behind that and how to fix this.
Thank you!
Update 2:
brew info llvm:
llvm: stable 10.0.0 (bottled), HEAD [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/llvm.rb
==> Dependencies
Build: cmake ✔, python#3.8 ✘
Required: libffi ✘
==> Requirements
Build: xcode ✘
==> Options
--HEAD
Install HEAD version
==> Caveats
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
Update 3:
clang --version
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
You're not actually using the Clang you installed. The official LLVM Clang doesn't have a version 11.0! LLVM is only on version 10.0 (check brew info llvm). You're using Apple Clang, which comes with its own installation of libc++. Presumably to catch this exact type of problem, Apple Clang responds to clang --version/clang++ --version with the "future" version 11.0. However, Apple's distribution is actually a bit behind the state of the art, and it simply isn't updated to these features.
When you install LLVM with Homebrew, it doesn't automatically set it up the new clang/clang++ to be easily callable, because things might expect clang to specifically mean "Apple Clang" and break if that changes. You should add /usr/local/opt/llvm/bin (which Homebrew symlinks into the LLVM installation) to your PATH (which is where your shell and other programs search for programs). In an already running shell session
export PATH="/usr/local/opt/llvm/bin:$PATH"
will switch to using the newly installed LLVM Clang until you exit the session. Putting this line in a shell startup file, like ~/.bash_profile, will set up this adjustment to be applied every time you open a new shell.
Alternatively, like #Eljay does, you could just manually type out the full path to the new clang/clang++ (/usr/local/opt/llvm/bin/clang/-++), thereby bypassing the search into PATH, but that's a pain. In any case, you'll know you got it right if clang --version/clang++ --version gives you 10.0.0.
I am trying to build a C++ application on macOS with Clang. I am using the CoreAudio API, so I need to link the CoreServices framework. However, I get the following error:
clang: error: unknown argument: '-framework CoreServices'
Here is my Clang version:
clang --version
clang version 3.9.1 (tags/RELEASE_391/final)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /usr/local/bin
These are my linker flags:
-framework CoreServices
What am I doing wrong here?
The following program code prints out the size occupied by the output devices (this is just to use some functionality from the CoreAudio API):
// main.cpp
#include <cstdlib>
#include <cstdio>
#include <CoreAudio/CoreAudio.h>
#include <CoreServices/CoreServices.h>
int
main (void)
{
AudioObjectPropertyAddress property = {
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
OSStatus status;
UInt32 size;
status = AudioObjectGetPropertyDataSize(
kAudioObjectSystemObject,
&property,
0,
NULL,
&size);
printf("status(%d), size: %d\n", status, size);
return EXIT_SUCCESS;
}
This C++ program can be compiled with:
clang -framework CoreServices -framework CoreAudio main.cpp
And will produce a a.out executable, that you can then call with
./a.out
> status(0), siz
I've reduced my code to the following to illustrate my problem:
#include <iostream>
#include <stack>
#include <utility>
std::pair<double,double> test(double a, double b)
{
std::stack<int> my_stack;
return std::make_pair<double,double>(a,b);
}
int main()
{
std::pair<double,double> p = test(1.1,2.2);
std::cout << p.first << " " << p.second << "\n";
return 0;
}
The return value from the test() function gets corrupted when I use the gcc -O1 flag. Here is some sample output:
$ gcc -O2 a.cxx -lstdc++
$ ./a.out
1.1 2.2
$ gcc -O1 a.cxx -lstdc++
$ ./a.out
2.60831e-317 2.60657e-317
$ gcc -v
Reading specs from /usr/lib64/gcc-lib/x86_64-suse-linux/3.3.3/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local- prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib64 --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit x86_64-suse-linux Thread model: posix
gcc version 3.3.3 (SuSE Linux)
This code works with all gcc optimazation flags except "-O1". It also works if I remove the declaration of my_stack. Would you classify this as a compiler bug, or am I missing something about std::stack and returning std::pair values?
Definitely a compiler bug.
BTW it is not reproduced with my GCC version (4.4.5).
The version of GCC 3.3.3 shipped with SuSE 9.1 is known to be broken (see here).
It's a bug. Check if the bug has been reported, if not, report it.