为什么这个方法打破std :: cout与Cygwin g ++? [英] Why does this method break std::cout with Cygwin g++?

查看:281
本文介绍了为什么这个方法打破std :: cout与Cygwin g ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to include the following header in my c++ program:

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

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

but when I attempt to compile with Cygwin g++ and run it, my simple program runs and exits without printing anything:

#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文件的大部分时,我可以缩小到以下函数导致的问题:

I get no compile or runtime errors. When I comment out the "tiny_obj_loader.h" include, it prints "hello world". Also, when I comment out most of the tiny_obj_loader.h file, I can narrow it down to the following function causing the problem:

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

The following modifications to this function cause "hello world" to be printed correctly:

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

但是,这会导致无法工作:

However, this causes it to not work:

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

请注意,material_t是一个匿名typedef,我想知道可能会导致问题...?这里究竟是怎么回事?这如何导致std :: cout不工作?它似乎在linux上使用g ++。

Note that material_t is an anonymous typedef, which I am wondering might be causing problems...? What on earth is going on here? How is this causing std::cout to not work? It appears to work with g++ on linux.

UPDATE:
你可以看到 material_t ,但这里是为了方便:

UPDATE: You can see the definition of material_t in the link above but here it is for conveniency:

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上。但是当我运行较新的5.2.0编译器,似乎不同的库被链接可能会导致一个问题。当我编译静态,问题消失了。

I do not think it is a code issue. It builds and runs on cygwin with the 4.9.3 compiler. But when I run the newer 5.2.0 compiler, it seems the different libraries being linked may be causing an issue. When I compiled static, the problem disappeared. Not a real solution or cause of the problem, but it may let you move forward in the meantime.

$ 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编译器上:

On 32-bit with the 4.9.3 compiler:

$ 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

这篇关于为什么这个方法打破std :: cout与Cygwin g ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