error C2059: syntax error :'}' when adding c source files to project

22,508

Solution 1

This syntax:

struct MyStruct someObject = {.foo = bar, .baz = quux};

is called designated initializers. It's only valid in the C99 dialect of C—it's invalid in C89 and in all versions of C++. Microsoft Visual Studio's C compiler is not C99-compliant, so it will not be able to compile that code. You must either convert the code to use C89 or C++, or use a different compiler which supports C99.

Solution 2

C99 initialization style (designated initializers) is not supported in C++, see here or here. In other words, { .blah = 42 }; is illegal in C++.

What you can do is to create a C wrapper to this library, the file will be compiled in C but the functions will be available to C++ code. Note that msvc compiler does not support C99.

Share:
22,508
Sam
Author by

Sam

Updated on July 10, 2022

Comments

  • Sam
    Sam over 1 year

    I'm writing a C++ application in visual studio express for windows phone 8.

    I'm trying to use flite, a text to speech library written in c, so far I've added its source files and headers, and I've set the option to use precompiled headers to no on all of the individual c files, However the source files still do not compile, instead the compiler complains (many times) :

     error C2059: syntax error : '.'
     error C2059: syntax error : '}'
    

    It complains of these problems for this code in the flite source:

    DEF_STATIC_CONST_VAL_STRING(ffeature_default_val,"0");
    

    The definition of DEF_STATIC_CONST_VAL_STRING being:

    #define DEF_CONST_VAL_STRING(N,S) const cst_val N = {{.a={.type=CST_VAL_TYPE_STRING,.ref_count=-1,.v={.vval= (void *)S}}}}
    

    Here you can see the "." and "}" the compiler complains of. I've not modified the c source in anyway, and it builds for iOS and Android projects, so I'd assume I've not grasped how to include C files in visual express. On a side note, in Visual Express, the icons next to the .c files are "++" :/

    Any help is greatly appreciated.