3

I compiled my program and run it but nothing happened when I tried to narrow the problem down, it turned out anytime I defined a vector nothing would be sent in the output. In this program, nothing is printed, even the std:cout at the start, and I don't understand how to fix this problem. A simple "hello world" works though. Also, I'm on Windows 10 and use VSCode.

    #include <iostream>
    #include <vector>


    int main( void )
    {
        std::cout << "hello\n";
        std::vector<int> g1; 
    
        for (int i = 1; i <= 5; i++) 
            g1.push_back(i); 
    
        std::cout << "Output of begin and end: "; 
        for (auto i = g1.begin(); i != g1.end(); ++i) 
            std::cout << *i << " "; 
    
        std::cout << "\nOutput of cbegin and cend: "; 
        for (auto i = g1.cbegin(); i != g1.cend(); ++i) 
            std::cout << *i << " "; 
    
        std::cout << "\nOutput of rbegin and rend: "; 
        for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) 
            std::cout << *ir << " "; 
    
        std::cout << "\nOutput of crbegin and crend : "; 
        for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir) 
            std::cout << *ir << " "; 
    
        return 0; 
    } 

In terminal (Windows Powershell) I write this command

g++ test.cpp -o test
.\test

Thank you for answering

Valerii Boldakov
  • 1,751
  • 9
  • 19

2 Answers2

2

With the same idea as what was advised by Harry. Try to flush systematically after a cout (not doing it may not guaranty the print when you expect it). using << std::endl will end the line and flush the ostream (recomanded after each cout). And if you don't want to end the line use simply << std::flush

Sambalika
  • 100
  • 7
0

Found answer: turns out you need to put a dll file libstdc++-6.dll into the .exe dir and solved my problem

  • 3
    That's not a solution, that's a workaround. Take a look at this answer for proper solution: https://stackoverflow.com/a/6405064/6871623 – Everyone Nov 15 '20 at 14:04
  • @Everyone Static linking is not **the** proper solution. It is **a** solution. It may or may not work for you depending on what exactly you need. – n. m. could be an AI Nov 15 '20 at 14:11
  • @n.'pronouns'm. What's the proper solution? – Sambalika Nov 15 '20 at 14:12
  • @n.'pronouns'm. you are correct, not even my method is a proper solution. A proper solution should let the program dynamically link to the standard library and be able to find it. Unfortunately, the author gave us no information as which compiler he's using. This can all be fixed by adding necessary info to environment variables. Without knowing what compiler he's using we can't help much. – Everyone Nov 15 '20 at 14:23
  • @Everyone: "I write this command `g++ test.cpp -o test`" looks like information about the compiler to me. Perhaps not *complete* information, but quite a lot of it. – Ben Voigt Nov 16 '20 at 00:10
  • Library files need to be either in the same directory as the .exe, as you did, or, better, in a directory that is referenced in the `PATH` environment variable (for linux, it would be the `LD_LIBRARY_PATH` environment variable). Do you need help editing those? (I wonder why the installer for your compiler did not do that, though.) – Aziuth Nov 16 '20 at 00:11
  • @BenVoigt that's not any information about the compiler. Different implementations can use the same `g++` name. On Windows, this can be MinGW or Cygwin... – Everyone Nov 16 '20 at 12:58
  • @everyone: But we know that it isn't cl.exe (Microsoft), or clang, or icc (Intel). Or some obscure compiler, of which there are plenty. Knowing it is GCC (the compiler collection, not the C compiler) is a substantial amount of information. To add to your mingw and cygwin possibilities, it could also be Linux GCC running in a WSL distro. – Ben Voigt Nov 16 '20 at 15:54