0

I am trying to test c code using gtest (with visual studio). I have two projects under the same solution: a C project and a gtest project using c++. The code is attached below:

source.c

#include "source.h"

int fun(int x)
{
    return x + 1;
}

int main()
{
    return 0;
}

source.h

#pragma once
int fun(int);

test.cpp (testing project)

#include "pch.h"
extern "C" {
#include "../runtest/source.h"
}

TEST(TestCaseName, TestName) {
  EXPECT_EQ(fun(5), 6);
  EXPECT_TRUE(true);
}

The following codes doesn't build and returns the following error:

Error LNK2019 unresolved external symbol _fun referenced in function "private: virtual void __thiscall TestCaseName_TestName_Test::TestBody(void)" (?TestBody@TestCaseName_TestName_Test@@EAEXXZ) Sample-Test1 D:\dev\C\runtest\Sample-Test1\test.obj 1

If I am changing the include statement at test.cpp to #include "../runtest/source.c" - the test runs properly. I don't think the problem is with the linkage because it can run the c code from the c++ project. But I can't understand why the header is problematic.

Thank you :)

user2740785
  • 141
  • 1
  • 3
  • 9

0 Answers0