A lot of warnings when compiling date library - c++

The date library is very useful but you can see a lot of warnings after compiling a simple example that uses date library.
for example:
#include "date.h"
int main() {
using namespace date;
std::cout << weekday{July/4/2001} << '\n';
}
compiled with:
g++ -c -Waggregate-return main.cpp
warning: function call has aggregate value [-Waggregate-return]...

Depending on the version of your compiler, C++11 (or later) mode might not be active, so try adding -std=gnu++0x at least. Also you code needs #include <iostream> added.

To fix these warnings you should remove the flag -Waggregate-return from your compile string. This flag causes warnings for valid code.
Note that you also need #include <iostream>, and for older versions of g++ , -std=c++14 or a similar flag.

Related

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

std::thread is not being found on gcc-8.2.2 with flag option -m32. I'm using mingw

I was trying to compile a cpp program for a school project. It uses libbgi for graphics, which works for 32-bit target. For this project i wanted to add some multithreading with std::thread, but the compiler say i couldn't find std::thread.
I tried a compiler with a lower version, with c++11 support.
#include <iostream>
#include <thread>
int main(){
std::thread t([](){
std::cout << "Hello\n";
}
);
t.join();
}
this program compiles fine with -m64, but not with -m32.
I believe you might need to link with pthread and use c++0x.
g++ -std=c++0x -m32 main.cpp -pthread -o main
I recreated your error and it was solved using these flags.

Compile error: 'stoi' is not a member of 'std'

My code:
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
Gives me the compile error:
error: 'stoi' is not a member of 'std'
int myint = std::stoi(test);
^
However, according to here, this code should compile fine. I am using the line set(CMAKE_CXX_FLAGS "-std=c++11 -O3") in my CMakeLists.txt file.
Why is it not compiling?
Update: I am using gcc, and running gcc --version prints out:
gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
In libstdc++, the definitions of stoi, stol, etc., as well as the to_string functions, are guarded by the condition
#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
I have had this fail on one platform before (namely Termux on Android), resulting in to_string not being available even with g++ 6.1 and the C++14 standard. In that case, I just did
#define _GLIBCXX_USE_C99 1
before including anything, and voilà, suddenly the functions existed. (You should put this first, or even on the command line, rather than just before including <string>, because another header may include <string> first, and then its include guards will keep it from ever seeing your macro.)
I did not investigate why this macro wasn't set in the first place. Obviously this is a cause for concern if you want your code to actually work (in my case I didn't particularly, but FWIW there were no problems.)
You should check if _GLIBCXX_USE_C99 is not defined, or if _GLIBCXX_HAVE_BROKEN_VSWPRINTF is defined (which may be the case on MinGW?)
std::stoi is a C++11 function. You have to use the -std=c++11 to enable it in both g++ and clang++. This is the actual issue, not a linking error or a specific preprocessor define.
$ cat test.cxx
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
$ g++ -otest test.cxx
test.cxx: In Funktion »int main()«:
test.cxx:7:17: Fehler: »stoi« ist kein Element von »std«
int myint = std::stoi(test);
^
$ g++ -otest test.cxx -std=c++11
$ ./test
45
$
edit: I just saw that you used c++11. Are you sure that's making it into your compile options? Check the generated makefile and watch the executed commands to be certain.
Your version seems up to date, so there shouldn't be an issue. I think it may be related to gcc. Try g++ instead.(Most likely automatically linking issue. If you just run gcc on a C++ file, it will not 'just work' like g++ does. That's because it won't automatically link to the C++ std library, etc.). My second advise is try std::atoi.
# I have fixed the issue. std::stoi uses libstdc++. It is about The GNU Standard C++ Library. In gcc you have to link adding -lstdc++. However, in g++, libstdc++ is linked automatically.
using gcc and using g++
Pay attention how it is compiled
using g++: g++ -std=c++11 -O3 -Wall -pedantic main.cpp && ./a.out
using gcc: gcc -std=c++11 -O3 -Wall -pedantic -lstdc++ main.cpp && ./a.out
I think you should set flag like set(CMAKE_EXE_LINKER_FLAGS "-libgcc -lstdc++") (Not tested)
#include <cstdlib>
int myInt = std::atoi(test.c_str());
If you are using Cmake to compile, add line:
"add_definitions(-std=c++11)"
after find_package command.
Use 'set(CMAKE_CXX_STANDARD 11)' for Cmake

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.

Regex in C++: Requires compiler support error

I'm trying to use regex in C++. The following is my code:
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<regex>
using namespace std;
int main(int argc, char** argv) {
string A = "Hello!";
regex pattern = "(H)(.*)";
if (regex_match(A, pattern)) {
cout << "It worked!";
}
return 0;
}
But I'm encountering this error :
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/regex:35:0,
from main.cpp:12:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
How can this be solved and what is wrong?
and must be enabled with the -std=c++0x or -std=gnu++0x compiler options
Add one of those options, -std=c++0x or -std=gnu++0x, to your compiler command:
g++ -std=c++0x ...
Note if std::regex is not supported see boost::regex for an alternative.
It looks like you are trying to use the regex class, which is part of the new C++11 standard, but not telling the compiler to compile to that standard.
Add -std=c++0x to your compiler flags and try again.
EDIT : As the gcc implementation status page shows, the regex support in gcc is far from complete. So even adding the right flag wont help yet. If you need regex support, you could try boost.
Simply just add
g++ -std=gnu++0x <filename.cpp>
or
g++ -std=c++0x <filename.cpp>
It will work properly