Why does my hello world program take up 883KB? - c++

I have made a simple Hello World program in C++.
But, for some reasons it is taking up almost 900kb.
I have no idea why it is so big. I am using -s, -Os and -Bdynamic I am using Code blocks.
I tried this: GCC C++ "Hello World" program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?
My program:
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}
Yesterday i made a bigger program and it was only 21Kb.
Now every single of the executables are about 900Kb.
Info:
I tried to compile it myself using g++ Hello.cpp -Bdynamic -s -Os - Still the same size.
But when i compiled it using printf from <stdio.h> i got it to 21Kb.
I want to use <iostream>
(It is an updated version of my post)

Related

Getting stdout using vscode mingw gcc

I wanted to extent my c knowledge to c++. Using Win10, I installed VSCode and mingw following the tutorials.
Next I created a Hello World test file.
It compiles properly without errors. However when I run it from a terminal window, I do not get any output.
I am sure its a stupid beginners mistake...
#include <iostream>
using namespace std;
int main(){
std::cout << "Hello Moon!";
std::cout.flush();
return 0;
}
compiling:
Kompilierung wird gestartet...
D:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall "D:\CPLUSPLUS\programs\hello world\hello world.cpp" -o "D:\CPLUSPLUS\programs\hello world\hello world.exe"
Die Kompilierung wurde erfolgreich abgeschlossen.
console:
PS D:\CPLUSPLUS\programs\hello world> "hello world.exe"
hello world.exe
PS D:\CPLUSPLUS\programs\hello world>
so obviously it runs the exe without complaint, however I do not see any output...
Any hints/ideas?
Thanks quimby! that did the job!
actually ist not my c++ vscode ignorance but the one of powershell (coming from cmd...)
so you are right: powershell did NOT run my program but rather just echo the quoted string.
so ones needs the & operator to do the job.
Problem solved
Thanks again.
i guess you need to remove the std:: prefix because you already imported the std
try this
#include <iostream>
using namespace std;
int main(){
cout << "Hello Moon!";
return 0;
}

Compiling C++ with VSCode on the Mac

Total novice C++ user going thru tutorials using Visual Studio Code on OSX. Barest bones Hello World program
#include <iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}
Then I run-compile in VSCodes terminal using
$ g++ Foo.cpp -o foo
My question is, shouldn't I be seeing the Hello, World! out put in the terminal? Or will this only be visible if I compile and run in Windows?
I do see an executable after compiling but nothing in the VS Terminal window.
You actually have to run the file. So, in this case, since you named your file "foo" with the -o command, you have to run ./foo from the terminal.

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

std::endl crashes Windows 8, compiled using MinGW

I have 3 computers, two of which use Windows 8. Using the latest version of MinGW's g++ (4.8.1-4) my hello world program freezes whenever I compile and run on the Windows 8 computers but not in Windows 7.
#include <iostream>
int main()
{
std::cout << "Hello, World!" <<std::endl;
return 0;
}
This compiles just fine in g++ but running a.exe will display "Hello, World!" then a window will pop up and say "a.exe has stopped working, Windows can check online for a solution to the program...." etc.
Has anybody seen this problem.
Also, I tried "std::cout << "Hello, World!\n" << std::flush;" and this has the same problem. It seems that every function that flushes the buffer causes a crash.
Following Eric's advice, I recompiled the program and ran it in gdb and got the following output:
Program received signal SIGILL, Illegal instruction.
0x00405065 in _Jv_RegisterClasses ()
In the second instance, the '\n' should cause an output flush in any case, although in Windows I believe console output is immediate (or perhaps automatic after a short timeout) in any case without an explicit flush.
I suggest the following experiments:
1) See if it is specific to the C++ library by using the C library (in MinGW Microsoft's C runtime is used rather than glibc):
#include <stdio.h>
int main()
{
printf( "Hello, World!\n" ) ;
return 0;
}
2) Eliminate the exit code by:
int main()
{
return 0;
}
3) No newline at all:
#include <iostream>
int main()
{
std::cout << "Hello, World! ;
return 0;
}
4) Try different compiler options such as optimisation levels, or -fno-builtin for example, or as suggested here: -static-libgcc -static-libstdc++ (although I doubt ``-static-libgcc` will itself have any effect since MinGW uses Microsoft's C runtime DLL and the static library is only available with Microsoft's tools).
I had the same issue and found after a long painful search that I had multiple versions of the mingw provided libstdc++-6.dll on my computer. One was part of the mingw installation the others were part of other installation packages (gnuplot and GIMP). As I had gnuplot in my PATH the compiled mingw exe it would use an older, incompatible version of this dll and crash with the described symptoms. I can, therefore, confirm Dietmar Kühl's suspicion. As suggested above linking the library statically obviously helps in this case as the library functions are included in the exe at compile time.

Compiling simple Hello World program on OS X via command line

I've got a simple hello world example that I'm trying to compile on OS X, named hw.cpp:
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
I'd like to compile it using gcc, but I've had no success. I'd also like to hear the other options, like using Xcode ?
Try
g++ hw.cpp
./a.out
g++ is the C++ compiler frontend to GCC.
gcc is the C compiler frontend to GCC.
Yes, Xcode is definitely an option. It is a GUI IDE that is built on-top of GCC.
Though I prefer a slightly more verbose approach:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
g++ hw.cpp -o hw
./hw
user#host> g++ hw.cpp
user#host> ./a.out
Compiling it with gcc requires you to pass a number of command line options. Compile it with g++ instead.
Also, you can use an IDE like CLion (JetBrains) or a text editor like Atom, with the gpp-compiler plugin, works like a charm (F5 to compile & execute).
The new version of this should read like so:
xcrun g++ hw.cpp
./a.out
Use the following for multiple .cpp files
g++ *.cpp
./a.out
You didn't specify what the error you're seeing is.
Is the problem that gcc is giving you an error, or that you can't run gcc at all?
If it's the latter, the most likely explanation is that you didn't check "UNIX Development Support" when you installed the development tools, so the command-line executables aren't installed in your path. Re-install the development tools, and make sure to click "customize" and check that box.