Problems with compiling c++20 - c++

I'm trying to play around with some new c++20 features but I am not able to compile even some simple lines of code..
I am working on macOS and first tried to compile it in Xcode with c++2a language dialect which didn't work out and then tried to compile the code from the command line with g++ but I got similar errors.
Everything is up to date (Clang Vers 12, g++/gcc vers 10) and I know that there are still some issues with some features.
So here is an example:
import <iostream>;
#include <vector>
int main(){
std::vector<int> vec{1,2,3};
for (int e : vec) std::cout << e << std::endl;
}
compiled with:
g++ -std=c++2a -fmodules-ts test.cpp
Throws the following error:
header file <iostream> (aka '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream') cannot be imported because it is not known to be a header
So I assumed there is a problem with the header path from the toolchain which hasn't the latest updates so I tried changing the path to g++ main includes.
So I tried:
g++ -std=c++2a -fmodules-ts -I/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/ test.cpp
Which throwed this error:
test.cpp:1:8: error: header file <iostream> (aka '/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/iostream') cannot be imported because it is not known to be a header unit
import <iostream>;
^
In file included from test.cpp:2:
In file included from /usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/vector:60:
/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/bits/stl_algobase.h:59:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>
^~~~~~~~~~~~~~~~~~
2 errors generated.
Can someone help with this?
I am aware that I can make this compile using #include - this was just as an example for working with new features - I had the same issue with e.g. import .
I am really struggling with getting anything sorted - which compiler is the best usecase for c++20 and how and where can I include/locate the standardlib which has c++20 support?
And how can I use everything together in Xcode?

gcc 10 does not support this yet. You can use gcc trunk or the gcc modules branch, and you need to add -fmodules-ts to the command line.

Related

Error compiling C++ source utilizing the Boost.Math library

