Issue
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!
Solution
Edit:
It seems that the problem had to do with static linking of necessary libraries. Thus the solution I found was to include one of the static flags for g++
such as -static
, -static-libstdc++
, etc. To tell CMake to do this automatically upon compilation I just added this as a flag:
set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++")
That being said I still have so many questions regarding the need to actually do this and if this is even the preferred solution or use-case of static linking.
Answered By - Jack Henrikson Answer Checked By - Katrina (WPSolving Volunteer)