为什么这个方法用Cygwin g ++打破std :: cout?

时间:2016-01-15 04:23:11

标签: c++ g++ cygwin

我试图在我的c ++程序中包含以下标题:

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

但是当我尝试使用Cygwin g ++编译并运行它时,我的简单程序运行并退出而不打印任何内容:

#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是一个匿名的typedef,我想知道可能会导致问题......?这到底是怎么回事?这是如何导致std :: cout不起作用的?它似乎适用于Linux上的g ++。

更新 您可以在上面的链接中看到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

1 个答案:

答案 0 :(得分:1)

我认为这不是代码问题。它使用4.9.3编译器在cygwin上构建和运行。但是,当我运行较新的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

使用4.9.3编译器在32位上:

$ 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