c++visual-studio-codevectorg++

Nothing execute when using Vector in C++ with VSCode


The problem

I have a problem with Vector in C++.
When I try to do basic things with them, my program "doesn't works" anymore.

What I tried

Searching on Stack Overflow but didn't find something relevant.
But I don't know a lot on this topic so I'm kind of stuck with it.

Some code:

Example:

#include <iostream>
#include <vector>

int main(int argc, char ** argv){
    std::cout << "Hello world\n";
    std::vector< int > arr;
}

This program will outputs "Hello world" because I don't interact with the vector.
But if I do:

#include <iostream>
#include <vector>

int main(int argc, char ** argv){
    std::cout << "Hello world\n";
    std::vector< int > arr;
    arr.push_back(1);

}

for example, there is no STDOUT. Hello world is never "printed". And there are no errors. I'm on Visual Studio code and I compile my program with g++ -o progam -Wall main.cpp
When I run this on the "Terminal" of Visual Studio Code it doesn't works. But when I rut it on another shell it works.


Solution

  • The command g++ -o -Wall main.cpp will create an executable file called -Wall. Unless that's the program you're trying to run, it's not going to work.

    Instead you would need something like g++ -o program -Wall main.cpp and then run program. Both of your examples do the right thing in that case.