5

Is there any option to omit variable type or to set variable type to int in c++ code that to be compiled with g++ compiler in linux.

    const bufLen = 2000;

Compilation went fine in solaris (as I am doing porting from solaris to linux).

One more thing ,I dont have control over the file as it is generated by some parser (provided by some third party in the form of binary)

Since I cant change the c++ file(as it is generated everytime before compilation) , I need some option (of g++) so I can include during compilation to suppress/resolve the error:

error: ISO C++ forbids declaration of `bufLen` with no type

EDIT :

INFO : options currently I am using -c -fPIC -Wno-deprecated -m32 -O2 -ffriend-injection -g

Is anyone of the options causing me trouble (or affecting other) ?


Thanks in advance

Makesh
  • 1,236
  • 1
  • 11
  • 25
  • 1
    Are you sure it's a C++ (and not a C) file? C89 allowed `int` to be omitted. – avakar Jul 30 '12 at 08:54
  • 2
    Can you just compile it as C code? In other words use the C compiler and not the C++ compiler. – sashang Jul 30 '12 at 08:55
  • @sashang : It is c++ file not c file . I just mentioned one line which is giving me trouble from 10,000 line parser-generated C++ file – Makesh Jul 30 '12 at 08:56
  • Though it doesn't answer the question, but as an info: `const auto bufLen = 2000;` and compile with `g++ -std=c++0x`. – iammilind Jul 30 '12 at 08:57
  • 9
    @Makesh Can you complain to whoever is responsible for generating this crap? It’s probably very old software so the chances may be slim, but that’s the best course of action … – Konrad Rudolph Jul 30 '12 at 08:57
  • 1
    it's not that hard to 'preprocess' the file yourself and do a regex find/replace so const x = y; becomes const int x = y; – stijn Jul 30 '12 at 09:00
  • @KonradRudolph : I wished the same but it too compilcated that file is generated by more than 3 parsers (all binaries) and I dont know which parser is responsible for that segment of code :( – Makesh Jul 30 '12 at 09:05
  • 3
    have you tried -fms-extensions ? – AndersK Jul 30 '12 at 09:15
  • Implicit `int` is a disaster. I had to work some time with ancient compiler which supported it and got furious every time a constructor with misspelled name got compiled happily as a function returning int. – Tadeusz Kopec for Ukraine Jul 30 '12 at 09:26
  • Thanks everyone for your efforts on giving me suggestions – Makesh Jul 30 '12 at 09:45
  • @TadeuszKopec: I agree, implicit int is one of the stupidest ideas that were ever part of C, thank god it's gone for good... – MFH Jul 30 '12 at 09:48
  • 1
    @MFH: Arguably, automatic function prototyping is even stupider. – rodrigo Jul 30 '12 at 09:54
  • good that it worked! was just a lucky guess on my part :) – AndersK Jul 30 '12 at 11:17

2 Answers2

2

Thanks to AndersK who gave me a solution (through comments [below my question])

I tried compiling using -fms-extensions with g++ which resolved my problem

Reference : http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/C-Dialect-Options.html

PS : For visibility sake , I added the answer in answer section . Credits go to person who actually answered :)

Makesh
  • 1,236
  • 1
  • 11
  • 25
  • Do realize the irony of using -fms-extensions to get your code to compile? -fms-extensions was added to gcc to enable support for Microsoft specific extensions to the language (http://gcc.gnu.org/ml/gcc-patches/1999-08n/msg00363.html). I seriously doubt that the original code you're porting from Solaris to Linux has anything related to Microsoft in it. My guess is that real issue is the code generator is generating C code, and your problem is that you're compiling C code with the C++ compiler. Just change the compiler options to tell gcc to use the C compiler, as other people have noted. – sashang Jul 31 '12 at 15:10
1

Try -ansi or -std=c89. http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gcc/C-Dialect-Options.html

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77