Hi everyone,
I write this blog because I encountered a rather weird issue, maybe anyone here has the same issue or even has solved this?
I was participating in a contest earlier when I tried to compile & run for problem B and my code doesn't run >︿<
My case:
This code below compiles and run, I can see Hello
printed in my terminal
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
cout << "Hello";
// v.push_back(2);
return 0;
}
However when I uncomment v.push_back(2)
it compiles but doesn't run! (I don't see Hello
in terminal)
Commands I used: g++ main.cpp -Wall -o main
.\main.exe
There's no warning or error message.
I have tried compiling with MSYS64 (my main) and MSVC x64, same results.
Yesterday it was working fine, so maybe something broke my c++ runtime today.
Here's a list of what I did today before running to this problem:
- installed Anaconda,
- installed git,
- installed Visual Studio and C/C++ build toolchain (for rust),
- installed rust
Similar problem: https://stackoverflow.com/questions/70809283/c-program-does-not-run-if-a-vector-has-any-contents https://stackoverflow.com/questions/70994977/c-code-wont-run-if-a-vector-contains-value
I'd love to participate in that post's comment section however I don't have enough karma.
Someone mentioned this, tho I haven't tried.
Then I tried Visual Studio IDE and it works! Sadly VS complains when I do int arr[n];
saying array size should be static.
I'm considering IDEs since text editor has this issue (I implied IDE is OK because Visual Studio is OK), maybe I'll go with DevC++ or farmanager, or even code in Rust.
I'll share some update if this blog doesn't get >10 downvotes
It's (perhaps surprisingly, since it is VS) being very reasonable. VLAs are not legal C++, and are GNU extensions. They also tend to break other language features at times in unexpected ways, so you should avoid them.
As far as getting C++ to work on Windows is concerned, the WSL solution is probably the best one out there. Back when I used Windows, I used either DevC++ or custom invocations with probability $$$\frac{1}{2}$$$, and soon after making the switch to Linux realized how much better it was.
Edit: While I'm at it, maybe I should point out how messed up Windows libraries/runtimes really are. A ton of features don't work on Codeforces just because it uses MSVCRT (for instance, language features such as
std::aligned_alloc
don't work because apparently Windows deals with this stuff in a wacky way).I see, I will try the WSL solution. Kinda curious what do you use in Linux tho?
I'd love to try linux but currently I don't have the luxury
Not sure what you mean by "what do you use in Linux" but as far as IDEs/text editors are concerned, I use vim and a Makefile for build purposes (editors like VSCode and Sublime with their build systems are a popular choice too). If you're asking me which libraries and stuff I use, I use the bundled glibc, libstdc++ etc. (pretty sure Codeforces uses libstdc++ too).
At least in my opinion, Linux is much better for doing most stuff than Windows, so it doesn't hurt to try I guess. Lots of people I know took the WSL way out (of course, since it's literally the Windows Subsystem for Linux) and switched to Linux later on, so that's a viable option too.
Sorry I'm kinda bad at communicating but you answered my question.
I just installed WSL Ubuntu and it even compiles little bit faster. I think this will do for now. Maybe I'll switch to Linux some time.
Thank you for your reply!
So there's a quicker solution than running in WSL, simply add
-static-libstdc++
when compiling.Explanations can be found here stackoverflow.com/a/6405064/6871623
It seemed my Visual Studio 2022 installation indeed messed things up.
Now everything's back to normal!