【问题标题】:Why does this method break std::cout with Cygwin g++?为什么这个方法会用 Cygwin g++ 破坏 std::cout?
【发布时间】:2016-01-15 04:23:10
【问题描述】:

我正在尝试在我的 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

【问题讨论】:

  • material_t的定义是什么
  • @George Houpis 更新
  • 您使用的是什么版本的 g++?运行 g++ 4.9.3 x64 对我有用。
  • g++ 5.2.0 x86 位
  • 这很奇怪。我能够重现该问题。但是,我认为这是因为可执行文件链接到意外的 dll。尝试构建静态或调整 dll 路径。

标签: c++ g++ cygwin


【解决方案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

在 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

【讨论】:

  • 嘿,感谢您不厌其烦地重现此内容。我最终将我的编译器降级到 4.9.3 并且工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多