g++ -- missing array header - c++

The following simple code can't be compiled by g++ 4.3:
#include <array>
using namespace std;
int main()
{
std::array<int, 8> myarray;
return 0;
}
array: No such file or directory
Also, the compiler doesn't seen to understand option '-std=c++11' as is recommended to provide to the compiler. Is there another option?
Thanks.

GCC 4.3 and presumably also your C++ library are too old for the support you're looking for. You need a newer version. Here's a link to the GCC C++11 support page, and another link to the libstdc++ C++11 support page.
Alternatively, clang supports all of C++11 with libc++.

For me the problem was that it was a cross compiler that needed to be told where the sysroot was, and supplying --sysroot=<path to sysroot> allowed GCC to find the headers

Related

Mac c++ filesystem library not recognized

NOTE: this does not fail to #include <filesystem>. It fails afterward.
I’m on a macOS 10.15, using clang 11. Output of clang --version:
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
When I try to compile something simple like:
#include <filesystem>
using namespace std;
filesystem::path dev_dir;
int main() {
dev_dir = "/dev/";
return 0;
}
The compiler does find the filesystem library, but doesn’t recognize the namespace:
$clang trigger_controller.cpp -o trigger_controller
trigger_controller.cpp:##:##: error: use of undeclared identifier 'filesystem'
filesystem::path dev_dir;
This is the file I believe I’m trying to include (which I’ve confirmed exists):
// /usr/local/include/c++/9.2.0/filesystem
#ifndef _GLIBCXX_FILESYSTEM
#define _GLIBCXX_FILESYSTEM 1
#pragma GCC system_header
#if __cplusplus >= 201703L
#include <bits/fs_fwd.h>
#include <bits/fs_path.h>
#include <bits/fs_dir.h>
#include <bits/fs_ops.h>
#define __cpp_lib_filesystem 201703
#endif // C++17
#endif // _GLIBCXX_FILESYSTEM
What am I doing wrong? Are there specific compiler options I need? Is the namespace wrong?
Yes, there are specific compiler options you need. Notice that the entire contents of this header, apart from boilerplate, are wrapped in an #if __cplusplus >= 201703L ... #endif block. That means the header is effectively empty unless the compiler declares conformance with the 2017 revision of the C++ standard (or later).
For clang and gcc, standards conformance level is controlled with the command line option -std. GCC's documentation for this option is here; I don't know where to find clang's documentation, but clang generally tries to be command-line compatible with gcc, so the same options should work.
In this case, the exact option you should use is -std=gnu++17 for C++ 2017. As of this writing, support for newer revisions of the C++ standard is still "experimental" and "will almost certainly change in incompatible ways", so I would avoid it. I also recommend you avoid the hyperconformant variant of this mode, -std=c++17, because it has a good chance of exposing bugs in MacOS system headers.

note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?

I try to compile a simple c++ code in c++, but keep returning errors when I try to compile it with g++ in windows.
I use
g++ -std=c++0x -pthread main.cpp
The error messages are:
std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
Which doesn't make sense because the code is just
#include<thread>
void f(int i) {}
int main() {
std::thread t(f, 1);
t.join();
return 0;
}
I believe this code works in linux, I wonder why it can't work under windows.
To use std::thread you must be compiling your code as C++11, C++14 or C++17.
You are passing -std=c++0x to gcc. c++0x was the name used for pre-release versions of the gcc C++11 impletation and, depending on your compiler version, may be incomplete.
Change your gcc command line to -std=c++11 and things most likely work better. If not, you may need to get a newer version of the compiler.
Windows has its own threading API that is not POSIX standard. What you need to find out is (how to acquire and) how to link the threading library for your compiler. It sounds like you're using MinGW? I use MSVC and it automatically links with the Windows threading libraries. Unfortunately I don't know how to do this for MinGW, so this isn't the best answer, but here is a link that might get you started:
Does MinGW-w64 support std::thread out of the box when using the Win32 threading model?

'default_random_engine' is not a member of std

