compiling simple hellow world with mingw g++ - c++

I am new to mingw/C++ and would like to compile and run the following code I found online:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Unfortunately, running a g++ helloworld.cpp -o helloworld.exe throws the following error:
In file included from c:\mingw\include\_mingw.h:73,
from c:\mingw\include\wchar.h:53,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\cwchar:44,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\postypes.h:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
from helloworld.cpp:1:
c:\mingw\include\w32api.h:59:10: fatal error: sdkddkver.h: No such file or directory
#include <sdkddkver.h>
^~~~~~~~~~~~~
compilation terminated.
I have the impression this is related to Windows SDK and triggered by #include <iostream>. What is <iostream>, why is it important here and how can I get it to compile with g++?
EDIT
g++ version information
D:\TEMP\mingw_example>g++ --version
g++ (MinGW.org GCC-8.2.0-5) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Related

Running C/C++ code with VS code always throw "exited with code=1 in 0.123 seconds"

I am trying to set up VScode to be able to the "Code Runner" package for C and C++ code with the Code Runner package, but it would always error with:
[Running] cd "d:\Desktop\Programming\For_fun\tester\" && g++ practice.cpp -o practice && "d:\Desktop\Programming\For_fun\tester\"practice
[Done] exited with code=1 in 0.123 seconds
I have discovered that compiling and running with wsl and ubuntu would work, but regular command prompt wouldn't. I presume that is a big indicator of what the problem is, but I have no idea how to resolve it or even start googling.
I am on Windows 10
This is my code:
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello World!\n";
return 0;
}
And these are the results of me trying to confirm whether I have the relevant packages installed:
PS D:\Desktop\Programming\For_fun\tester> c++ --version
c++.exe (MinGW.org GCC Build-20200227-1) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
PS D:\Desktop\Programming\For_fun\tester> gcc --version
gcc.exe (Rev5, Built by MSYS2 project) 10.3.0
Copyright (C) 2020 Free Software Foundation, Inc.
PS D:\Desktop\Programming\For_fun\tester> g++ --version
g++.exe (MinGW.org GCC Build-20200227-1) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Thanks!
It doesn't work. But I fixed the problem: Basically, I used the windows finder to find all instances of gcc.exe and deleted the folders within my program files that house them. Then I removed their paths from my environment variables. Finally, I did a clean reinstall of gcc-core and gcc-g++ through cygwin and it worked

g++.exe: installation problem, cannot exec `as': No such file or directory

I'm trying to learn C++, I downloaded and installed g++ on Windows using Cygwin, following this tutorial:
https://www.cs.odu.edu/~zeil/cs250PreTest/latest/Public/installingACompiler/#installing-the-mingw-compiler
Just like in the tutorial, at the end, I check that g++ is installed:
PS D:\Desktop\coursera_ODS_in_c-\Week 2> g++ --version
g++.exe (GCC) 3.4.5 (mingw-vista special r3)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
and then I try to run this code:
#include <iostream>
int main() {
int num = 7;
std::cout << "Values:" << num << std::endl;
std::cout << "Address: " << &num << std::endl;
return 0;
}
by using this
PS D:\Desktop\coursera_ODS_in_c-\Week 2> g++ main.cpp
but I get this:
g++.exe: installation problem, cannot exec `as': No such file or directory
Do you how to solve this ?
The message:
g++.exe: installation problem
clearly states that something's wrong with the installation of your G++ compiler. Also, 3.4.5 version is too old. The current stable release is 10.2 (released on July 23, 2020), reinstalling it with the latest version will probably solve your issue. (Since insufficient details are given, the error cause is still a mystery.)
Try downloading the GNU GCC compiler which comes with the latest edition: GNU GCC.
OTOH, the program is coded properly, no error reported on OnlineGDB.

Hello World in C++ Using a Conda Build Environment

I'm setting up a shared conda build environment to help me collaborate with a colleague.
I'm fairly new to conda and have only used it for python programming before. But now I'd like to use it for c++ projects too.
I've run conda install -c gcc which has installed gcc and g++. I can verify this with the output of g++ --version.
>g++ --version
g++ (GCC) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
When I do this outside my conda environment I get this:
>g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
So I know that the install has worked.
I have this hello world code:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
And try compiling it like this:
g++ main.cpp -o main
But I get this error message:
g++ main.cpp -o main
In file included from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/bits/postypes.h:40:0,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/iosfwd:40,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/ios:38,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/ostream:38,
from /Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/iostream:39,
from main.cpp:1:
/Users/me/anaconda/envs/VolumeEstimation/gcc/include/c++/cwchar:44:19: fatal error: wchar.h: No such file or directory
#include <wchar.h>
^
compilation terminated.
How can I best resolve this?

Using SDL2 with g++ / MinGW crashes on launch

I am working on a game and recently made the transition from SDL 1.2 to SDL2 (kinda late to the party but heh). Despite having no compilation or linking error whatsoever, the program crashed on launch while stating (translated from French) "The application failed to start up correctly". At first I thought it was my own fault, but then I got suspicious and put together a quick SDL2 test, which indeed after flawless compilation crashed on launch.
Here is the code I've been using for this example (you can't make it more simple) :
#include <SDL.h>
int main(int argc, char *argv[])
{
return 0;
}
And the compilation line is g++ main.cpp -ISDL2-2.0.4\x86_64-w64-mingw32\include\SDL2 -LSDL2-2.0.4\x86_64-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2 -otest.exe -fpermissive -std=c++11 -Wno-write-strings -Wno-overflow
For what it's worth :
> g++ --version
g++ (x86_64-win32-seh, Built by MinGW-W64 project) 6.1.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I'm running MinGW-w64 on Windows, as you can see. The OS is Windows 8.1.
Welp. I actually used the wrong SDL2.dll. I feel dumb now.

Why doesn't g++ (4.9.2) compile (a very simple program)?

My overall problem is trying to install HEALPix C++ version and, to isolate the problem, I created foo.cpp:
#include <string>
#include <iostream>
I then run
g++ foo.cpp
to which I get the following error message:
foo.cpp:1:18: fatal error: string: No such file or directory
#include <string>
^
compilation terminated.
I am running
g++ (GCC) 4.9.2 20141029 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
downloaded from http://hpc.sourceforge.net/
on OS X Yosemite 10.10
I get the same error message with gcc 5.0 and 4.8 as well. I tried downloading gcc 4.9 from homebrew but this also doesn't work (though with a different error message).
I then try
g++ -I/usr/local/include/c++/4.9.2 foo.cpp
to which I get the error message:
In file included from foo.cpp:1:0:
/usr/local/include/c++/4.9.2/string:38:28: fatal error: bits/c++config.h: No such file
or directory
#include <bits/c++config.h>
^
compilation terminated.
Now, obviously, I don't want to have to find (recursively) the directory of each required header (for my HEALPix installation, this could take forever!) It seems to me that there is a problem with where #include is looking? How can I change this? Also, if the workaround is to specify the directories of required headers, it is not immediately obvious to me how to specify this within my configuration for the HEALPix installation.
Hope someone can suggest a solution!
Thanks