Commands 'cin' and 'cout' not working - c++

I installed MinGW on Windows 8.1 to code in C++ and I am trying to run my codes using Sublime Text 3. So far I am successful, but I am not being able to use cin and cout. I know I can use scanf and printf, instead. But I might need to use cin and cout as well. When I am trying to build a C++ code containing cin or cout, it gives a compile time error. Let's see at a piece of code which generates such an error:
#include "iostream"
#include "cstdio"
using namespace std;
int main()
{
int n;
std::cin >> n;
std::cout << n;
}
The error coming up on my machine is: (Note:I have WinGHCi installed on my machine to use Haskell)
Info: resolving std::cin by linking to __imp___ZSt3cin (auto-import)
Info: resolving std::cout by linking to __imp___ZSt4cout (auto-importc:/program files (x86)/haskell platform/2013.2.0.0/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: warning: auto-importing has been activated without --enable-auto-import specified on the command line.
This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.)
[Finished in 1.4s]
Please help me to find what is wrong. I deliberately don't want to use IDEs.
Note that a general suggestion is to use <iostream> instead of "iostream". In this regard, using <iostream> produces the following error on compiling the same code (with "iostream" and "cstdio" replaced by <iostream> & <cstdio>):
Info: resolving std::cin by linking to __imp___ZSt3cin (auto-import)
Info: resolving std::cout by linking to __imp___ZSt4cout (auto-importc:/program files (x86)/haskell platform/2013.2.0.0/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: warning: auto-importing has been activated without --enable-auto-import specified on the command line.
This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.)
[Finished in 1.8s]

These type or errors is often throwned when you are using:
headers with version x
----------------------
lib with version y
So, check this.
update
If you're using compiler with version 4.6.3 but you're using header of 4.6 (for any reason), you got these kind errors.
Useful command:
which g++ #locate where is your compiler (generally /usr/bin/g++)
g++ --version #get compiler's version
find / -name iostream | grep c++ #find where're your includes

Related

Why cannot I compile my code with g+ in my terminal [duplicate]

I just started using MinGW for Windows. When trying to create executable using
g++ a.cpp -o a.exe -std=c++14
for the code below:
#include <string>
using namespace std;
int main()
{
string x = to_string(123);
return 0;
}
I'm getting following error:
C:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingwex.a(vsnprintf.o):(.text+0x0): multiple definition of vsnprintf
C:\Users\..\Local\Temp\cc4sJDvK.o:c:/mingw/include/stdio.h:426: first defined here
collect2.exe: error: ld returned 1 exit status
What is the root cause for this error and how can I make it go away? While I can easily find a replacement for to_string() function I'm not sure what is causing this error to occur in the first place.
Installing MinGW packages mingw32-libmingwex-* will link an appropriate version of vsnprintf and avoid the linker error.
This issue, i.e. multiple definition of vsnprintf, still exists in MinGW as December 2019.
After investigating a lot, I found the solution in the official mailing list.
It's a bug in mingwrt-5.2.2. Downgrading to the mingwrt-5.2.1 version solves that issue.
To do that, just input the following command:
mingw-get upgrade mingwrt=5.2.1
Then restart the MinGW shell.
Read the full story here.
Note: MinGW-w64 and MinGW are separate projects, so the accepted solution is not so helpful to me, as I want to keep MinGW and not to move to MinGW-w64.
I solved this issue using MinGW w64 compiler
download mingw-w64-install.exe
setup to Version: 6.3.0, Architecture: i686, Threads: posix, Exception: dwarf and Build revision: 2.
I hope this will be of some help.
There are multiple definitions of vsnprintf in both stdio.h and libmingwex.a. I am able to work this around by adding #define __USE_MINGW_ANSI_STDIO 0 to the start of the code, before any includes, which disables the definition of vsnprintf in stdio.h.

