1

I want to do Unit Tests for a C project in Visul Studio 2017. It is a embedded project lib. I want make as few changes as possible to lib. Just compile c code in VS I have added some #ifdef WIN32. It ok for added some more.

Started with using Visual C++ Native Unit Test Project. But run in problems when wanted test private static functions from C files and inject values varibales in project.

  1. Include my embedded project as Static Library in to VS solution. (or should use Dynamic-Link library or something else?)
  2. Created Native Unit Test Project, and gave it the name UnitTest1.
  3. In UnitTest1 added this line as I understand it will include the my lib that want test. #pragma comment(lib, "../Release/Embedded.lib")
  4. In the embedded code header file I added this Macro to create lib that I will preform the test on.

    #pragma once
  5. #ifdef EXPORT_TEST_FUNCTIONS #define UNITTEST_Embedded_EXPORT __declspec(dllexport) #else #define UNITTEST_Embedded_EXPORT #endif
  6. inline UNITTEST_ZAP_EXPORT static bool isActiv(void);
    1. In the embedded C file, I added this line #define EXPORT_TEST_FUNCTIONS and this function:

    UNITTEST_ZAP_EXPORT static bool isActiv();
    {
        return false;
    }
    1. But when I build UnitTest1 get error LNK2001.

Very unsure how to proceed to make it work. Anyone who can guide me through my problem? I would like to be able to test all functions in C code. Hope it not hard understand what want get help with. I have only developed couple of years.

  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – harper Jul 22 '19 at 05:55

1 Answers1

0

I hade same problem as you. You need to explain to the C++ compiler in Visual Studio a C header is coming when you include the declaration for the C function. So your UnitTest1.cpp should begins with:

extern "C" {
#include "somecode.h"
}

static_lib1.hshould contain something like:

 #ifndef STATIC_LIB1_H_
 #define STATIC_LIB1_H_

 SWord doSomeThing();

 #endif

Remove everything extra code that have added in your h and c-files for unit test it should not be needed. Best unit test is when don't do any changes to code you test on becouse of unit test need it.

I sure this should fix your problem.