No output from C++ program
Asked Answered
S

2

3

First of all, sorry if this is in the wrong category, as I am not sure what the cause of this problem is.

For educational purposes I have created a small "Hello World" application

#include <iostream>
int main() {
    std::cout << "Hello World\n";
    return 0;
}

I have tried compiling it with both Visual Studio as well as MINGW-64(g++ -m64 main.cpp), as a 64-bit application. It runs perfectly on my Windows computer, but when I try to run it within the latest Windows PE it doesn't print out any thing. I have also tried with std::cin so that the program doesn't stop right away, but the same thing happens - no output and no error. enter image description here

I know WinPE is very limited in terms of included libraries and subsystems, but I really thought that this simple Hello World app would run. The WinPE environment is 64-bit, that 's why I am compiling as 64-bit

Any ideas where I should start?

Splashboard answered 10/11, 2019 at 13:48 Comment(5)
maybe this helps you LINK. I dont know if this has changed in any way, but u may need a 32bit exeRecrystallize
Thank you for the swift reply, but I think I should be able to run it as a 64-bit application. The target WinPE environment is 64-bit, which is why I am compiling it as a 64-bit application.Splashboard
COUT is a buffered stream. It looks like the buffer isn’t flushed before the program exits. You can flush the buffer and it should appear.Stripe
Thanks for the reply, Freddy. Adding "<< std::flush" to the end of the line didn't help, if that's what you meant?Splashboard
Found the issue, see my comment to Lucas' answer.Splashboard
S
7

I found the actual problem. I didn't compile the application statically which caused it to rely on dependencies not found in WinPE. I re-compiled it using the '-static' flag and it now works as expected on both WinPE and desktop versions of Windows.

Splashboard answered 10/11, 2019 at 14:58 Comment(3)
You can accept your own answer. There's a badge for everything.Disembark
Yep, I just have to wait two days until I can do so. Or at least that what it says when I try.Splashboard
Thanks. You saved my day. Nothing else worked, tried everything. std::endl; then std::flush, and also std::cout.flush(); Only adding "-static"worked. Deserve more votes.Superjacent
M
1

Use

    std::cout << "Hello World" << std::endl;

std::endl will flush the content and add a \n at the end of your message.

Macromolecule answered 10/11, 2019 at 14:52 Comment(1)
Thanks for the reply. I found the actual problem. I didn't compile the application statically which caused it to rely on dependencies not found in WinPE. I re-compiled it using the '-static' flag and it now works as expected on both WinPE and desktop versions of Windows.Splashboard

© 2022 - 2024 — McMap. All rights reserved.