I have found many questions to this topic but all problems seem to be related to not compiling with C++ 11. My code is
#include <random>
int main(int argc, char *argv[]){
std::default_random_engine generator;
return 0;
}
even though I compile with
gcc -std=c++0x testmain.cpp
Giving the error that default_random_engine is not a member of std. The program is compiled on a remote machine, which I do not maintain myself but
gcc -v
yields a version of 4.4.7.
Any ideas?
For others:
Check if you actually include random with #include <random>. I didn't have it and some other header included it previously. Now that header got updated and I got this error and didn't find it for a while because I was checking compiler settings.
As DevSolar already stated, your gcc version is too old, to support this C++11 feature.
It was added in gcc-4.5:
Improved experimental support for the upcoming ISO C++ standard,
C++0x, including:
Support for <future>, <functional>, and <random>.
Reference: https://gcc.gnu.org/gcc-4.5/changes.html
This is also reflected by the libstdc++ API Reference: https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-api-4.5/a01118.html
where you can find the following:
typedef minstd_rand0 default_random_engine
Your code works fine for me in: gcc-5.1.0, gcc-4.9.2 and clang-3.7.0,
Also you should use the command: g++ instead of gcc so gcc links against proper c++ libraies by default.
Your problem is you're not compiling with C++11. ;-) (Sorry, could not resist.)
GCC 4.4.7 is dated March 2012. C++11 support was not yet complete in that version.
As of the time of this writing, the current version of GCC is 5.2.0... which is C++14 compliant. Time to update your compiler. ;-)
I hate to recommend this but in your case (old untouchable machine) I can offer a suggestion. The tr1 version of the random library should be available for g++-4.4:
#include <tr1/random>
int main(int argc, char *argv[]){
std::tr1::default_random_engine generator;
return 0;
}
There have been improvements in the std version relative to tr1 version but you should be able to use most <random> features.
You don't even need C++0x.

How to detect the libstdc++ version in Clang?

I would like to write a "portable" C++ library in Clang. "Portable" means that I detect (in C preprocessor) what C++ features are available in the compilation environment and use these features or provide my workarounds. This is similar to what Boost libraries are doing.
However, the presence of some features depends not on the language, but on the Standard Library implementation. In particular I am interested in:
type traits (which of them are available and with what spelling)
if initializer_list being constexpr.
I find this problematic because Clang by default does not use its own Standard Library implementation: it uses libstdc++. While Clang has predefined preprocessor macros __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, they are hardcoded to values 4, 2, 1 respectively, and they tell me little about the available libstdc++ features.
How can I check in Clang preprocessor what version of libstdc++ it is using?
Clang does come with its own standard library implementation, it's called libc++. You can use it by adding -stdlib=libc++ to your compile command.
That being said, there are various ways to check Clang/libstdc++ C++ support:
Clang has the __has_feature macro (and friends) that can be used to detect language features and language extenstions.
Libstdc++ has its own version macros, see the documentation. You'll need to include a libstdc++ header to get these defined though.
GCC has its version macros which you already discovered, but those would need to be manually compared to the documentation.
And also, this took me 2 minutes of googling.
This is what I think would help. It prints the value of the _LIBCPP_VERSION macro:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
cout<<"Value = "<<_LIBCPP_VERSION<<endl;
return 0;
}
Compile it again the version of clang you want the info for.

Can't access std::move(...) C++ 11

I want to use the std::move function in <utility>. So I put an include for this at the top of my code along with all the others. However, when I use the move function eclipse underlines it as red and it won't compile. I know I am using c++ 11 since I can declare move constructors however, this won't work. I am using GCC to compile and I used the -std=c++11 option. I also put this in my linker. Before that #include <utility> would not show up. Do I need to include something else?
Here is the basic prolbem. std::move(...) does not seem to be defined.
#include <utility>
#include <vector>
int main()
{
std::vector<int> v1;
std::vector<int> v2;
v1 = std::move(v2); // Function move could not be resolved.
return 0;
}
Also here are the options I have set on my compiler -O3 -g3 -Wall -c -fmessage-length=0 -std=c++11
Your code is OK. But you should update your gcc to newest version. Gcc 4.7.2 don't have everything implemented from c++11 standard.
Are you sure is not defined? Perhaps your error is at linking time cause the linker cannot find the library?
Whether the case, you have to add library paths to your eclipse project settings as well as include paths.
I've compiled your example and after executing ldd on the generated binary I can see there are linking dependences, I hope this help: