Where to find standard C++17 cmath file? - c++

Where I can find original cmath or math.h files?
I need to use a comp_ellint_1(double) function in my program. It is declared in C++17 standard in Special functions.
But my compilers(I tried g++/clang++ etc.) say this.
I found out that in core files, where standard math.h and cmath are, there are no such functions declared in cmath. It looks like it is 99 standard...

These functions are available in gcc starting with version 7.1 and in clang starting with 3.9. You either have to upgrade your compiler, or use some other implementation (according to en.cppreference.com, you can use Boost.math

This works for me. You need g++ version 7 or higher and you must specify C++17 or higher.
#include <ctgmath>
#include <iostream>
int main(int argc, char *argv[]){
double x = 0.5;
double y = std::comp_ellint_1(x);
std::cout << "x="<<x <<" -> y(x)="<<y <<std::endl;
return 0;
}
Compiled and ran as follows
$ g++ --std=c++17 test.cpp -o test
$ ./test
x=0.5 -> y(x)=1.68575
$
Incidentally, in Ubuntu 18.04, the headers are in /usr/include/c++/n/tr1/ where n=7,8, or 9 (major versions of g++ that support special math functions, as of Dec 2019)

Related

std::numbers has not been declared - GCC 11.2 on Windows

I have GCC 11.2.0 installed on my Windows 10 machine (from here: https://winlibs.com/). I have updated the environment variable Path to C:\MinGW\bin
gcc version 11.2.0 (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders)
I'm using VSCode with the C/C++ extension configured to use the correct compiler path.
I want to use a C++20 feature which is
std::numbers::sqrt2
Still I get an error telling me it doesn't know std::numbers
[Running] cd "c:\Users\XX\XX\" && g++
main.cpp -o main && "c:\Users\XX\XX\"main
main.cpp: In function 'double sin_x_plus_cos_sqrt2_times_x(double)':
main.cpp:15:41: error: 'std::numbers' has not been declared
15 | return std::sin(x) + std::cos( std::numbers::sqrt2 * x );
|
I've added the header #include <numbers>
What am I missing ?
Default version of c++ standard for this version of gcc is C++17.
See this: https://godbolt.org/z/4Pjzd5r7s
Use
g++ main.cpp -o main -std=c++20
to force C++20
There is some support of C++20 in gcc, but it is simply to early to make it default standard.
What am I missing ?
In order to use C++20 features, you need to select the C++20 standard version.
Both gcc 10 & 11 require -std=c++20 on the command line for std::numbers to work. (Older versions than that don't support std::numbers at all)
https://gcc.gnu.org/projects/cxx-status.html#cxx20 shows progress on standards compliance. I would expect a standard to become default soon after all the details of the standard are met. As of Nov 2021, it seems they've got pretty much everything except some remaining details of the C++20 "modules" features.

How to compile special mathematical functions with clang?

One can call sph_legendre from tr1 and compile it with gcc-5 or gcc-6
#include<tr1/cmath>
int main()
{
std::tr1::sph_legendre(1,1,0);
return 0;
}
Unfortunately, if I try to compile it with clang++ as:
clang++ -stdlib=libstdc++ legendre.cpp -o legendre
I get
error: no member named 'sph_legendre' in namespace 'std::tr1'
In the section 40.3, The C++PP (4th edition) states:
"There is a separate ISO standard for special mathematical functions [C++Math,2010]. An implementation may add these functions to cmath"
How can I compile these special functions with clang++?
As Richard already mentioned, clang does not support std::sph_legendre at the moment.
A possible workaround for you is to use the boost libraries (http://www.boost.org)
and include header: boost/math/tr1.hpp

GCC C++11 Fails to See #if windows And #if linux (Re-ask)

When using the #if windows/#if linux compilers features in Debian GCC version 4.7.2, I have been unable to get them work when using std11.
Independently, I can get the compiler to accept the if-defines without complaint. I can also get the compiler to use the same code with c++ 11, without any if-defines (and thus not conditionally). But when I run the conditional defines through the same compiler, with the tag for c++ 11, the code is rejected.
Below I have included a simple example, with two alternate mains, and the error I get. The only difference between the two mains are the commented out lines.
Runs:
g++ main.cpp -std=c++11 -o test
The above uses the c++ 11 standard. When running commented main it works perfectly. But when running uncommented main, it fails entirely, giving the error at the far end of this post.
g++ main.cpp -o test
The above does not use the c++ 11 standard. When running either commented main or uncommented main it works perfectly.
Below are the code examples.
Commented main.cpp:
#include <iostream>
//#if windows
//#include "WindowsSolution.hpp"
//#endif
//#if linux
#include "LinuxSolution.hpp"
//#endif
int main()
{
std::cout << myGlobalSolution.out() << std::endl;
return 0;
}
LinuxSolution.hpp:
class LinSolution{
public:
LinSolution(){
}
std::string out(){
std::string ret("Linux solution");
return ret;
}
};
LinSolution myGlobalSolution;
WindowsSolution.hpp:
class WinSolution{
private:
WinSolution(){
}
std::string out(){
std::string ret("Windows solution");
return ret;
}
};
WinSolution myGlobalSolution;
Uncommented main.cpp:
#include <iostream>
#if windows
#include "WindowsSolution.hpp"
#endif
#if linux
#include "LinuxSolution.hpp"
#endif
int main()
{
std::cout << myGlobalSolution.out() << std::endl;
return 0;
}
Below is the error I get when compiling with the uncommented main.cpp, using the c++ 11 flag.
main.cpp: In function ‘int main()’:
main.cpp:13:15: error: ‘myGlobalSolution’ was not declared in this scope
The simple constant linux is a GCC extension and not an official OS constant. The proper constant for Debian is probably __gnu_linux__; you can find a list of them for various systems here. Usually official predefined constants follow the convention of using __ at the start.
Your code works without the standard flag because by default GCC compiles in a GNU language mode (GNU C++) rather than a standard language mode (ISO C++); the GNU language includes extensions (extra language features, and in this case, legacy OS constants). When you pass the -std=c++11 flag you are requesting ISO language mode, which means GNU extensions are disabled, including GNU-only constants. To get both GNU extensions and a specific set of ISO features, try requesting a GNU language standard version instead (e.g. -std=gnu++11 works fine).

Decimal GCC library

I wrote this simple code just to check whether the #include <decimal/decimal> worked:
#include <iostream>
#include <decimal/decimal>
using namespace std;
int main ()
{
cout << "Hello, world!" << endl;
return 0;
}
I compiled it with:
$ g++ main.cpp -o exe
But I get this error message:
In file included from main.cpp:2:0:
/opt/local/include/gcc47/c++/decimal/decimal:39:2: error: #error This file requires compiler and library support for ISO/IEC TR 24733 that is currently not available.
/opt/local/include/gcc47/c++/decimal/decimal:230:56: error: unable to emulate 'SD'
/opt/local/include/gcc47/c++/decimal/decimal:251:5: error: 'std::decimal::decimal32::decimal32(std::decimal::decimal32::__decfloat32)' cannot be overloaded
... lots more similar errors ... (removed by Mats Petersson)
What does this mean? How can I fix it?
When building gcc you can enable support for C's decimal support of TR24732. The C TR creates built-in types like _Decimal64which are given nicer names by including <decimal.h>, e.g., decimal64. These built-in types are also available in C++ when gcc is configured when building to include decimal support.
The decimal support forC++ is defined by TR27433 and would be made available by including <decimal/decimal>. With gcc decimal support in C++ seems to be only available if gcc is compiled with decimal support enabled, i.e., it seems it uses the same built-in types as the C code does.
In either case it seems the decimal support in gcc doesn't cover everything specified by the respective TRs. In particular, last time I checked I/O support was missing.

How to compile a C++ program in LLVM using clang++?

There is a tutorial - http://llvm.org/docs/GettingStartedVS.html Example done in pure C. I can compile and link it. Without problem, really. But I need C++, not pure C. And here the difficulties begin.
For clang++ I use string like
"C:\..> clang++ -c hello.cpp -emit-llvm -o hello.bc"
then:
"C:\..> llc -filetype=obj hello.bc"
and
"C:\..> link hello.obj -defaultlib:libcmt -out:hello.exe"
there I get 14 errors LNK2001: unresolved external symbol
So, I need some tips. What I do wrong?
//-----------------
hello.cpp:
#include < iostream >
int main()
{
std::cout << "TEST\n" << std::endl;
return 0;
}
//-----------------
OS: Windows7.
UPD: Main question: how from .bc get .exe? (LLVM, Windows7)
You can use my GCC and Clang packages:
Download and extract them to the same directory. Clang will use GCC 4.6.3's libstdc++ and MinGW-w64's CRT. Call it like you would gcc.
Clang/LLVM cannot currently work with MSVC's C++ library, due to ABI issues. GCC's libstdc++ works quite well, though it has holes in surprising places (like std::to_string, <regex>, and <thread>).
Clang's Windows support is OK, but far from complete. You cannot for example dllexport whole C++ classes, unfortunately. And Win64 code generation is also not good enough to have a working C++ install (even in combination with GCC, like for 32-bit).