mercredi 16 avril 2014

Windows - C++: compilateur g ++: le choc des stdio avec des fonctions virtuelles ? -Débordement de pile


I am not able to use stdio combined with virtual functions (Windows Vista, Cygwin, GCC 4.8.2)


#include <stdio.h>

class A
{
public:

// If I make g() as virtual, stdio doesn't print.
virtual void g() {}
};

int main()
{
A a; // Or I could remove this object to make stdio print.
printf("testing\n");
}

With hint by "Borgleader", I found that it works when used with "-O2" option. Linking seems to be different when used with/without "-O2".


/home/user> g++ test.cc
/home/user> ./a.exe
/home/user> ldd ./a.exe
ntdll.dll => /cygdrive/c/Windows/system32/ntdll.dll (0x77540000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x75790000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)
cyggcc_s-1.dll => /usr/bin/cyggcc_s-1.dll (0x6bf40000)
cygstdc++-6.dll => /usr/bin/cygstdc++-6.dll (0x6c480000)

/home/user> g++ -O2 test.cc
/home/user> ./a.exe
testing
/home/user> ldd ./a.exe
ntdll.dll => /cygdrive/c/Windows/system32/ntdll.dll (0x77540000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x75790000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)

Thanks for selbie's suggestion to try GDB. Now things are a bit more clear. I should forget about printf, the program doesn't even load! With option "-O2", it works ok (even in GDB).


/home/user> g++ -g test.cc
/home/user> gdb ./a.exe
<<Edited for brevity>>
(gdb) break main
Breakpoint 1 at 0x40119e: file test.cc, line 14.
(gdb) run
Starting program: /home/user/a.exe
[New Thread 4888.0x13e0]
gdb: unknown target exception 0xc0000139 at 0x77549cfc
During startup program exited with code 0xc0000139.

Another hint that the program was not loading is that, even if I put sleep(100) in it, it exists immediately.




Summary: The problem got solved after updating the entire cygwin installation


Details: Reinstall of just g++ compiler did not help. Updating just g++ compiler to new version did not help. Updating the entire Development category did not help. Updating Base category did not help. Updating the entire cygwin installation helped. It remains unknown as per why the previous install was still working with "-O2" option. I could have continued to use "-O2" option. But, I chose to do complete update of cygwin.




Nothing in this code that should not work.


You can switch to VirtualBox and run gcc on Linux VM, this will be more robust than cygwin and other .exe stuff which is modelling Linux on Windows.


Seems that now you are testing cygwin implementation of compiler.



I am not able to use stdio combined with virtual functions (Windows Vista, Cygwin, GCC 4.8.2)


#include <stdio.h>

class A
{
public:

// If I make g() as virtual, stdio doesn't print.
virtual void g() {}
};

int main()
{
A a; // Or I could remove this object to make stdio print.
printf("testing\n");
}

With hint by "Borgleader", I found that it works when used with "-O2" option. Linking seems to be different when used with/without "-O2".


/home/user> g++ test.cc
/home/user> ./a.exe
/home/user> ldd ./a.exe
ntdll.dll => /cygdrive/c/Windows/system32/ntdll.dll (0x77540000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x75790000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)
cyggcc_s-1.dll => /usr/bin/cyggcc_s-1.dll (0x6bf40000)
cygstdc++-6.dll => /usr/bin/cygstdc++-6.dll (0x6c480000)

/home/user> g++ -O2 test.cc
/home/user> ./a.exe
testing
/home/user> ldd ./a.exe
ntdll.dll => /cygdrive/c/Windows/system32/ntdll.dll (0x77540000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x75790000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)

Thanks for selbie's suggestion to try GDB. Now things are a bit more clear. I should forget about printf, the program doesn't even load! With option "-O2", it works ok (even in GDB).


/home/user> g++ -g test.cc
/home/user> gdb ./a.exe
<<Edited for brevity>>
(gdb) break main
Breakpoint 1 at 0x40119e: file test.cc, line 14.
(gdb) run
Starting program: /home/user/a.exe
[New Thread 4888.0x13e0]
gdb: unknown target exception 0xc0000139 at 0x77549cfc
During startup program exited with code 0xc0000139.

Another hint that the program was not loading is that, even if I put sleep(100) in it, it exists immediately.




Summary: The problem got solved after updating the entire cygwin installation


Details: Reinstall of just g++ compiler did not help. Updating just g++ compiler to new version did not help. Updating the entire Development category did not help. Updating Base category did not help. Updating the entire cygwin installation helped. It remains unknown as per why the previous install was still working with "-O2" option. I could have continued to use "-O2" option. But, I chose to do complete update of cygwin.



Nothing in this code that should not work.


You can switch to VirtualBox and run gcc on Linux VM, this will be more robust than cygwin and other .exe stuff which is modelling Linux on Windows.


Seems that now you are testing cygwin implementation of compiler.


0 commentaires:

Enregistrer un commentaire