3

So I'm working on this C/C++ library, and I'm using the GNU compiler, which supports the latest standard of C and C++.

I want to make my library compatible with the VC++ compiler which only supports C89/C90, not the newer C standards that allow variable declarations after other statements inside of a function body.

Is there a tool I can use that will make all my variable declarations at the beginning of the function bodies?

My library is quite large and having a tool to do this task will make the process easy.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
turnt
  • 3,235
  • 5
  • 23
  • 39
  • 2
    there may be a compiler option to enforce stricter conformance, or generate warnings on nonconformance – Steve Townsend Feb 15 '13 at 16:06
  • You can use `-std=c90` I think to flag these up for you but I don't know of a tool that'll automatically transform the source for you. I thought this had changed, though - I thought the newer VCs had relaxed this? – Rup Feb 15 '13 at 16:09
  • 9
    I am a bit confused on why this is tagged as c++, you seem to want to make sure that your C code uses C90 only, where does C++ come into the game? – PlasmaHH Feb 15 '13 at 16:13
  • Fun fact, even MSVC supports certain aspects of C99, such as variadic macros (since VS 2003, I think) or - lo and behold - C++ comments (`//`). All things that weren't in C90. So be careful, you will probably hate switching on `-std=ansi` ... I know I did. – 0xC0000022L Feb 15 '13 at 16:20
  • I'll just point out that if you want to compile an old C library on a new compiler you'll need to thoroughly test an old C library on a new compiler as well. – djechlin Feb 15 '13 at 16:39
  • GCC has `-Wdeclaration-after-statement` which is useful to spot where declarations need changing; it does not automate the change, though. Compiing with `-std=c89 -pedantic` or equivalently `-ansi -pedantic` will give you a lot more information about what needs changing. (Also, variable declarations only need to be at the start of the block; not at the start of a function body. And beware initialized variables; you often need to move the uninitialized declaration to the start of a block and leave an assignment where the initialized declaration was.) – Jonathan Leffler Feb 15 '13 at 16:44
  • What about other C99 features? – Carl Norum Feb 15 '13 at 16:49
  • 5
    Simple question: why support VC++ for compiling C code? From the lore of the internet, there is no plans to ever make VC++ a C compiler that supports modern C features. I know that it is a major PITA to try and support C code for VC++. A somewhat hacky solution is to tell VC++ to compile .c as C++ (you can do this at the file properties level). However, then you lose some of the good features of C99 such as designated initializers. If your library only uses the subset of C++ that is valid C, then you are probably better off just compiling as C++ code with VC++. – Josh Petitt Feb 15 '13 at 17:12
  • @PlasmaHH There is a majority of C++ in the library. – turnt Feb 15 '13 at 17:33
  • @Cygwinnian: What does C++ code have to do with C90 compliancy? – PlasmaHH Feb 18 '13 at 10:06

1 Answers1

4

Sorry for the anti-answer, but Visual C++ is not a C compiler. It may be a compiler for some old, outdated (22 years outdated, to be precise) language, which is not C. The best thing you should do is to advise people that are asking you for such support about this and request them to switch compiler and not use a broken one.

If this solution is really beyond your choice (it's not), then there are similar topics that you could look for: How to compile c99-to-c89 convertor with clang? https://github.com/libav/c99-to-c89/

If you think what I said makes no sense think again: It's a 22 year outdated C compiler. Think about the effort and money that people all over the world have spent to support it.

Edit: btw, that's C: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

http://www.open-std.org/jtc1/sc22/wg14/www/standards

Community
  • 1
  • 1
hdante
  • 7,685
  • 3
  • 31
  • 36