Home > Software design >  CMake output configured with vscode not running in terminal
CMake output configured with vscode not running in terminal

Time:11-03

Okay so I am new to using CMake and I was trying to get it to work in vscode. I am using the extension CMake Tools to run the build and configuration. I'm running a basic hello world program that writes an output as well to test everything out and what happens is when the executable produced gets run from the terminal it does not produce any output.

What I am expecting to happen is when I do the configuration and building with the extension it produces an output file that when run from the terminal says hello world and writes a example file. What actually happens is it does not output anything at all when run from the terminal but when run through the extension it does give an output of text in the terminal the extension opens up and it produce a file.

What I've tried so far is compiling the program from g and it works as expected running from the terminal, I've created the cmake project and built it manually from the terminal and it works as expected running from the terminal, and I have finally created the cmake project manually from the terminal and built it inside of vscode using the build task and it works as expected running from the terminal. The only time it seems to not work as I would expect is when the vscode extension configures the project automatically. In all of the cmake projects it was built in release mode.

One thing I've notice about the executable that get outputted is the ones that function when called by the regular terminal is they are a larger file size then the ones that do not output so I assume that some setting in the automatic configuration is causing this which is probably the problem just I'm not sure what.

The code for the cpp program is

#include <iostream>
#include <fstream>
int main(int argc, char const *argv[])
{
    std::ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();
    std::cout<<"hello world"<<'\n';
    return 0;
}

The cmakelist.txt is this

cmake_minimum_required(VERSION 3.0.0)
project(abc123 VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(abc123 main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

I am also using MinGW for the gcc compiler and cmake So in summary is there a way to get the auto configuration of the extension to produce a output file that can be run from anywhere on my system not just through vscode extension

Thanks

Edit: I tried the same thing on linux and the cmake extension works as expected it seems like this is only a problem on windows

CodePudding user response:

Okay so switching from MinGW to Mingw-w64 with MSYS2 everything works as expected to work. Not sure what caused this problem in MinGW though.

  • Related