Search code examples
c++g++cygwin

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


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

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

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;
}

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();
}

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 = "";
}

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: 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

Solution

  • 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
    

    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