-1

I am assigned to port a big project from a dialect of C99 to Visual C++ 2013.

The dialect of C99:

  1. When C99 and C++ have different behaviors, choose C++.

  2. Supports some C++ features, e.g. function/operator overloading.

  3. Supports some C99 features, e.g. compound literals, variadic macros, and designated initializer.

Before getting stuck, I wanna know if we can write C99/C++ mixed code in VS2013?

It must be compiled on both compilers, I guess there might be some amazing pragmas like

 struct T {
     int data;
 };
 #pragma CompiledByC99_begin
 struct T tmp = { .data = 1 };
 #pragma CompiledByC99_end

Unfortunately, nothing found after Googling. Does such hack exist in VS2013?

  • I dont know if you can really do this with vs, I would go with CMake (supported by vs), take a look over it, you can modify the build behaviour with it as you need – Netwave Feb 25 '16 at 08:53
  • The code you posted doesn't make sense, because you certainly don't want `.data` to be initialized to `1` in one compiler, and uninitialized in the other one. What you *can* do is define certain macros depending on the compiler, which is basically the standard way for cross-compiler support. This also includes compiler-specific keywords (like `far` or `near` for embedded firmware programming). Projects sometimes even define their own entire sets of defines for all primitive types (like `U08`, `S08`, `U16` for `byte`, `signed byte` or `signed short`). Btw, why not VS2015 community edition? – vgru Feb 25 '16 at 08:57
  • Define what you mean by porting... Are you expected to rewrite genuine C++11 to replace C99 code? Also, did you try to compile your C99 code with your C++11 compiler? Then, what concrete issues did you encounter (be specific, e.g. give compiler errors)? So **edit your question** to improve it, it is currently unclear. – Basile Starynkevitch Feb 25 '16 at 09:00
  • If I am trying to read between the lines, project was compiled with C++ compiler which supported some of C99 features as an extension? Because C99 compiler that supports funciton/operator overloading sounds horrible. What compiler were you using originally? – user694733 Feb 25 '16 at 09:14
  • This looks like a case where the introduction of a few macro's may reduce the clutter quite a bit. But stuff like the ` = { .data = 1 }` should just be rewritten as a normal initialization followed by member assignment. – MSalters Feb 25 '16 at 09:15
  • IMHO: Don't mix, make it standard compliant C++ (C++11 or even C++14, though both will probably be hard with VS2013) for as much as you can, compile these parts with a (standard compliant) C++ compiler, and compile the rest with the compiler handling your "C99 dialect", then link everything together. This will help you in the long run. – Daniel Jour Feb 25 '16 at 09:51

2 Answers2

2

You can only use a limited selection of C99 features in Visual Studio 2015 and even less in 2013 (see this question for reference)

Quote from MSDN Blog entry related to your needed features:

Additionally, some C99 Core Language features will be implemented in 2013 RTM:

C99 _Bool

C99 compound literals

C99 designated initializers

C99 variable declarations

So, depending on what exactly you need, you may be able to do it without any hack

Community
  • 1
  • 1
Jan Hohenheim
  • 3,552
  • 2
  • 17
  • 42
0

If you need to change some small portions of code to make them compilable by MSVC2013, you can use the _MSC_VER. It is defined for MSVC compilers and has integer value 1800 for VC 2013.

So you can use

#ifdef _MSC_VER
// code compilable under MSVC
#else
// other dialect
#endif

(Refs on MSDN)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252