Visual Studio 2015: Extern "C" and including C header files

2267 views c++
7

I'm working on migrating a large project from VS2012 to VS2015 (baby steps, I know), and I'm running into an issue with C headers no longer compiling, erroring out on reserved c++ keywords - even though they're being included with extern C.

Here's a simplified example:

main.cpp

extern "C" {
    #include "cheader.h"
}

int main()
{
    printfFromC();
    return 0;
}

cheader.h

#ifndef HEADER_H
#define HEADER_H

extern int new;
int printfFromC();

#endif

ctest.c

#include "cheader.h"
#include <stdio.h>

int new = 0;

int printfFromC()
{
    new++;
    return printf("Hello from C (invocation %d) !\n", new);
}

with the following errors:

------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
  main.cpp
c:\[...]\cheader.h(4): warning C4091: 'extern ': ignored on left of 'int' when no variable is declared
c:\[...]\cheader.h(4): error C2143: syntax error: missing ';' before 'new'
c:\[...]\cheader.h(4): error C2059: syntax error: 'new'

answered question

This worked in VS2012?

1 Answer

9

new is a keyword in C++. You can't use it as a variable name in cross-compiled files.

posted this

Have an answer?

JD

Please login first before posting an answer.