C++ hello world not working? - c++

I am new to C++ and I am self-teaching. I am using code::blocks, and have a question. When I write hello world, nothing happens. This is what is says in the debugger window:
Building to ensure sources are up-to-date
Selecting target:
Debug
ERROR: You need to specify a debugger program in the debuggers's settings.
(For MinGW compilers, it's 'gdb.exe' (without the quotes))
(For MSVC compilers, it's 'cdb.exe' (without the quotes))
and here is the program:
#include <iostream>
using namespace std;
int main()
{
cout << "Helloworld!" << endl;
return 0;
}
also if you need it, here is the build log:
"hello world again - Debug": The compiler's setup (GNU GCC Compiler) is invalid, so Code::Blocks cannot find/run the compiler.
Probably the toolchain path within the compiler options is not setup correctly?!
Goto "Settings->Compiler and debugger...->Global compiler settings->GNU GCC Compiler->Toolchain executables" and fix the compiler's setup.
Skipping...
Nothing to be done (all items are up-to-date).

Try to download the debugger and the compiler again. It worked for me when I had this problem. Try downloading the 'gdb.exe' from the internet.

Related

Why does string type cause c++ program to not run?

I have a simple program, written in C++, on a Windows 10 machine, compiled with the MinGW g++ compiler.
I am including the <string> header, and it runs fine. When I include the string data type, it will compile, but it will not run.
The minimum amount of code to reproduce this is:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting;
cout << "hi" << endl;
return 1;
}
Please note, this works fine:
#include <iostream>
#include <string>
using namespace std;
int main() {
//string greeting;
cout << "hi" << endl;
return 1;
}
The version of the compiler is 6.3.0
g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
I downloaded it 3 days ago, so I assume it is the most recent version. (Maybe not?)
I have googled and browsed stackoverflow for answers.
The closest question I could find, the person gave up and changed operating systems.
One other solution I found was to use Cygwin's compiler. I would rather not, as I already have a seemingly otherwise fine compiler. It seems that MinGW tools are widely used enough that I should be able to use the compiler.
Any other forum/blog/etc resources have problems about converting strings or calling string methods.
NOTE: The same exact code runs fine on Ubuntu 16.04, with the included compiler.
EDITS:
While I realize that "It does not run" is not helpful, I don't know how else to describe it. I run the compiled program, and it behaves the same as if I entered echo ''. There is no output, no indication that anything has happened. What is the most helpful way to phrase that behavior?
If I use a debugger, I get program exited with code 0xc0000139
A quick google search returns results indication that it is a problem with the compiler. Same as the comments below about my compiler version...
To compile the program, I run g++ main.cpp -o b.exe
To execute it, I run ./b.exe
Mingw has a long standing issue with certain consoles (see their faq). It might be worth checking whether it's failing to output rather than execute by e.g. redirecting to a file ./b.exe >out.txt.
After some useful comments, and much frustration, I decided to try to build the most current compiler. I caved and used Cygwin. (I really have no problem with Cygwin, I just wanted to get MinGW to work.)
I followed this site's instructions (after downloading the current source from https://gcc.gnu.org).
http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/
Notes:
This is for an older version of gcc, but I replaced the appropriate version numbers with 8.2.0 (current at this time).
I was missing some prerequisites, and had to cd into the source directory and run ./contrib/download_prerequisites from the Cygwin terminal. It handled everything seamlessly, and while it took a little bit, I am now able to use string data types and run the program (successfully).
Don't forget the final make install command. I did, and it was a headache.
Lastly, thanks to all the helpful comments, and those that asked for clarification. When I call the mechanic, and say, "my car won't run", they ask helpful questions, and we work together to get them the relevant information they need. That's what happened above, and I learned some things (and solved my issue).
I had the same issue. I downloaded mingw a few days from sourceforge and everything complied fine except when I declared a string. Then it would show no output. Maybe it was some problem with the old version (6.3.0). I deleted all the files and re-installed using the given tutorial : https://code.visualstudio.com/docs/cpp/config-mingw . This is a newer version (8.1.0). Now it works just fine!

Function to_string() was not resolved using g++ mingw--w64 5.3.0

In Eclipse I am using mingw-w64 V5.3.0 as the compiler and I have enabled C++11 (which was the solution in the following related post here and here). My compilation command looks as follows (from eclipse console window):
g++ -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0 -o "src\Launch.o" "..\src\Launch.cpp"
The simple code I am attempting to run is as follows:
#include <stdio.h>
#include <iostream>
#include <string>
int main()
{
std::string test = std::to_string(0);
std::cout <<"HI" << std::endl;
return 0;
}
The program compiles but for an error stated in the question. Running it without the to_string line works fine. There have been suggestions back in 2012 that MingW has a problem with to_string(), but was resolved in later versions has shown here.
Screen shot below:
And the console output is as follows:
The following is the error I receive when running the code from the .exe directly:
As mentioned in the comments, the issue is a linking issue, however it is linking correctly to iostream which is in the same directory as string.
Eclipse was looking in System32 for the library and driver files, despite PATH and Eclipse pointing to the MingW64 compiler on the computer. Eclipse is also showing it is linked to the MingW64 libraries correctly as the path to the headers when right-clicking and opening deceleration is shown to be correct. Why then it looks in System32 for the library at run-time I don't understand.
The problem was 'solved' by copying the entire MingW64 compiler driver folder into the System32 folder on Windows.

Eclipse g++ not found in path: windows

I am attempting to setup SDL2 for C++ with Eclipse on Windows 7.
In order to do so, I am following the tutorial in this link, which states that I must first install MinGW. So I follow the link provided in order to setup MinGW. I follow all the steps without issue. I then open Eclipse and attempt to build a simple hello world program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
To my surprise, this code doesn't build, with 6 errors.
I then proceed to simplify the program further:
int main()
{
return 0;
}
This also does not compile. There are two errors:
Program "g++" not found in PATH
Program "gcc" not found in PATH
Here is a screenshot.
However, my path does contain "C:\mingw\bin". I have also tried changing this to "C:\mingw". Looking inside "C:\mingw\bin", I find gcc and g++:
In addition, compiling a test program using the command line (g++ Test.cpp -o Test) works just fine, as does "g++ -v".
I have been searching the web for hours, and can't seem to find an answer as to why Eclipse can't seem to compile anything with MinGW. Questions I have looked at on SO (which haven't been able to fix my issue) include:
Eclipse mingw binary not found
Eclipse not finding c std libraries
g++ not found in path
Eclipse C++ : "Program g++ not found in PATH"
Program g++ not found in path
Program g++ not found in path C++ [duplicate]
Eclipse CDT (Juno) in Win7: Cannot find g++ in PATH, iostream unresolved and other fun stuff
Additional info:
Window > Preferences > C/C++ > Build > Settings > "CDT GCC Built-in Complier Settings MinGW [Shared]" : Toolchain MinGW GCC is not detected on this system.
I have also reinstalled Eclipse to no avail.
I realize that this may be a duplicate question of some that I have linked, but the information in previous questions have not been able to fix my problem, and I fear that adding a comment to an old question may not result in an answer.
Please request additional information as needed.
You need to set the environment for the c/c++ builder.
First you need to install the GNU tool-chain, you can choose either MinGW or Cygwin. You can see the steps here. I used MinGW.
Go to Window->Preferences->C/C++->Build->Environment and add a new variable, name it whatever you want for example a named it "MINGW", now paste the binaries directory of MinGW which is by default C:\MinGW\bin, you should have something like this:
Now when you create a new project you just have to select the MinGW tool-chain:
Hope that helps.
It appears as though I have fixed the problem for the moment.
In case others encounter the same issue:
Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Command changed from "g++" to "C:\mingw\bin\g++".

Setting up MingW and Code::Blocks in Windows 8 64 bit

I am trying to setup MingW and Code::Blocks on my Windows 8 64 bit laptop, and I'm facing some problem while building a main.cpp file. These are the versions that I have installed:
x86_64-w64-mingw32-gcc-4.7.4-release-win64_rubenvb.7z for MingW (4th one in that list), and
codeblocks-12.11-setup.exe for Code::Blocks.
I've set the path to mingw64\bin in the environment variable. Also, in the Code::Blocks compiler settings, I have set path for all ToolChain Executables. There are two gcc in the MingW bin path. One is - gcc.exe and other is - x86_64-w64-mingw32-gcc.exe. Same for C++ Compiler, Linker for static and dynamic libs. Now when I try to build a simple Hello World file, it shows some errors:
obj\Debug\main.o -- In function swscanf
c:\mingw\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\4.7.3\..\..\..\..\x86_64-w64-mingw32\include\wchar.h -- undefined reference to `__gxx_personality_sj0'
obj\Debug\main.o -- In function `wscanf':
c:\mingw\mingw64\bin\..\lib\gcc\x86_64-w64-mingw32\4.7.3\..\..\..\..\x86_64-w64-mingw32\include\wchar.h -- undefined reference to `__gxx_personality_sj0'
Similarly there are many errors and undefined references for - fwprintf, wprintf, std:cout, etc..
I'm sure there is some configuration problem, but I cannot find out what's the issue. Can any one take a look at the problem.
Here's the code I'm running:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
If you don't require 64 bit support, consider downloading "codeblocks-12.11mingw-setup.exe"
It packs its own 32 bit mingw and has everything preconfigured for you.
Otherwise, if you still need to compile amd64 apps:-
Assuming you extracted to D:\Mingw64, under the "Tool Chain Executables" tab, set the "Compiler's Installation Directory" to "D:\Mingw64\bin", Then click auto-detect to check whether code::blocks likes what it finds.
If everythings Ok, make sure the rest of the fields are as follows:-
C compiler: x86_64-w64-mingw32-gcc.exe
C++ compiler: x86_64-w64-mingw32-g++.exe
Linker for dynamic libs: x86_64-w64-mingw32-g++.exe
Linker for static libs: x86_64-w64-mingw32-ar.exe
Debugger: GDB/CDB Debugger...
Ressource compiler: x86_64-w64-mingw32-windres.exe
Make program: mingw32-make.exe
Click on the "Additional Paths" tab and enter the following line:- (the path is correct for ver 4.7.4,the one you downloaded. It ends with 4.7.3)
D:\Mingw64\libexec\gcc\x86_64-w64-mingw32\4.7.3\
Click on the "Search directories" tab to the imediate left "Toolchain Executables"
Add the following paths:-
D:\Mingw64\include
D:\Mingw64\x86_64-w64-mingw32\include
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3\backward
D:\Mingw64\x86_64-w64-mingw32\include\c++\4.7.3\x86_64-w64-mingw32
D:\Mingw64\lib\gcc\x86_64-w64-mingw32\4.7.3\include
Click on the "linker" tab and add the following paths:-
D:\Mingw64\Lib
D:\Mingw64\x86_64-w64-mingw32\lib
And finally click on the "Compiler Settings tab", Click on "Other options" and add:-
-m64
One last thing-> remember to save those settings to disk!
If it crashes while you test this new config, you might wish you had!
Do this (I think) either by "File->Save Everything" or restarting code::blocks ...without another instance running.
Try compiling something then share the outcome
Peace! Dear brother, I think you need this:
(1)
std::cout << "Hello world!" << std::endl;
You need to put "std::function_name" whenever you are using a predefined function from iostream.h, because this is you way access it in GCC, which Code::Blocks uses.
(2) Try it compiling by omitting "using namespace std;" from your code.
Because I've compiled C and C++ code many times in Code::Blocks on 32-bit Windows XP, and it did!
(3) Make sure the path is set to the directory "mingw\bin".
(4) Ensure if you have 64-bit MinGW installed and path set to its "bin" directory (if you want to compile for 64-bit). It will compile for 64-bit by default.
(5) Try the flags/arguments "-m32" and "-m64" for compiling for 32- and 64-big respectively.

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)).