小编Joh*_*don的帖子

gcc:剥离未使用的函数

我注意到有时即使我不使用iostream和相关的I/O库,我的Mingw生成的二进制文件仍然不合理.

例如,我编写了一个代码来使用vector并且cstdio只编译它-O2 -flto,我的程序可以大到2MB!我跑了nm main.exe > e.txt,震惊地看到iostream它里面的所有相关功能.

经过一些谷歌搜索,我学会了使用-ffunction-sections -Wl,-gc-sections,将程序大小从2MB减少到~300KB(如果有-s,100 + KB).优秀!

为了进一步测试效果-ffunction-sections -Wl,-gc-sections,这里是另一个代码:

#include <cstdio>
#include <vector>
#include <tuple>
#include <algorithm>
#include <chrono>
#include <windows.h>

#undef min

struct Point {
    int x, y;
};

constexpr int length = 5;
constexpr int half_length() {
    return length & 1 ? length : length - 1;
}

template<class F>
int func_template(F&& f) {
#ifdef _MSC_VER
    puts(__FUNCSIG__); …
Run Code Online (Sandbox Code Playgroud)

c++ gcc iostream visual-studio visual-c++

18
推荐指数
1
解决办法
2239
查看次数

编译 GCC 7:错误:C++ 预处理器“/lib/cpp”未通过健全性检查

尝试从源代码构建 GCC 7.1.0。这不是我第一次使用,它曾经在过去的 GCC 版本中工作过。

../configure --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 \
 --prefix=/c/mingw64 --with-sysroot=/c/mingw64 \
 --with-gmp=/c/mingw64/gmp --with-mpfr=/c/mingw64/mpfr --with-mpc=/c/mingw64/mpc \
 --with-isl=/c/mingw64/isl \
 --disable-nls --disable-multilib --disable-libstdcxx-pch --disable-shared \
 --disable-win32-registry --disable-libstdcxx-debug --disable-libstdcxx-verbose \
 --with-tune=haswell --enable-lto --enable-checking=release --enable-languages=c,c++ \
 --enable-libstdcxx-time --enable-threads=win32 --enable-libatomic --enable-fully-dynamic-string
Run Code Online (Sandbox Code Playgroud)

错误

checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/c/mingw64/src/build/gcc':
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details.
make[2]: *** [Makefile:4309: configure-stage2-gcc] Error 1
make[2]: Leaving directory '/c/mingw64/src/build'
make[1]: *** [Makefile:20550: stage2-bubble] Error 2
make[1]: …
Run Code Online (Sandbox Code Playgroud)

windows gcc mingw-w64

6
推荐指数
1
解决办法
1万
查看次数

在不带 UWP 的情况下从 C++/WinRT 使用 FileOpenPicker 时出现“无效窗口句柄”错误

我正在尝试使用C++/WinRT编写一些有趣的东西。我的 Windows 编程经验很少,也没有 C++/CX 经验,因此我首先尝试示例程序 (OCR)

示例程序是关于光学字符识别的,我将其修改为面部检测器(基于控制台)。效果非常好。

我想将获取文件从命令行转换为文件对话框,因此我从 C#移植了以下代码片段:

  FileOpenPicker picker = FileOpenPicker();
  picker.ViewMode(PickerViewMode::Thumbnail);
  picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);
  picker.FileTypeFilter().Append(L".jpg");
  picker.FileTypeFilter().Append(L".jpeg");
  picker.FileTypeFilter().Append(L".png");

  StorageFile file = co_await picker.PickSingleFileAsync();
Run Code Online (Sandbox Code Playgroud)

没有编译错误,但是当我运行程序时,我收到以下错误消息:

hresult_error: (0x80070578) Invalid window handle.
Run Code Online (Sandbox Code Playgroud)

我认为发生该错误是因为它是基于控制台的程序(wmain),而不是wWinMain。

我尝试在网上寻找解决方案,但都是关于UWP的。请提供一个不涉及UWP并且必须能够cl.exe直接编译的解决方案。

笔记

根据可从经典桌面应用程序调用的 UWP API,如果 API 具有DualApiPartitionAttribute属性,则它将在经典桌面应用程序中工作,否则不会。例如,Geolocator具有此属性,因此它可以工作。

然而,即使FaceDetector没有这个属性它仍然可以在我的玩具程序中得到证明。

任何东西都Windows::UI肯定需要 UWP(尽管有https://aka.ms/windowsui/inwin32)。FileOpenPicker 没有此属性,但它不在 下Windows::UI,因此可能有解决方法。

完整代码:

#pragma comment(lib, "windowsapp")

#include <winrt/Windows.Storage.Pickers.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Media.FaceAnalysis.h>

using …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++ windows-runtime visual-studio-2017

6
推荐指数
1
解决办法
1772
查看次数

windows环境路径重复

由于某种我不知道的原因,我的echo %path%有很多重复的C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\. 据我所知,长%path%是不好的,因为它会减慢搜索过程。删除这些重复项是否安全?

我还注意到路径变量有两种版本:一种用于用户变量,一种用于系统变量。如果我echo %path%以普通用户身份输入命令提示符,它将显示这两个版本的串联(系统版本在前)。如果我要删除重复项,我应该从哪个版本中删除?

(粗体为系统版本)

C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C: \Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\ x64;C:\Program Files\Intel\Intel(R) 管理引擎组件\DAL;C:\Program Files\Intel\Intel(R) 管理引擎组件\IPT;C:\Program Files (x86)\Intel\Intel (R) 管理引擎组件\DAL;C:\Program Files (x86)\Intel\Intel(R) 管理引擎组件\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS \system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared; C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32; C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86) \Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL; C:\Program Files\Intel\Intel(R) 管理引擎组件\IPT;C:\Program Files (x86)\Intel\Intel(R) 管理引擎组件\DAL;C:\Program Files (x86)\Intel\英特尔(R) 管理引擎组件\IPT;C:\MinGW\bin;C:\Python27;C:\Users\jie\AppData\Local\Microsoft\WindowsApps