C++ - undefined reference to `__gxx_personality_seh0'

I am having an issue when trying to build my C++ application in Eclipse (neon.2). This application is simply supposed to create a file, print the numbers 15-210, save the file, then close. This is the error I am getting each time I try to build it:
17:59:42 **** Incremental Build of configuration Debug for project Project1 ****
Info: Internal Builder is used for build
g++ -o Project1.exe Q1.o Q2.o displayHeader.o
Q2.o:Q2.cpp:(.data+0x0): undefined reference to `__gxx_personality_seh0'
Q2.o:Q2.cpp:(.xdata+0x10): undefined reference to `__gxx_personality_seh0'
collect2.exe: error: ld returned 1 exit status
17:59:44 Build Finished (took 1s.391ms)
This only happens when I attempt to use either ofstream or ifstream in my program. I have successfully built & ran other (very simple) C++ applications that don't use this in Eclipse, but when I do, I get this error.
Here is my code:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main(){
ofstream outfile;
outfile.open("nums.txt");
for (int i = 15; i < 211; i++) {
outfile << i << " ";
}
outfile.close();
return 0;
}
I created the project with MingW, and I have the /bin folder in my environment path variable, and under Project/Properties/C/C++ Build/Environment all of the variables are set. I've been searching through forums and tutorials like crazy to try and see if I missed something during installation of either Eclipse or MingW, but I don't see any issues.
I hope I provided adequate information but please let me know if anything else could help. I've been pulling my hair out over this for almost a week. And thank you so much for any help.
You didn't do anything wrong or miss anything during installation!
I had this issue before , this occurs in MingW compiler and ofstream objects, you should change this command
x86_64-w64-mingw32-gcc [your code file]
with this :
x86_64-w64-mingw32-g++ [your code file]
In short , change gcc with c++ in Eclipse or command line.
My opinion, use MS Visual Studio for compiling C++ code in Windows, its compiler give you more features and flexibility and you could develop faster.

Clang fails to find iostream. What should I do?

Earlier, I posed a related question.
I have the following program extracted from a large project in my Mac OS
#include <iostream>
int main(){
std::cout<<"hello"<<std::endl;
return 0;
}
Compiling it with Clang fails with the following error:
$ clang test.cpp
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
For information,
A) I have already installed xcode command line tools, using xcodeselect --install. But it seems iostream does not locate in the default search path of clang.
B) Using g++ instead of clang compiles the program. But in my problem, I am not allowed to use other compiler than clang, or to change the source program.
C) I can see workaround techniques, e.g, by tweaking the search path in .bashrc or with some symbolic link, etc. But I feel reluctant to use them, because it seems that I have an installation problem with my Clang and tweaking the path only helps to avoid one of these path issues.
clang and clang++ do different things. If you want to compile C++ code, you need to use clang++
Alternatively you can invoke c++ compiler directly by providing language name explicitely:
clang -x=c++

Compile error with "cin >> a >> b" with intel c++ compile in release version

When I use the intel C++ compiler with VS2015 to compile a release version, it will fail. But when I compile a debug version, it work. I can't understand what happen. It the configure have some wrong? The error list is in below. How can I do?
--edit--
Today I compare the configure between the debug version and the release version in each option, I finally find if I set interprocedural optimization to no or single-file(/Qip), it works. But if i set the interprocedural optimization to Multi-File(/Qipo), it doesn't work. Besides, I can't find the iostream in the intel C++ compiler include path, the project seem like to use the library in VC++, it that the reason why I can't compile? How can I solve this problem? Thanks a lot.
warn #11021 unresolved __imp_?_Src#?3??_Getffld#?$num_get#DV?$istreambuf_iterator#DU?$char_traits#D#std###std###std##ABAHPADAAV?$istreambuf_iterator#DU?$char_traits#D#std###3#1AAVios_base#3#PAH#Z#4QBDB test 1
error #11023 Not all components required for linking are present on command line test 1
#include<iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)
cout << a << ' ' << b;
return 0;
}
After I asking in the intel developer zone. They tell me the issue is ONLY reproducible with /MD and /Qipo used together, so I may use /MT to work around it. And when I use /MT, it works.

Eclipse C++ on OS X

I'm trying to develop programs using C++ and Eclipse on MacOS X but I can't make it work. Please help me configure Eclipse! I have job interviews and I need to refresh my C++ skills!
I installed Eclipse CDT and GCC compiler for MacOS X and I try to compile a simple HelloWorld program (see code below).
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
I get errors and warnings and I can't seem to resolve them. Any help would be much appreciated.
Errors
Symbol 'cout' could not be resolved (Semantic Error),
Same for 'endl',
Same for 'std'.
Warnings
Error launching external scanner info generator (g++),
Error launching external scanner info generator (gcc),
Program 'g++' is not found in $PATH,
Program 'gcc' is not found in $PATH.
On OSX, the compiler comes as part of XCode. So, even if you don't want to use the XCode IDE, you have to install XCode to get the compiler (and linker (and other tools you need)).