views:

64

answers:

3

I'm working with quite a big codebase which compiles fine in linux but vc++ 2008 spits errors.

The problem code goes like this:

Declaration:
typedef float vec_t;
typedef vec_t vec2_t[2];

The codebase is littered with in-place construction like this one:
(vec2_t){0, divs}

Or more complex:
(vec2_t){ 1/(float)Vid_GetScreenW(), 1/(float)Vid_GetScreenH()}

As far as I know, this code constructs a struct, then converts it to an array and passes the address to the function. I personally never used in-place construction like this so I have no clue how to make this one work.

The compiler produces a bunch of syntax errors like these:
Error 2 error C2143: syntax error : missing ')' before '{'
Error 3 error C2059: syntax error : ')'
Error 4 error C2143: syntax error : missing ';' before '{'

I don't maintain the linux build, only the windows one. And I can't get it to compile. Is there some switch, some macro to make vc++ compile it?

Maybe there is a similar nifty way to construct those arrays and pass them to the functions in-place that compiles just fine in vc++?

A: 

If you simply want to compile your codes under Windows and it doesn't have to use MSVC, have you tried using GCC? it might be caused by specific extensions of GCC.

typedef float vec_t;
typedef vec_t vec2_t[2];

vec_t Vid_GetScreenW()
{
    return 600.f;
}
vec_t Vid_GetScreenH()
{
    return 300.f;
}
int main() {
    vec_t divs = 3.f;

    //The codebase is littered with in-place construction like this one:
    vec_t* vec1 = (vec2_t){0, divs};

    //Or more complex:
    vec_t* vec2 = 
          (vec2_t){ 1/(float)Vid_GetScreenW(), 1/(float)Vid_GetScreenH()};
    return 0;
}

The snippet above compiles with g++ (MinGW) under Windows XP and produce the following error using cl.exe:

E:\tmp>g++ vec.cc

E:\tmp>cl vec.cc Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86

Copyright (C) Microsoft Corporation. All rights reserved.

vec.cc vec.cc(16) : error C2059: syntax error : '{' vec.cc(16) : error C2143: syntax error : missing ';' before '{' vec.cc(16) : error C2143: syntax error : missing ';' before '}' vec.cc(19) : error C2059: syntax error : '{' vec.cc(19) : error C2143: syntax error : missing ';' before '{' vec.cc(19) : error C2143: syntax error : missing ';' before '}'

afriza
G++'s Windows support is *horrible*. (Namely because the compiler *still* does not have proper Unicode support! :( ) Also, this doesn't answer the OP's question.
Billy ONeal
I am using g++ bundled with Qt 4.6 for Windows and it seems like Qt's codes compile just fine using g++ under windows.
afriza
Most Qt apps on windows do not support Unicode. Q.E.D.
Billy ONeal
+1  A: 

I believe this is a language extension, supported by GCC that probably isn't available in MSVC. You can look around for a "Constructor Expression" in your docs, but your only solution might be to get a GCC for windows. See:

Constructor Expressions

John
+1  A: 

You're using a GCC extension that MSVC simply doesn't support, "compound literals", also called "constructor expressions" in older GCC docs.

If you want portable code, I think you'll need to change the code to declare the structs normally and initialize them with initializers that have constants expressions or using standard assignments (or use something like MinGW as your Windows compiler, if that'll do the trick).

Michael Burr