windows command-prompt

5
推荐指数
1
解决办法
1万
查看次数

Visual Studio 2017 中的“onecore”文件夹

在Visual Studio 2017 RTM VC\Tools\MSVC\14.10.25017\lib文件夹中,有一个名为“onecore”的文件夹,里面有x64、x86和arm的文件夹,每个文件夹都包含一些VC\Tools中已经存在的.lib文件\MSVC\14.10.25017\lib{x64,x86,arm}。这些副本的作用是什么?

另外,“onecore”到底是什么意思?

visual-studio visual-c++ visual-studio-2017

5
推荐指数
1
解决办法
2379
查看次数

子集相关的位操作

我想知道这个位操作意味着什么:

for (int m = 1; m < (1 << n); ++m) {
    for (int s = m; s; s = (s - 1) & m) {
        // ....
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个for循环是枚举n个元素的所有子集,但是第二个for循环意味着什么?

c++ bit-manipulation

5
推荐指数
1
解决办法
285
查看次数

如何修复 Windows 版 Git 不断要求提供 Github 凭据

我总是通过 HTTPS 克隆私有 Github 存储库。当 git 第一次询问时,我有个人访问令牌用作“密码”。

在很长一段时间里,当我在命令行中运行东西时,我能够执行 git 操作,而无需 git 询问每个命令的凭据。

但现在它总是要求在命令行中提供凭据!

我尝试了各种组合来更改git configcredentials.helper(更改为store/wincred/manager/cache)和一堆其他东西,但没有任何效果。

有趣的是,如果我在 VSCode 终端内运行 git 命令,那么它不会要求提供凭据并且可以正常工作。但我不想每次需要执行一些 git 操作时都必须启动 VSCode(特别是当我的计算机由于其他任务而导致 RAM 不足时)

我需要一种永久的方法来解决此问题,同时仍然使用 HTTPS 远程 url + 个人访问令牌

我正在使用适用于 Windows 的 Git git 版本 2.40.0.windows.1

.gitconfig在尝试了很多在线指南后,我现在的情况很糟糕

[credential "github.com"]
    authority = Github
    interactive = never
    preserve = true
[credential]
    helper = manager
    modalPrompt = true
    writelog = true
Run Code Online (Sandbox Code Playgroud)

windows git

5
推荐指数
1
解决办法
2081
查看次数

从函数返回stl向量的有效方法

这是从Efficient方式返回c ++中的std :: vector的扩展问题

#include <cstdio>
#include <vector>
#include <chrono>

std::vector<int> func1() {
    std::vector<int> v;
    v.reserve(1e6);
    for (int i = 0; i < 1e6; i++) v.emplace_back(i);
    return v;
}

std::vector<int> func2() {
    std::vector<int> v;
    v.reserve(1e6);
    for (int i = 0; i < 1e6; i++) v.emplace_back(i);
    return std::move(v);
}

int main() {
    auto start1 = std::chrono::steady_clock::now();
    std::vector<int> v1 = func1();
    auto end1 = std::chrono::steady_clock::now();
    printf("%d\n", std::chrono::duration_cast<std::chrono::nanoseconds>(end1-start1).count());

    auto start2 = std::chrono::steady_clock::now();
    std::vector<int> v2 = func2();
    auto end2 = std::chrono::steady_clock::now();
    printf("%d\n", …
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11

4
推荐指数
1
解决办法
502
查看次数

CMake用于检测Ninja的Visual C++编译器工具的环境变量

我有PATHVisual Studio 2017 RTM中的Mingw64 GCC 6.3.0(总是在)和Visual C++编译工具(不在PATH).

如果我运行cmake . -G "MinGW Makefiles",将选择GCC 6.3.0.

如果我运行cmake . -G "Ninja",将选择GCC 6.3.0.

我的Visual C++编译工具不是标准的,我只保留我需要的部分并删除其余部分(如MSBuild,IDE等).我使用自己的批处理脚本来设置PATH,INCLUDE并且LIB(工作得很好).

如果我运行此批处理脚本并运行cmake .,将选择MSVC并使用NMake构建.

在相同的环境下,cmake . -G "Ninja"选择GCC 6.3.0而不是MSVC.

所以我的问题是,如何告诉CMake我想要使用MSVC + Ninja而不是GCC + Ninja PATH?我应该设置的任何环境变量?

cmake ninja

3
推荐指数
1
解决办法
2060
查看次数