samedi 2 juillet 2016

C struct definition in *.c, declaration in *.h, callable from *.cpp

I have a struct that I'm trying to compile using strict, standard C11, something like:

PointT.C

#include <stdlib.h>
#include "PointT.h"
struct PointT {
    union {
            float data[3];
            struct {
                    float x, y, z;
            };
    };
 };

With a header/interface file, something like:

PointT.h

#ifndef _POINTT_H
#define _POINTT_H

struct _PointT; 
typedef struct _PointT PointT;

#endif //_POINTT_H

And finally, callable from a strict, C++11 compiled source file:

main.cpp

#include <iostream> 
#include "PointT.h"

int main(int argc, char* argv[])
{
    PointT test;
    std::cout << test.x << std::endl;
    return 0; 
}

This fails using clang-700.1.76 with the following message:

error: variable has incomplete type 'PointT' (aka '_PointT')

Of course, I could compile this with C++, but apparently anonymous structs are a GNU extension, and this is intended for a project that is being developed cross-platform, using multiple compilers.

Imaginary bonus points if someone knows how to do this with CMake...

Aucun commentaire:

Enregistrer un commentaire