0

I have code that contains these declarations:

class IRealNetDll;

extern "C"__declspec(dllexport)
IRealNetDll * CreateRealInstance(int ilicence);

This builds properly with Visual Studio 2012 on Win7.

But on VS 2015, 2017 on Windows 10, this line:

extern "C"__declspec(dllexport)

results in:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C3690: expected a string literal, but found a user-defined string literal instead
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error: missing ';' before identifier 'IRealNetDll'

Why do I get this error, why only on the newer compiler, and how can I prevent it?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Raheem
  • 91
  • 1
  • 8
  • 1
    Unrelated to your problem, but symbols starting with an underscore and followed by an upper-case letter (like e.g. `_IRealNetDll`) are reserved everywhere. See [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more information – Some programmer dude Aug 22 '18 at 09:18
  • 1
    Be careful about your spacing (or rather the *lack* of space between things). – Some programmer dude Aug 22 '18 at 10:07
  • 1
    @πάνταῥεῖ The error is *very* related to the `extern "C"__declspec(dllexport)` part. It's the "user-defined string literal" part of the error message that gives it away. – Some programmer dude Aug 22 '18 at 10:11

2 Answers2

7

When user-defined literals were introduced, it caused a small, breaking change to the language. String literals now need to be separated from a following identifier by whitespace, otherwise the the identifier is parsed as a user-defined literal operator:

  • "C"__declspec
    

    One token, understood as a user-defined literal using the __declspec conversion.

  • "C" __declspec
    

    Two tokens - the string literal "C" and the identifier __declspec.

You need to add a space to separate your tokens.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
1

Instead of

extern "C"__declspec(dllexport)
IRealNetDll * CreateRealInstance(int ilicence);

you should write

extern "C"
{
__declspec(dllexport) IRealNetDll * CreateRealInstance(int ilicence);
}
PilouPili
  • 2,601
  • 2
  • 17
  • 31
  • 1
    While your proposed solution should work, it's not because of the curly braces `{}`. There is an even simpler solution. – Some programmer dude Aug 22 '18 at 10:08
  • 1
    You are right I think the issue is that a space is missing after "C". But I believe using extern "C" without the curly braces is confusing – PilouPili Aug 22 '18 at 10:26