10

I was wondering what the best way to deal with code containing GCC's __attribute__ extension when using MSVC. Is the following a safe way of dealing with this:

#define __attribute__(x) /* blank - should simply ignore thanks to C preprocessor */

Thanks!

1
  • 1
    Is there a reason both the c and visual-c++ tags are specified? The most suitable answer can differ for each, and usually it doesn't make sense to mix c and c++ tags.
    – starball
    Commented Sep 9, 2022 at 7:41

4 Answers 4

10

__attribute__ is not a macro, it is a GCC specific extension that needs to be replaced with appropriate equivalent Visual C++ has. Its equivalent is usually __declspec:

http://msdn.microsoft.com/en-US/library/dabb5z75(v=vs.110).aspx

For example:

#if defined(_MSC_VER)
#define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
#else
#if defined(__GNUC__)
#define DLL_PUBLIC __attribute__ ((dllexport))
#endif
2
  • How would you go on translating this to MSVC? #define PV_API __attribute__((visibility ("default"))) Commented Apr 18, 2020 at 17:25
  • @sockevalley if you are using CMake, you can use GenerateExportHeader.
    – starball
    Commented Jun 8, 2022 at 22:04
3

Take a look at the GCC Manual and find out what each attribute does. Then find out what the MSVC equivalent is. Some can be safely ignored but some you will want the replace.

If you want your code to be truly cross platform, create your own set of macros that can be implemented correctly for each platform.

3

If the code containing the attribute is code that you control, you can switch to use c++ standard attribute syntax. For example, instead of __attribute__(hot), use [[gnu::hot]]. Note: If you also compile on MSVC with /W3, it will fire warning 5030 (unrecognized attribute) for attributes intended for other compilers such as [[gnu::hot]], so you need to add /wd5030 (warning disable: 5030).

1

Trying to redefine __attribute__ is the wrong approach here, I think.

Anything with __ in its name is typically something gunky or low-level or implementation-dependent that you probably shouldn't be using directly in your code.

So, if for no other reason than to keep (the bulk of) your code clean, you should probably be doing things like, for example:

#define PUREFUNCTION __attribute__(pure)

Then when you have a pure function to define, rather than using __attribute__ directly, you use your new macro:

PUREFUNCTION
double mysqrt(double x)
{
    ...
}

And then, when the time comes to compile your code under something other than gcc, rather than trying to kludgily redefine __attribute__, you just redefine your PUREFUNCTION macro. You can redefine it to some other compiler-specific attribute modifier, or if there isn't one, you can define it to be nothing:

#define PUREFUNCTION

and then you won't get any compilation errors on your functions tagged as PUREFUNCTION. (Obviously you won't be able to tag them as "pure", either, but if the compiler in question has no way to, then presumably there's no way to.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.