为什么此方法会破坏 std::cout 与 Cygwin g++?

Why does this method break std::cout with Cygwin g++?

我试图在我的 C++ 程序中包含以下 header:

https://raw.githubusercontent.com/syoyo/tinyobjloader/master/tiny_obj_loader.h

但是当我尝试使用 Cygwin g++ 和 运行 进行编译时,我的简单程序 运行s 并在不打印任何内容的情况下退出:

#include <iostream>

#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"


int main( int argc, const char* argv[] )
{

   std::cout << "hello world" << std::endl;
}

我没有遇到编译或 运行时间错误。当我注释掉 "tiny_obj_loader.h" 包含时,它会打印 "hello world"。此外,当我注释掉大部分 tiny_obj_loader.h 文件时,我可以将其缩小到以下导致问题的函数:

static void InitMaterial(material_t &material) {
  material.name = "";
  material.ambient_texname = "";
  material.diffuse_texname = "";
  material.specular_texname = "";
  material.specular_highlight_texname = "";
  material.bump_texname = "";
  material.displacement_texname = "";
  material.alpha_texname = "";
  for (int i = 0; i < 3; i++) {
    material.ambient[i] = 0.f;
    material.diffuse[i] = 0.f;
    material.specular[i] = 0.f;
    material.transmittance[i] = 0.f;
    material.emission[i] = 0.f;
  }
  material.illum = 0;
  material.dissolve = 1.f;
  material.shininess = 1.f;
  material.ior = 1.f;
  material.unknown_parameter.clear();
}

对该函数的以下修改会导致 "hello world" 正确打印:

static void InitMaterial(material_t &material) {
    //nothing
}
static int InitMaterial(material_t &material) {
    return 0;
}

但是,这会导致它不起作用:

static void InitMaterial(material_t &material) {
    material.name = "";
}

请注意 material_t 是一个匿名类型定义,我想知道这可能会导致问题...?这到底是怎么回事?这怎么会导致 std::cout 不起作用?它似乎可以在 linux.

上与 g++ 一起使用

更新: 你可以在上面的link中看到material_t的定义,但这里是为了方便:

namespace tinyobj {

typedef struct {
  std::string name;

  float ambient[3];
  float diffuse[3];
  float specular[3];
  float transmittance[3];
  float emission[3];
  float shininess;
  float ior;      // index of refraction
  float dissolve; // 1 == opaque; 0 == fully transparent
  // illumination model (see http://www.fileformat.info/format/material/)
  int illum;

  int dummy; // Supress padding warning.

  std::string ambient_texname;            // map_Ka
  std::string diffuse_texname;            // map_Kd
  std::string specular_texname;           // map_Ks
  std::string specular_highlight_texname; // map_Ns
  std::string bump_texname;               // map_bump, bump
  std::string displacement_texname;       // disp
  std::string alpha_texname;              // map_d
  std::map<std::string, std::string> unknown_parameter;
} material_t;

...//tons of other stuff

我认为这不是代码问题。它使用 4.9.3 编译器在 cygwin 上构建 运行s。但是当我 运行 较新的 5.2.0 编译器时,似乎链接的不同库可能会导致问题。当我编译静态时,问题消失了。不是真正的解决方案或问题的原因,但它可能会让你在此期间继续前进。

$ g++ -g test.cpp

$ ldd a.exe
        ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77ca0000)
        kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77b80000)
        KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x7fefdab0000)
        cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)
        cyggcc_s-seh-1.dll => /usr/bin/cyggcc_s-seh-1.dll (0x3ffba0000)
        cygstdc++-6.dll => /usr/bin/cygstdc++-6.dll (0x3ff0e0000)

$ ./a.exe

$ g++ -g --static test.cpp

$ ldd a.exe
        ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77ca0000)
        kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77b80000)
        KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x7fefdab0000)
        cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)

$ ./a.exe
hello world

在 32 位上使用 4.9.3 编译器:

$ g++ --version
g++ (GCC) 4.9.3
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


$ g++ test.cpp  
$ ldd a.exe
        ntdll.dll => /cygdrive/c/Windows/SysWOW64/ntdll.dll (0x77e80000)
        kernel32.dll => /cygdrive/c/Windows/syswow64/kernel32.dll (0x75930000)
        KERNELBASE.dll => /cygdrive/c/Windows/syswow64/KERNELBASE.dll (0x7674000                                                0)
        cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)
        cyggcc_s-1.dll => /usr/bin/cyggcc_s-1.dll (0x6f790000)
        cygstdc++-6.dll => /usr/bin/cygstdc++-6.dll (0x6e4b0000)

$ ./a.exe
hello world