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.
Related
I am compiling a C++ code on mac using swig and making a shared .so library.
I have found a similar question here but I get an error for the last line:
ls
sl.i sl.hpp
swig -c++ -python sl.i
clang -O2 -fPIC -c sl_wrap.cxx -I /Library/Frameworks/Python.framework/Versions/3.8/include/python3.8
clang -lpython -dynamiclib sl_wrap.o -o _sl.so
ls
sl.py sl_wrap.o sl.wrap.cxx sl.i sl.hpp
error:
ld: library not found for -lpython
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
My machine:
ProductName: macOS
ProductVersion: 13.0
Homebrew clang version 15.0.2
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
SWIG Version 4.0.2
Compiled with clang++ [x86_64-apple-darwin21.1.0]
Configured options: +pcre
A toy example:
sl.hpp:
#include <cmath>
#include <string>
#include <iostream>
class My_Class
{
private:
int N;
public:
int add(int a, int b)
{
return (a+b);
}
};
sl.i:
%module sl
%{
#include "sl.hpp"
%}
%include stl.i
%include "std_string.i"
/* instantiate the required template specializations */
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
%template(DoubleVector2) vector<vector<double> >;
%template(SingleVector) vector<float>;
%template(SingleVector2) vector<vector<float> >;
}
%include "sl.hpp"
Install python via brew or
check your installation for python3-config tool, then launch e.g.
/opt/homebrew/bin/python3-config --ldflags
and set the output as linker flags to your cmdline, e.g.
-L/opt/homebrew/opt/python#3.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/config-3.10-darwin -ldl -framework CoreFoundation
I am trying to get this snippet to compile
#include <vector>
#include <numeric>
#include <execution>
double result = std::reduce(std::execution::par, v.begin(), v.end());
I tried these compilers:
Apple LLVM version 8.1.0 (clang-802.0.42)
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
All three give me 'execution' file not found
respectively error: no member named 'reduce' in namespace 'std'
auto result = std::reduce(v.begin(), v.end());
for this snippet
#include<numeric>
#include<vector>
int main(int argc, char *argv[])
{
std::vector<double> v(10, 1);
auto result = std::reduce(v.begin(), v.end());
return 0;
}
I guess my compilers are too old? But on cppreference it does not say which compiler version is requiered minimum and also I do not see any newer versions for clang or gcc in the repo.
std::reduce and std::execution::par are available since C++17.
For most of the compilers C++17 isn't fully implemented yet. You can try using clang with flag -std=c++1z.
I upgraded my GCC to version 10 and it compiled fine the std::execution::par with its include <execution> (which was failing to locate with gcc version 7.5.0). I followed to the instructions in this link: https://tuxamito.com/wiki/index.php/Installing_newer_GCC_versions_in_Ubuntu
I have a problem compiling code using std::variant.
I try to compile this code with g++ 5.4/6.2 on ubuntu and fedora with -std=c++17:
#include <variant>
#include <string>
int main()
{
std::variant<int, float> v, w;
v = 12; // v contains int
int i = std::get<int>(v);
w = std::get<int>(v);
w = std::get<0>(v); // same effect as the previous line
w = v; // same effect as the previous line
try {
std::get<float>(w); // w contains int, not float: will throw
}
catch (std::bad_variant_access&) {}
std::variant<std::string> v("abc"); // converting constructors work when unambiguous
v = "def"; // converting assignment also works when unambiguous
}
found on cppreference.com but this error append: "fatal error: variant: No such file or directory".
Install g++-7; e.g. if ubuntu
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt-get update
sudo apt-get install g++-7
run /usr/bin/g++-7 -std=c++1z your_program.cc
Optional, if you use cmake system, add these lines :
set(CMAKE_CXX_COMPILER "/usr/bin/g++-7")
set(CMAKE_CXX_FLAGS "-std=c++1z")
std::variant was added in C++17.
gcc does not yet fully support the relevant bits of the C++17 standard.
I don't even see std::variant listed in the gcc's tracking page.
I have a C++ program that will not compile under OS X 10.8.5 with the g++ compiler. The problem seems to be with the math.h header file.
This is the version of g++ is
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/Users/densmore3/local/usr/local/bin/../libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../gcc-4.9-20141029/configure --enable- languages=c++,fortran
Thread model: posix
gcc version 4.9.2 20141029 (prerelease) (GCC)
There are 40-50 errors of the type below. The code compiled fine on 10.6. What is going on?
/Users/xxxx/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:203:1: error: ‘__header_always_inline’ does not name a type
__header_always_inline int __inline_isfinitef(float);
Users/densmore3/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:580:27: error: expected initializer before ‘__AVAILABILITY_INTERNAL__MAC_10_0_DEP__MAC_10_9’
extern float __inff(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_9, __IPHONE_NA, __IPHONE_NA);
Here is a piece of test code that gives the same error as my real code. The error goes away if I remove the math.h include statement.
#include <iostream>
#include <math.h>
#include <stdio.h>
//#include <complex>
//#include <vector>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
The compile command I am using is:
g++ test.cpp
gcc-4.8 is the correct version to use on OS X 10.8.
I read it here that CUDA 6.5 has started support for C++11 :
https://groups.google.com/forum/#!topic/thrust-users/R37GIkMG4tk
But when I compile an example code below, I got
$ nvcc -std=c++11 cu-gcc11.cu -o test
nvcc warning : The -c++11 flag is not supported with the configured host compiler. Flag will be ignored.
cu-gcc11.cu(7): error: explicit type is missing ("int" assumed)
My setting : CUDA 6.5, g++ 4.5, ubuntu 12.04
Codes :
#include <cuda.h>
#include <iostream>
__host__ void test() {
float a = 12.;
double b = 3.;
auto c = a * b;
std::cout << c << std::endl;
}
int main()
{
test();
return 0;
}
C++11 support in nvcc is experimental at this time. In order to properly use it you will need an appropriate configuration. This is not documented anywhere AFAIK, but you should have good results with either Fedora 20 or Ubuntu 14.04, both of which are supported configs for cuda 6.5 and include GCC 4.8.x.
In your case your GCC version is just too old.
I don't think -std=c++11 was available in GCC 4.5. Try -std=c++0x.