I'm trying to use a couple of functions from the Boost Math library in some C++ code using the G++ compiler but I've been unsuccessful. This is on macOS.
I downloaded and extracted the Boost tar.gz from here and placed it into my source folder.
Within my C++ I've tried
#include "boost_1_63_0/boost/math/distributions/chi_squared.hpp" and
#include <boost_1_63_0/boost/math/distributions/chi_squared.hpp>.
The quotation version partially works but the chi_squared.hpp file includes fwd.hpp using the bracket (#include <...>) notation and that breaks my compilation with error In file included from main.cpp:9: ./boost_1_63_0/boost/math/distributions/chi_squared.hpp:12:10: fatal error: 'boost/math/distributions/fwd.hpp' file not found #include <boost/math/distributions/fwd.hpp>.
To compile I've used an assortment of commands, all unsuccessfully:
g++ -L /boost_1_63_0/boost/math/distributions main.cpp
g++ -I"/boost_1_63_0/boost/math/" main.cpp
g++ -I "/boost_1_63_0/boost/math/" main.cpp
g++ main.cpp -lboost_math
What is the correct include statement and G++ command that I need to use?
Resolved using
#include "/Users/[me]/[project_dir]/boost_1_63_0/boost/math/distributions/chi_squared.hpp"
and
g++ -I/Users/[me]/[project_dir]/boost_1_63_0/ main.cpp

C++20, how to compile with Clang-10 or GCC11

I'm aware that C++20 is not fully supported (yet) by the compilers, but I really want to learn modules and other C++20 stuff.
Modules are supported in GCC11 and Clang-8+.
Compiler Support of C++20
I've installed Clang-10 on my Ubuntu, but it still gives me errors:
import <iostream>;
using namespace std;
int main(){
cout << "Hello world";
}
What am I doing wrong?
COMMANDS:
clang++ -Wall -std=c++2a -stdlib=libc++ -fimplicit-modules
-fimplicit-module-maps main.cpp -o main
clang++ -Wall -std=c++20 -stdlib=libc++ -fimplicit-modules
-fimplicit-module-maps main.cpp -o main
ERROR: fatal error: 'iostream' file not found
Although c++20 adds modules the c++20 standard library doesn't expose any modules.
Microsoft have implemented some standard library modules which may or may not match a future c++ standard: https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#consume-the-c-standard-library-as-modules. With these your example would be:
import std.core;
using namespace std;
int main(){
cout << "Hello world";
}
As far as I can see neither libc++ or libstdc++ have implemented any modules yet.
By default, gcc trunk use c++17, and clang trunk use c++14, so you have to say compiler, that you want to use c++20
If you are compiling your code in terminal by yourself, than add following flag
--std=c++2a
If you compile your code using Cmake, than add following to your CMakeLists.txt
set(CMAKE_CXX_STANDARD 20)
And if you compile in some IDE(Codeblocks or Visual studio), than somewhere in compiler settings put supporting c++20
trunk means "the main line of development", so this compiler version should be latest officially supported

clang++ fails to compile hello world

I installed clang in my conda environment along with gcc. Their versions are
gcc 7.2.0
clang 7.0.0
libcxx 7.0.0
I then created an hello world src file a.cpp
If I compile the file using clang++ a.cpp. The error reads
a.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
Using clang++ a.cpp --stdlib=libstdc++, the error is the same
Using clang++ a.cpp --stdlib=libc++, the error becomes
~/conda/envs/test/bin/ld: cannot find crtbegin.o: No such file or directory
~/conda/envs/test/bin/ld: cannot find -lgcc
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
Using clang++ a.cpp -I$HOME/conda/envs/test/include/c++/7.2.0
In file included from a.cpp:1:
/site/home/shliu/conda/envs/test/include/c++/7.2.0/iostream:38:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>
^~~~~~~~~~~~~~~~~~
1 error generated.
I use a shared computer so I cannot install system wide compilers and header files.
Questions:
What should I do to have it work?
If clang does not ship with its own header files and I need to use what are provided by gcc, should I consider the compatibility of clang version and the gcc version?
Do I need to install libc++ in the same conda environment in order to use clang++?
After some test, I found the way to do it in conda, which is posted as the an answer. However, I still don't understand how clang works, especially its relation with gcc. I would appreciate it very much if any one could answer (and I will accept that as the answer to this post):
Does clang forward all the jobs to gcc so we always need the gcc tool chain to be installed in order to use clang?
I found an include folder for clang, which is $HOME/conda/envs/test/include/c++/v1 alongside with $HOME/conda/envs/test/include/c++/7.2.0 which is from gcc. But if the --gcc-toolchain has been specified, the v1 folder is not searched for headers, (which can be seen from the output by adding -v to the compiler. Then what is the usage of the v1 include files?
Finally I found the way, which is to do
clang++ --gcc-toolchain=$HOME/conda/envs/test a.cpp
This is not obvious at all.

Cannot include standard libraries in C++ file

#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.

MinGW g++ cannot find std::stof

I just (re)installed MinGW from scratch, with gcc 4.8.1 (the latest available), and the following program won't compile:
#include <iostream>
#include <string>
int main()
{
float f;
std::string s = "5.235";
f = std::stof(s);
std::cout << f << '\n';
}
Here's the command I use:
g++ -std=c++11 -o test test.cpp
I get this error:
test.cpp:8:9: error: 'stof' is not a member of 'std'
f = std::stof(s);
^
The file bits\basic_string.h which declares std::stof is included properly in the string header, and I checked bits\basic_string.h for std::stof's declaration, and it's there.
After a bit of Googling I did find some old patches for MinGW (4.6 - 4.7) but they seem irrelevant since I'm on gcc 4.8.
Any ideas? Thanks in advance.
Not sure about exact problem, but check mingw-w64 they have gcc 4.9.2 for now. It compiles your code just well. (But since the mingw-w64 project on sourceforge.net is moving to mingw-w64.org it's better to use mingw-w64.org)
Despite of it's name it provides compilers for both x86 and x64 targets.
Probably this should be a comment, not an answer.