Search code examples
c++cgoogletest

problem with compiling C project in a C++ unit test


I have a struct forward declaration like below in C I added googletest and want to test my code using I added extern "C" in my test.cpp file but I get the error

baseCommand.h file

typedef struct Command_t Command_t;

struct Command_t{

    uint8_t id;
    uint8_t procId;
    uint8_t priority;
    char initCommand[50];
    char command[50]; 
    char commandParam[450];
    char finishParam[10];
    int32_t (*fpInit)(struct Command_t* this);
    uint16_t initDelayMs;
    int32_t (*fpSend)(struct Command_t* this);
    uint16_t sendDelayMs;
    int32_t (*fpReceive)(struct Command_t* this);
    char expectedAnswerOnSucessCommand[100];
    char expectedAnswerOnError[100];
    uint16_t receiveDelayMs;
    int32_t (*fpProc)(struct Command_t* this);
    int8_t retry;
    void (*fpReset)(void);
    int port;
    int32_t (*fpCtor)(struct Command_t* this);
};

test.cpp file

extern "C" {

#include "commands/baseCommand.h"


}

# include "gtest/gtest.h"


TEST(IntegerFunctionTest, negative) {
    
        EXPECT_EQ(1, 1);

}

but I got the error (Updated I deleted some not related files )

expected ',' or '...' before 'this' baseCommand.h

on line

int32_t (fpInit)(struct Command_t this);


Solution

  • Here:

    int32_t (*fpInit)(struct Command_t* this);
    

    You're getting an error because this is a keyword in C++ and therefore can't be used as an identifier. You need to change the name to something else.