strNOcat
strNOcat

Reputation: 923

C99 code compiles on Linux, syntax errors on Windows

I have a codebase in C99 that compiles and runs perfectly on Linux with both GCC and Clang; however, on MinGW-w64 (installed through MSYS2), it gives errors like

include/math3d.h: In function 'matPerspective':
include/math3d.h:577:1: error: parameter name omitted
matPerspective (float y_fov, float aspect, float near, float far)
 ^~~~~~~~~~~~~~
include/math3d.h:577:1: error: parameter name omitted
include/math3d.h:595:28: error: expected expression before ')' token
  m.elem[10] = -((far + near) / (far - near));
                        ^
include/math3d.h:600:34: error: expected expression before ')' token
  m.elem[14] = -((2.0 * far * near) / (far - near));

As evident, it can't even parse the header file without puking all over itself. The part of program with errors is:

static inline Matrix
matPerspective (float y_fov, float aspect, float near, float far)
{
    float a = 1.0/tan(y_fov/2.0);

    Matrix m;

    m.elem[0] = a / aspect;
    m.elem[1] = 0.0;
    m.elem[2] = 0.0;
    m.elem[3] = 0.0;

    m.elem[4] = 0.0;
    m.elem[5] = a;
    m.elem[6] = 0.0;
    m.elem[7] = 0.0;

    m.elem[8] = 0.0;
    m.elem[9] = 0.0;
    m.elem[10] = -((far + near) / (far - near));
    m.elem[11] = -1.0;

    m.elem[12] = 0.0;
    m.elem[13] = 0.0;
    m.elem[14] = -((2.0 * far * near) / (far - near));
    m.elem[15] = 0.0;

    return m;
}

I have been using this same code for over a month on Linux and everything works perfectly there.

The command used to compile was:

gcc -c -o obj/win/x86_64/code.o src/code.c -Iinclude -std=c99

The operating system used is Window 7 x64. Compiler was installed through MSYS2 by installing package group mingw-w64-x86_64-toolchain. The compilation was done (or atleast tried) on the MinGW64 shell created by MSYS2 upon installation.

Upvotes: 1

Views: 226

Answers (1)

Paul R
Paul R

Reputation: 213120

It looks like the problem is the use of far and near as parameter names - these were keywords which historically had a special meaning on crusty old x86 compilers when compiling code for segmented (16 bit) architectures - try changing the names of these parameters, to e.g. far_val and near_val.

Addition by OP: The problem was that GLAD loader includes WinDef.h which defines

#define far 

and

#define near

essentially messing up the syntax.

Upvotes: 4

Related Questions