How to compile special mathematical functions with clang? - c++

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

Related

What is the status of C++20 coroutines support in Clang?

According to cppreference.com ( https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features ) Clang has partial support of C++20 coroutines since version 8:
But if in Clang trunk (which is upcoming version 13) I write
#include <coroutine>
it results in the error( https://gcc.godbolt.org/z/rTfjbarKz ):
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/coroutine:334:2: error: "the coroutine header requires -fcoroutines"
#error "the coroutine header requires -fcoroutines"
And if I add -fcoroutines flag in the command line, then Clang complains( https://gcc.godbolt.org/z/qMrv6nMzE ):
clang-13: error: unknown argument: '-fcoroutines'
Is there any way to start using C++20 coroutines in Clang?
Note that the first error is in the GCC standard library, and from that it can be deduced that the -fcoroutines option is for GCC not Clang.
To build with the Clang libc++ you need to add the option -stdlib=libc++ instead. But that will lead to the <coroutine> header file not being found instead.
Since Clang coroutines is still in the "experimental" stage you have to include <experimental/coroutine>.
So there are two things you need to change:
Use the Clang libc++ (-stdlib=libc++)
Include the experimental header file (#include <experimental/coroutine>)
Also note that since coroutines are experimental, the symbols defined in the header file will be in the std::experimental namespace.

Is this the correct way to initialize a vector in CPP?

I am running the below program on Dev-C++, and I am not able to execute it, as it always give me error while compiling the program.
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v2{2,3,4,5};
for(int x : v2)
{
cout<<x<<" ";
}
return 0;
}
[Warning] extended initializer lists only available with -std=c++11 or
-std=gnu++11
It always give me error, I just wanted to confirm, whether this type of Vector Initialisation is right? Or I am doing it the wrong way?
I am new to the community, sorry If this has been answered previously also, you could redirect me to the previous answer also.
Avoid use of <bits/stdc++.h>. It is not supported by some compilers and IDEs, plus it includes the entire STL library, which is unnecessary.
Instead, use specific templates such as <vector> in your case, since your using an std::vector. Don't forget to include <iostream> for std::cout as well.
[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
Provided your compiler supports C++ uptil a specific standard, you can set the standard using -std=standard, where 'standard' can be c++11, c++14, c++17 or c++20, taking into account of the recent versions of the C++ standard.
Your compiler clearly supports c++11 (in fact every compiler nowadays does, thats like the minimum) so just include that while compiling:
g++ -std=c++11 Filename.cpp -o Filename
This will compile your C++ file using g++ compiler with C++11 standard, and create an object file of the source file 'Filename.cpp'.

More detailed C++17 status in GCC?

Consider the following Minimal Complete:
#include <numeric>
int main() {
std::transform_reduce();
}
An attempt to compile it with GCC 7.2 results in error:
[bipll#home ~]$ g++ -std=c++17 omg.cpp
omg.cpp: In function ‘int main()’:
omg.cpp:4:7: error: ‘transform_reduce’ is not a member of ‘std’
std::transform_reduce();
^~~~~~~~~~~~~~~~
[bipll#home ~]$ g++ -v |& tail -1
gcc version 7.2.0 (GCC)
I have tried to find something related on https://gcc.gnu.org/projects/cxx-status.html but it mostly covers language features and I haven't seen anything on new STL definitions support (sometimes this can be trivially automated, like checking #if __has_include(<execution>), but <numeric> is an old include and I don't see any clean way to check whether it contains a particular definition). Aside from scriptedly parsing STL headers, are there any quick info sources on whether something described in current draft has already been implemented? and when something else will probably be?
The library (libstdc++) support is documented on a different page:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.201z
The "Parallelism TS" is still marked as "No", so it's not been implemented yet. It's also not implemented in Clang. You can test various compilers here:
https://gcc.godbolt.org

Clang and GCC different diagnostics behaviour for function invocation before definition?

Let's analyze the following code example:
int main() {
return func();
}
int func() {
return 1;
}
Clang will report error:
/private/tmp/src.cpp:2,9 - Error - use of undeclared identifier 'func'
But i can find lot's of such errors in wide used examples, f.e. some arduino sources:
https://github.com/firmata/arduino/blob/master/examples/StandardFirmata/StandardFirmata.ino
line 235 : disableI2CPins(); // invocation (before definition, no declaration before)
...
line 651 : void disableI2CPins() { .. } // definition
Though function is used before defined that code can be compiled with GCC successfully. How that can be? Is it different Clang and GCC behavior? Are there any flags to allow this for Clang?
PS. Here is successful command line to compile the code used by Arduino IDE:
/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++
-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10600 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino
-I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/variants/mega
-I/Applications/Arduino.app/Contents/Resources/Java/libraries/Servo/src
-I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Wire
-I/Applications/Arduino.app/Contents/Resources/Java/libraries/Firmata/src
/var/folders/64/fwfkm1k51zbd4_c5lwpsbljh0000gn/T/build8049651108713091801.tmp/StandardFirmata.cpp
-o /var/folders/64/fwfkm1k51zbd4_c5lwpsbljh0000gn/T/build8049651108713091801.tmp/StandardFirmata.cpp.o
This is actually not to do with the compiler, but the Arduino IDE. The IDE has a preprocessing step which forward-declares all the defined functions so that you don't need to worry about function placement in the file.
My strong recommendation would be to just follow the language rules and forward-declare your functions or define them before their use. Your code will be more portable and understandable for it.
No, you don't want clang to behave like gcc in this regard. In fact, use -Wall and -W (or -Wextra) in order to get gcc and clang both to give you even more warnings.
But i can find lot's of such errors in wide used examples
Sadly, getting C and C++ right is so hard that widely used examples are not good enough.
Your example includes a bug that arguably has no real effect on this program. EDIT: it appears that the IDE is clever enough to workaround language features. Refer to #TartanLlama's answer for details. Perhaps the author could make the claim that it's by design. But it's so rarely the case that clang thankfully saves you from yourself.
C89 §3.3.2.2, "Function calls" includes:
If the expression that precedes the parenthesized argument list in a
function call consists solely of an identifier, and if no declaration
is visible for this identifier, the identifier is implicitly declared
exactly as if, in the innermost block containing the function call,
the declaration
extern int identifier();
appeared
When I first learned that, I thought to myself, "OMG that's not what I wanted at all!" And, so it went with so many other folks that this "feature" was changed in C99.
Clang is per default in GNU C11 mode which is uses C99 semantics. In C99 the implicit function declaration is not allowed anymore.
you can put clang into c89 mode with clang --std=c89 or clang --std=gnu89 which should allow that behavior,
See: Are prototypes required for all functions in C89, C90 or C99?

How configure include path and use standard library with gcc compiler?

I know this topic was few times there, but I can't get satisfactory answer.
C:\Users\Krzysiek>gcc test.c
test.c:3:20: fatal error: iostream: No such file or directory
compilation terminated.
This is what I try to do
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Simple program with "include"
I've heard of LIBRARY_PATH. So I've setted that. Still this same error I have.
GCC provides wrappers around calling its various compilers.
You are using gcc, which is for C (and consequently will not include or link the C++ standard library; the compiler would go on to complain about the rest of your code, too, since it's not valid C);
Use g++, which is for C++.
Also try to use a conventional extension for C++ source files, which is .cc, .cxx or .cpp.
Use g++ instead: it will link to the c++ standard library.
When you use the gcc command, gcc looks at the file extension to decide which language to use to compile. As you used a .c file, gcc will switch by default to C.
# Use the C compiler
gcc test.c
# Use the C++ compiler
gcc test.cpp
To choose a different language, you can use the -x option:
# Use the C++ compiler even if the extension is .c
gcc -xc++ test.c
Another method of using the C++ compiler is to use g++ in the command line. This is the preferred way, as it links with the correct libraries.
# Use the C++ compiler
g++ test.c