'CMake: Bundling a Console Application in MacOS c++

I am building a MacOS c++ application in CMake that is a terminal program with a few associated libraries. I would like bundle the executable because customers are familiar with that. However, then opening the bundled .app the terminal will not open and the program disappears immediately even if I request input.

For example, the application main.cpp

#include <iostream>

int main()
{
    int x;
    std::cout << "Hello World!\n";
    std::cin >> x;
    std::cout << "You said " << x << "\n";
    return 0;
}

can be made up with CMakeLists.txt file:

cmake_minimum_required(VERSION 3.4)
project(hello_world)
SET(CURRENT_TARGET hello)
add_executable(${CURRENT_TARGET} MACOSX_BUNDLE main.cpp)

But if I double-click the hello.app file it creates, it closes immediately. Is there a way to fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source