I just learned C++, and I’m trying to code a simple Hello World program on my 64-bit Windows 11 machine. I write the code with Visual Studio Code, but compile it with Command Prompt. When I try to compile it, no error pops up, but when I try to run it, nothing is printed.
The program (main.cpp):
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
This is the output.The output is always stored right next to the main.cpp
file, and is always named a.exe
(without specifying).
Microsoft Windows [Version 10.0.22621.2134]
(c) Microsoft Corporation. All rights reserved.
C:UsersmeDesktopHello World>g++ main.cpp
C:UsersmeDesktopHello World>a.exe
C:UsersmeDesktopHello World>a
C:UsersmeDesktopHello World>
What other things I tried:
-
Opening the a.exe file in my folder, which closes immediately. I also tried adding
system("pause");
to the main method, but to no avail. -
Adding a custom name for the output, such as
g++ main.cpp -o hello
, with or without the .exe suffix. -
Renaming main.cpp to something else, such as hello.cpp.
G++, GCC and GDB are installed on my machine as required.
C:Usersme>g++ --version
g++ (Rev2, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 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.
C:Usersme>gcc --version
gcc (Rev2, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 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.
C:Usersme>gdb --version
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Help would be greatly appreciated.
2
Answers
Thanks Acane: the problem was caused by Inkscape. After I have uninstalled Inkscape, things start to work now. Thank you!
i know you have solved the problem, but i encourage you to use input/output from file, it will make things easier than using terminal.
If you accept my suggestion, you can search about how to take input/output using file in cpp or look at this answer