GCC iostream fstream error in Ubuntu 13.10 - c++

I am using Ubuntu 13.10. I am getting some errors for the following code.
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
int main(int argc, char *argv[])
{
error.set_program_name(argv[0]);
if ( argc != 2 )
{
// printf(argv[0] + " usage: fifo_client [string] \n");
/// cout << argv[0] << " usage: fifo_client [string]" << endl;
exit(EXIT_FAILURE);
}
ofstream out(fifo_file);
if(out)
out << argv[1] << endl;
return(EXIT_SUCCESS);
}
If I run the above program a.c using command
gcc a.c -o a
a.c:1:20: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.
I don't know whats the problem.

Use g++ instead of gcc. gcc could compile a c++ file if it had the right extension (.cpp for instance) or with the right arguments (-x c++) but adding the arguments needed to link with the C++ libraries is far too complex to avoid the simple solution.

The problem is that you're mixing C & C++ code and compiling it using GCC.

try
#include <fstream>
using namespace std;
instead of #include <fstream.h>
anyway your source code is not full to make correct suggestion.

I ran your code in my compiler and got following error :-
test2.c:3:21: fatal error: fstream.h: No such file or directory
#include <fstream.h>
^
compilation terminated.
so i think your question has typo.
It is because you are mixing c and c++ code, fstream is part of c++. try to run by g++.

Related

Why does codeblocks ide raise: fatal error: iostream: no such file or directory

when I run this test code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << end1;
return 0;
}
with a more than less than around iostream just can't use it on stackover flow.
I got this code from an old book (2014) it might be outdated. I've never used C++ before how do I fix this.

Where does Homebrew install C++ packages?

I would like to use C++'s std::format library to format strings. See the minimal working example below.
/* example.cpp */
#include <iostream>
#include <format>
#include <string>
int main() {
std::string s = std::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
However, when I compile my code, I get the following error message:
example.cpp:2:10: fatal error: format: No such file or directory
2 | #include <format>
I am using the latest version of macOS, and I have Homebrew installed as my package manager. I already installed clang-format through Homebrew, but for some reason, my compiler can't locate the header file. Can somebody help me figure out the problem is? I have tried using Apple's GCC and the custom GCC10 provided by Homebrew, but in both cases, I get the same error message. Is this a Homebrew issue or a C++ issue?
Following this answer to use fmt, you can install the library with brew install fmt, then modified the code to
/* example.cpp */
#include "fmt/format.h"
#include <iostream>
#include <string>
int main() {
std::string s = fmt::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
and compile with
clang++ -std=c++11 test.cpp -lfmt

Compiler Error Assumed value of MB_LEN_MAX wrong

I'm currently trying to run this image segmentation program, but I'm having errors when I try to compile it. To compile, you just make, which runs the command
g++ -g -O3 -I. -o segment segment.cpp -lm
However, it runs into a compilation error -- its output is
In file included from /usr/include/wchar.h:887:0,
from /usr/include/c++/6/cwchar:44,
from /usr/include/c++/6/bits/postypes.h:40,
from /usr/include/c++/6/iosfwd:40,
from /usr/include/c++/6/ios:38,
from /usr/include/c++/6/istream:38,
from /usr/include/c++/6/fstream:38,
from ./pnmfile.h:27,
from segment.cpp:23:
/usr/include/x86_64-linux-gnu/bits/wchar2.h:448:3: error: #error "Assumed value of MB_LEN_MAX wrong"
# error "Assumed value of MB_LEN_MAX wrong"
^~~~~
Makefile:14: recipe for target 'segment' failed
make: *** [segment] Error 1
Here is the relevant area of /usr/include/x86_64-linux-gnu/bits/wchar2.h
/* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
But this would only disturb the namespace. So we define our own
version here. */
#define __WCHAR_MB_LEN_MAX 16
#if defined MB_LEN_MAX && MB_LEN_MAX != __WCHAR_MB_LEN_MAX
# error "Assumed value of MB_LEN_MAX wrong"
#endif
if (__bos (__s) != (size_t) -1 && __WCHAR_MB_LEN_MAX > __bos (__s))
return __wcrtomb_chk (__s, __wchar, __ps, __bos (__s));
return __wcrtomb_alias (__s, __wchar, __ps);
}
And the relevant areas of segment.cpp:
#include <cstdio>
#include <cstdlib>
#include <image.h>
#include <misc.h>
#include <pnmfile.h> //<-- Line 23 of segment.cpp, where the error occurs
#include "segment-image.h"
and pnmfile.h:
#include <cstdlib>
#include <climits>
#include <cstring>
#include <fstream> //<-- Line 27 of pnmfile.h where the error occurs
#include "image.h"
#include "misc.h"
Does anyone know what's causing this error (and if so, how to fix it)? I'm running Ubuntu 16.04 with g++ 6.4.0. Thank you for any help you can give!
EDIT: I've confirmed that fstream does work. I compiled the following
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main() {
ifstream is("in.txt");
int a, b;
is >> a >> b;
cout << a << endl;
cout << b << endl;
return 0;
}
with the same command
g++ -g -O3 -I. -o temp.exe temp.cpp -lm
and it successfully printed out two integers read from a text file.
You need to include limits.h from the same libc from which your bits/wchar2.h is coming. If you check the comment in the latter file just above the #error which you are getting, you'll see why - it serves as a safeguard against mixing incompatible header files.
This kind of longtime issues is my motivation to build my own Linux distro.
Googling gets you nowhere. But ... building your own (cross) compiler
solves the problem. Compiler has it own definition in his relevant headers MB_LEN_MAX 1
MB_LEN_MAX 16 in glibc includes. No problem.

Simple netbeans C++ project doesn't compile

I installed Netbeans and as C++ compiler I installed cygwin. I made a simple project to test out my installation, this is the code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout << "test";
return 0;
}
This is the error message that it gives: http://pastebin.com/jRRh7MPi
I hope you guys can help me out.
You need to either explicitly link to C++ standard library, or compile using g++ instead of gcc.

Boost precompiled headers problem

I try to precompile Boost headers.
First experiment - with std:: headers. I create file std.hpp:
#include <vector>
#include <iostream>
// And other std:: headers
After that:
g++ std.hpp
Copy std.hpp.gch in /usr/include/c++/4.4.5
And write test program:
#include <std.hpp>
int main() {
std::cout << "Hello, precompiled world!" << std::endl;
return 0;
}
Works fine.
Now try precompile Boost headers.
I create boost.hpp file:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
After that:
g++ boost.hpp
Copy boost.hpp.gch in /usr/local/include/boost
And write test program:
#include <boost/boost.hpp>
int main() {
// Some code...
return 0;
}
But got error:
main.cpp:2:33: error: /usr/local/include/boost/boost.hpp: No such file or directory.
Try, for experiment:
#include </usr/local/include/boost/boost.hpp>
int main() {
// Some code...
return 0;
}
Same error.
Try copy boost.hpp.gch in another place - same error.
If I put file boost.hpp in same place - works fine (so there is no problems with path):
ls /usr/local/include/boost | grep boost
boost.hpp
boost.hpp.gch
So compiler use boost.hpp header. But why compiler don't see precompiled boost.hpp.gch??
This might be a gcc bug as documented in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46110