【问题标题】:Exe not working properly outside of visual studio?Exe 在 Visual Studio 之外无法正常工作?
【发布时间】:2022-01-20 19:47:00
【问题描述】:

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

当我进行构建并想从资源管理器启动 .exe 时,它会启动并运行,但是......好吧,看看:

应该是这样的:

这就是它在 VS 之外的样子:

到目前为止,我已尝试将运行时库设置为多线程 (/MT),但没有成功。

否则,我似乎真的找不到太多东西。似乎独立的.exe 缺少一些依赖项,但我不知道我需要做什么。据我了解,我包含在标头中的所有内容也应该编译“到”.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 << "\033[1;7;32m" << std::setw(lengthy) << std::left << y << "\033[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 << "\033[1;7;36m" << std::setw(10) << std::left << elapsed.count() << "\033[0m ";
        else
        {
            std::cout << "\033[1;36m" << std::setw(10) << std::left << elapsed.count() << "\033[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;
}

【问题讨论】:

  • cmd 提示符不接受您的颜色控制字符
  • @386 我的意思是boost 和isPrime 以及其他所有内容。与您的 std::cout 的单行具有相同的问题。如上所述 - “控制台”问题
  • @386 回复:do you see the numbers 1..100 counting up? They are not there in the second picture - 但他们是!我可以看到1duration2 等
  • 嗯..这很尴尬。我会将您的评论标记为已回答。对不起,谢谢你的帮助。我想我会好好睡一觉的。

标签: c++ visual-studio-2022


【解决方案1】:

见https://en.wikipedia.org/wiki/ANSI_escape_code,具体来说:

2016 年,微软发布了 Windows 10 版本 1511 更新,该更新在 Windows NT 首次亮相的二十多年后意外地实现了对 ANSI 转义序列的支持。[13]这是与适用于 Linux 的 Windows 子系统一起完成的,允许基于终端的类 Unix 软件使用 Windows 控制台中的序列。不幸的是,这默认为关闭,但 Windows PowerShell 5.1 启用了它。 PowerShell 6 可以使用 `e.[14] 将必要的 ESC 字符嵌入到字符串中2019 年推出的 Windows 终端默认支持序列,微软打算将 Windows 控制台替换为 Windows 终端。[15]

颜色代码在 Visual Studio Code(终端)中工作。

附加信息:

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

【讨论】:

  • 那么,OP没有解决方案吗?
  • 好像有。您可以启用它,但需要 windows.h。我不想包括的东西。但颜色代码不是主要问题。
【解决方案2】:

我已经能够使用以下代码在默认 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 函数开始时调用一次。

【讨论】:

  • 谢谢。我已经实现了一个稍长的版本,可以捕获可能的异常和错误等。虽然我还没有真正理解代码,但它可以工作。也许你可以详细说明你的#ifdef _WIN32 是做什么的?
  • @386 确保这段代码只在Windows上编译,因为它不会在其他操作系统上编译。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2022-07-04
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 2011-05-28
  • 2018-01-20
相关资源
最近更新 更多