Why g++ compiler is not able to find unique_ptr? - c++

I am trying to compile a small C++ code which invloves unique_ptr as given below.
#include <iostream>
#include <memory>
using namespace std;
int main()
{
unique_ptr<int> p1(new int);
}
when I tried to compile the code using g++, it is throwing up 'unique_ptr' was not declared in this scope. I was trying to compile on Linux box. even I tried with '-std=c++11' option. It was saying 'unrecognized command line option -std=c++11'. Can any one please let me know how to fix this?

You need to include it, it comes out of the <memory> library
#include <memory>

According to the GCC 4.4 release notes, unique_ptr was not in GCC's standard C++ library before 4.4.
So you might want to check your GCC version first, using g++ --version like #40two said.

Related

Why clang on Mac automatically includes some missing headers?

I noticed that clang++ includes a missing header - <limits> on Mac, while g++ shows errors about it on Linux. Now I wonder why clang does it, and gcc not. And how I can force clang to not do it.
Here is a sample code which compiles by clang on Mac, but not by gcc on Linux:
#include <iostream>
using namespace std;
int main()
{
cout << "int max: " << numeric_limits<int>::max() << endl;
}
UPD
I looked into libraries and here is what I found.
Internally <iostream> includes <istream>, which defines >> operator for different types. <istream> wants to know limits for short, int and streamsize types.
clang++ uses libc++ standard library, which uses std::numeric_limits class template from <limits> in <istream> for this purpose. That's why this header is included automatically when <iostream> is included.
g++ uses libstdc++ standard library, which uses __gnu_cxx::__numeric_traits class template from <ext/numeric_traits.h> instead of using <limits> in <istream> (<bits/istream.tcc>). There is also a comment in that header which explains why they don't use <limits>:
<limits> is big and we avoid including it
Used compilers:
> clang++ --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
$ g++ --version
g++ (Debian 4.9.2-10) 4.9.2
In C++, unlike C, standard headers are allowed to #include other standard headers. That sometimes leads to mysterious errors like the ones you're seeing: one compiler's <iostream> includes <limits> and the other doesn't. The solution is to always include the headers needed for whatever names you use. In this case that means to #include <limits> in your code, even though it compiles okay as is with one compiler. There's no harm in #include a header that's already been pulled in, so that's okay with both compilers. It's annoying sometimes, but that's just the way things are.
The clang version of the <iostream> header likely #includes the <limits> header, as such you get it automatically as part of #includeing the <iostream> header.
There's nothing you can do about it. That's how that compiler's library is implemented. You can simply add
#include <limits>
to this file, and it should compile on both platforms.

geany: C++ Including libraries and headers

I'm very new in Ubuntu and programming C++ on Ubuntu using Geany.
The problem I have here is that:
the classes i want to iclude to my project will receive an error,
I type,
#include <vector>
the error given here is,
fatal error: vector: No such file or directory
also I cannot use namespace std,
typing using namespace std returns the following error,
error: unknown type name 'using'
Here is the code:
#include <stdio.h> //no problem here
#include "stdlib.h" //no problem here
#include <vector> //this is a problem (lets say it returns error 1)
using namespace std; //this is a problem (lets say it returns error 2)
int main(int argc, char **argv)
{
return 0;
}
This sounds like you are using the wrong compiler to compile your C++ code. For example, by invoking gcc test.cpp the C++ file is actually compiled as C and you receive errors such as the one you posted - there is no vector header in C and there is also no using keyword.
If you are using gcc, the correct way to invoke the compiler to compile C++ is via the g++ symlink, i.e. g++ test.cpp
If you are using clang, the executable is called clang++ instead.
Both compilers support the -x parameter to manually change the language to C++, although in that case you also have to specify that the compiler needs to link your files with the C++ standard library. For example: gcc -x c++ test.cpp -lstdc++

c++: How to remove libstdc++.so.6 dependencies

