'Vector push_back() fails when compiling with CMake and MinGW Makefiles
I've been searching all over the internet for a solution to this problem, however there is no good information about the origin of this problem. In essence, the program fails to run whenever I execute any vector-related function such as resize() or initializing the vector with a given size.
This is the current project structure of this example:
example_project
│ CMakeLists.txt
|
└───src
│ | main.cpp
│
└───build
│ Example.exe
│ ...
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20.0)
project(Example)
# Retrieve all source files
file(GLOB_RECURSE SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/main.cpp)
add_executable(Example ${SOURCE_FILES})
main.cpp:
#include <iostream>
#include <vector>
int main() {
std::cout << "Test\n";
// Example code
std::vector<int> exampleVector;
exampleVector.push_back(1);
// Same applies to any other vector operation
// Ex: std::vector<int> exampleVector(10);
return 0;
}
This code runs fine when compiling with CMake for Visual Studio via cmake .. from the build directory via the terminal. However, I want to build this project without any IDE, thus I want to directly compile it with CMake while still being able to use another text editor. Thus I assumed "MinGW Makefiles" to be a good candidate.
Building and running the application:
> cmake -G "MinGW Makefiles" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
> make
The above commands execute without any errors. Then running Example.exe yields:

I am rather inexperienced when it comes to CMake so I would appreciate any feedback on a better solution for building the project. But the bottom-line is that I want to use CMake for managing large projects without having to use any IDE or solution-file systems such as MSVC, just the terminal for building and running the application. The vector standard header seems to be the problem. Thank you in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
