简体   繁体   中英

gcc 4.4 vs. Visual C++ 2008

I am somewhat confused, need some C/C++ guru here. If it is C and in principle the language cannot change then how come some programs run on Visual C++ and not on gcc???

I am using Visual C++ 2008 and gcc 4.4.1 and trying to compile some really old code (1996). It somewhat works with Visual C++ (Windows XP) and completely fails with gcc (Ubuntu 9.10).

What alarms me is that if it is C/C++, then the compiler should not be an issue. Any help?

UPDATE : This is the code, http://www.ece.unh.edu/robots/cmacdemo.c

The most obvious problem is:

#include <sys\types.h>
#include <sys\stat.h>

The \ is allowed by Visual Studio, as the Windows dir separator, but not by gcc on Linux/Unix, where the directory separator is / .

/ should work everywhere though, so I suggest changing those over.

Then you should add the compile errors so we can see what/if problems remain.

The language specification cannot change, but how closely a compiler follows those specifications can .

There is a number of factors:

  • C and C++ , as defined by their respective standards, are evolving;
  • the level of standard adherence varies from compiler to compiler;
  • both gcc and Visual C++ have non-standard extensions, which -- if used -- would cause compability problems with other compilers;
  • each operating system has its own APIs, so code written for Windows XP won't necessarily compile on Ubuntu, and vice versa.

It is impossible to tell from your question what combination of these is causing problems in your case.

Without some example code, one can only guess.

Anyways, a common reason is non-standard code that worked by accident back with whatever compiler was used to develop the code, and better diagnostics in newer compilers prohibit it, or better optimizations in newer compilers compile the code so that it behaves differently than the original author intended.

It doesn't compile with gcc because it uses windows specific headers, like "conio.h" etc. and windows specific directory delimiters, like "\" instead of "/".

I got it to compile and link, under gcc on linux:

  • as already pointed out, change the "\" to "/" in the #include file names
  • change #include "io.h" to #include "sys/io.h"
  • comment out #include "conio.h" , it is not on linux and doesn't seem to have been needed
  • comment out the Windows-only flag O_BINARY in file unh_cmac.c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM