writing compiler-independent code

Hi -

I have a small program that I developed using MinGW. I'd now like to port it to MSVC; ideally, it would compile on both platforms.

1. When using MSVC, I need to specify this include file like this:
#include <tr1/cstdint>
When using MinGW, I can't have the "tr1/" in the path. Is there some way to make the include conditional on the compiler, or is there some other trick to this?

2. MSVC doesn't seem to provide me with a size_t and an ssize_t type. Do I need to bring these in from somewhere?

Thanks...I'm new to MSVC and I'm sure I'll have more questions.
Maybe you should update to a newer version of MSVC.
You could use precompiler directives.

#ifdef MSVC
#include <tr1/cstdint>
#include <cstdint>
Thanks, pogrady. Now I'm getting a really strange occurrence: in one of my header files, MSVC seems to be defined, but not in the other one.

Both tests are at the very top of the file; any idea what could be happening?

Check out this link for compiler specific macros you can use that will expand into values that you can use to determine the results of the #ifdef.

The macro you want to use is _MSC_VER_

Sorry I'm on my iPod so researching this stuff is a little tricky.
Thanks, pogrady. Actually, the macro appears to be "_MSC_VER" according to my IDE fill-in. And, it solved that problem.

I'll take these one at a time...any idea about the ssize_t? Evidently those aren't defined in the stdint file.
Windows does have SSIZE_T data type , declared in BaseTsd.h as follows:



Beware that _MSC_VER returns Visual Studio version. In MSVC 2010 there is stdint.h as well cstdint headers already present (the header has been missing starting with VS 2003). Do not make wrong assumptions regarding VS.

There is an implementation here (to make your code work with every version of VS):
Last edited on
Thanks, modoran. So, I take it that this file doesn't live anywhere in my VC distribution, and I need to download it?

And, regarding _MSC_VER: it looks like I need to check two macros: one to determine whether I'm building on a Windows platform, and another to determine whether I'm using MSVC. What would you (or anyone) suggest I use for these?



This seems to be working for me:

#ifdef _WIN32
    #ifdef _MSC_VER
//        #include "stdint_ISO_C9x.h"
        #include <BaseTsd.h>
        #include <cstdint>
    #include <tr1/cstdint>

stdint_ISO_C9x.h is my copy of the file linked to above. Using BaseTsd.h instead, however, eliminates a bunch of niggling compiler warnings, so I may go with that.

Anyway, I think I'm getting closer, since the compiler isn't giving me any more warnings. I am getting a link error now, for an unresolved external "_WinMain@16." Am I omitting a library inclusion?


It just occurred to me that the above question is more of a Windows issue than a C/C++ issue. I'll post the question there.
Last edited on
Topic archived. No new replies allowed.