I have 2 program I wrote on my windows computer using Visual Studio 2013. They run fine and work perfectly on my computer, but when I brought them over to my school account that is on a Linux machine, a problem arose. They compile and 1 ran, but the other did not. The one that did not run gave me an error:
.../lib/compat/libstdc++.so.6: version CXXABI_1.3.2 required by...
I have been doing research and I can't seem to find out what in my program would be using libstdc++.so.6, I'm not even really sure what it is or does. Since I am on a student account I can't go installing it using sudo, and it is a homework so I can't submit it using my own libraries.
Any Idea on what my program might be using that would require libstdc++.so.6?
I have 3 files: main.cpp, LinkedList.cpp and LinkedList.h.
I think it might be in main.cpp because I think it stems from a library I am including and main.cpp is the only one that uses outside libraries. Here is the list of libraries it uses:
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <ctype.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <bitset>
#include <algorithm>
#include "LinkedList.h"
Thanks in advance!
You are trying to run a program linked against one version of the libraries under another set. That should work fine as long as the library versions aren't too far apart. In your case, the difference between libraries is just too large.
GCC (C++ in particular) has changed quite a bit lately, some programs that used to compile and run fine now blow up or don't compile at all (due to language changes, compiler bugs accepting broken code, ...), and the library ABI has also changed. Your best bet is to carry source code around, and make sure you got compatible language versions on both ends. If that is inconvenient, a solution is to make sure you have the same compiler (and other environment) at both places. The easiest way to get this is to install the same distribution and version.
First you can't remove the dependencies of libstdc++.so.6, because it's a standard C++ library.
To solve your problem you have to check whether your libstdc++.so have the right version
strings /usr/lib64/libstdc++.so.6|grep GXXABI_1.3.1
if there have no matching version, you will have 2 methods like these:
update your gcc on your school's linux OS
yum intsall gcc
download a matching libstdc++.so from this website:
download gcc || download matching libstdc++
then replace the libstdc++.so to /usr/lib64/libstdc++.so.6.*
SOLUTION
I went through a few steps to find my solution. Originally I could compile my program but could not run it.
1) My first step to solve the issue was to change my method of compiling. Originally I compiled my program with the following: g++ main.cpp LinkedList.cpp -o output. I changed it to: g++ -static main.cpp LinkedList.cpp -o output which allowed me to compile and run. This worked but static is a method to dynamically link libraries. This prevents linking with the shared libraries. This is not a good solution because it takes a lot longer and increases the file size of the executable, so I wanted to improve.
2) The second thing I did was remove using namespace std. Yes, I cheated and used it. So I went through my program and added std:: to the appropriate places.
3) The last thing I did was clean up my code. I was using a lot of libraries because my program was a large and complicated program. I was using all of the libraries I had listed in my original post. I went through my code and anywhere I was using a function from a library I would try and write my own code that would do the same thing which would result in my program not depending on those libraries. I was able to replicate a decent amount of these dependent foreign functions with my own which added lot of code, but it allowing me to remove some of these includes. My list of includes is now:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "LinkedList.h"
#include <math.h>
I am not sure exactly which step resolved my issue, but now I can compile with my preferred method, g++ main.cpp LinkedList.cpp -o output, and my program runs fine.
I hope this helps someone.

fstream does not function in Qt Creator

I am new in Qt. I have my project coded in visual C++ 2010 express edition which works totally fine. Then, I want to use .cpp files that I already created in Qt console application (similar to my project in VC++2010). Compiling fails at "ofstream" and "ifstream" with this error:
"no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::basic_strinstream<char>::_string_type)'"
"no matching function for call to 'std::basic_ifstream<char>::basic_ofstream(std::string&)'"
I have included fstream to the cpp file as:
#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
and the piece of the code which fails is as follows:
stringstream s1; s1 << "Estimated_NN_ThrMWh.csv";
ofstream outF1(s1.str());
By the way, I am using "MinGW 4.9.1 32bit" as my compiler. What is the issue and how can I fix it? Thanks for your help.
You are passing a std::string to the std::ofstream constructor. This is a C++11 feature and to use this you need to pass -std=c++11 to GCC or Clang. MSVC automatically compiles for its hybrid not-quite-C++11-or-anything-else language that the compiler release compiles.
If you're using Qt 5's qmake, you can do just CONFIG+=c++11 and you're good to go. Otherwise you'll need something like
*gcc*:QMAKE_CXXFLAGS += -std=c++11
If you don't want to depend on C++11, don't use it, and just do:
std::string filename("Estimated_NN_ThrMWh.csv");
std::ofstream outF1(filename.c_str());
which will always work. Note I removed the stringstream because, at least in the short code you show, it's superfluous.
you should always write as following:
string s = s1.str();
ofstream outF1(s);
And, in my opinion, You code works under MSVC should be considered as a bug of MSVC (which allows non-const reference bind to a temporary object).

G++ not finding <iostream.h> in Ubuntu

I just installed Ubuntu and tried making the famed "Hello World" program to make sure that all the basics were working. For some reason though, g++ fails to compile my program with the error: "'cout' is not a member of 'std'". I've installed the build-essential package. Am I missing something else?
#include <iostream.h>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Looks pretty good to me...
Use #include <iostream> - iostream.h is not standard and may differ from the standard behaviour.
See e.g. the C++ FAQ lite entry on the matter.
The standard header is called <iostream>, not <iostream.h>. Also, it is agood idea to compile your C++code with the -Wall and -pedantic flags, which can point out lots of errors with non-standard code that g++ would otherwise ignore. Use:
g++ -Wall -pedantic myprog.cpp
Sounds like it did find iostream.h but it does not define cout in the std namespace. It is there for backwards compatibility with older programs that expect cout to be in the global namespace.
use
#include<iostream>
using namespace std;
without namespace you won't be able to use cout or cin