Exe 在 visual studio 之外不能正常工作?

Exe not working properly outside of visual studio?

我的程序在 IDE(Visual Studio 2022)、调试和发布模式下运行良好。

当我构建并想从 Explorer 启动 .exe 时,它会启动并运行,但是......好吧,看看:

应该是这样的:

这是在 VS 之外的样子:

到目前为止,我已经尝试将运行时库设置为 Multi-threaded (/MT),但没有成功。

不然我好像真的没找到多少。似乎独立 .exe 缺少一些依赖项,但我不知道我需要做什么。根据我的理解,我在 header 中包含的所有内容也应该被编译“到”.exe 中。

int128_t好像不行。 ANSI 颜色代码也没有。 不过,计时器正在工作。

代码:

#include <iostream>
#include <chrono>
#include <boost/multiprecision/cpp_int.hpp>

namespace mp = boost::multiprecision;

bool isPrime(mp::int128_t n);
mp::int128_t n{ 0 }, y{ 0 };

int main()
{
    std::cin >> y;
    std::cin >> n;
    std::cout << "\n";
    
    for (y; y <= n; y++)
    {
        int lengthy = to_string(y).length();

        const auto start = std::chrono::steady_clock::now();

        if (isPrime(y) == true)
            std::cout << "3[1;7;32m" << std::setw(lengthy) << std::left << y << "3[0m ";
        else
        {
            std::cout << std::setw(lengthy) << std::left << y << " ";
        }

        const auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> elapsed = end - start;

        if (elapsed.count() >= 0.1)
            std::cout << "3[1;7;36m" << std::setw(10) << std::left << elapsed.count() << "3[0m ";
        else
        {
            std::cout << "3[1;36m" << std::setw(10) << std::left << elapsed.count() << "3[0m ";
        }
    } 

    std::cout << "\n";
    std::cin >> y;
}

bool isPrime(mp::int128_t n)
{
    if (n == 2 || n == 3)
        return true;
    
    if (n <= 1 or n % 2 == 0 or n % 3 == 0)
        return false;
    
    for (uint64_t i = 5; i * i <= n; i += 6)
    {
        if (n % i == 0 or n % (i + 2) == 0)
            return false;
    }

    return true;
}

https://en.wikipedia.org/wiki/ANSI_escape_code,具体为:

In 2016, Microsoft released the Windows 10 version 1511 update which unexpectedly implemented support for ANSI escape sequences, over two decades after the debut of Windows NT.[13] This was done alongside Windows Subsystem for Linux, allowing Unix-like terminal-based software to use the sequences in Windows Console. Unfortunately this defaults to off, but Windows PowerShell 5.1 enabled it. PowerShell 6 made it possible to embed the necessary ESC character into a string with `e.[14] Windows Terminal, introduced in 2019, supports the sequences by default, and Microsoft intends to replace the Windows Console with Windows Terminal.[15]

颜色代码适用于 Visual Studio 代码(终端)。

附加信息:

https://devblogs.microsoft.com/commandline/new-experimental-console-features/

我已经能够使用以下代码在默认 Windows 终端中启用颜色代码:

#ifdef _WIN32

#include <Windows.h>

void enableColors()
{
    DWORD consoleMode;
    HANDLE outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleMode(outputHandle, &consoleMode))
    {
        SetConsoleMode(outputHandle, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
    }
}

#endif

main 函数开始时调用一次。