# Googletest CMAKE using library including library !

**URL:** https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902
**Category:** Usage
**Tags:** tool:cmake
**Created:** [January 25, 2022, 9:45am UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902 "2022-01-25T09:45:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![SHOAR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@SHOAR](https://discourse.cmake.org/u/SHOAR)
#### Post date: [January 25, 2022, 9:45am UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902/1 "2022-01-25T09:45:14Z")

</div>

Hello,  
I do have the following workspace structure structure  
C:/Workspace/Project1  
C:/Workspace/Project1/file1.c  
C:/Workspace/Project1/file1.h  
C:/Workspace/Project1/unit\_test.cpp  
C:/Workspace/externalLib/  
C:/Workspace/Project1/googletest

The goal is the unit\_test is testing “function\_to\_test” defined in file1.c. But this function also calls an external function (lib1\_fucntion) defined in the library C:/Workspace/externalLib/lib1.lib

So My CMake definition includes:

…

include\_directories (googletest)  
include\_directories(C:/Workspace/Project1)  
enable\_testing()  
add\_subdirectory(googletest)

set (Headers file1.h)  
set (Sources file1.c )  
set(CMAKE\_CXX\_FLAGS\_DEBUG “${CMAKE\_CXX\_FLAGS\_DEBUG} /MTd”)  
add\_library (file\_1\_libray ${Sources} ${Headers})  
target\_link\_libraries(file\_1\_libray “C:/Workspace/externalLib/lib1.lib”)

add\_executable( runUnitTests unit\_test.cpp )  
target\_link\_libraries(runUnitTests gtest gtest\_main)  
target\_link\_libraries(runUnitTests file\_1\_library )

My issue is that when building the unit\_test.obj, it can not resolve the symbol function\_to\_test.

What am i missing in my CMakeLists.txt deifnition?

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [January 25, 2022, 7:16pm UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902/2 "2022-01-25T19:16:19Z")

</div>

I don’t want to give unnecessary/confusing answers or ideas, so I’d first like to better understand the project setup:

1. What header files are included in `unit_test.cpp`?
2. Where is `function_to_test` declared?

---

<div class="post-metadata">

### Author: ![SHOAR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@SHOAR](https://discourse.cmake.org/u/SHOAR)
#### Post date: [January 25, 2022, 8:01pm UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902/3 "2022-01-25T20:01:31Z")

</div>

Alain , here is more details :  
In unit\_test.cpp , the header is  
#include \<gtest/gtest.h\>  
#include “file1.h”

and `function_to_test` is decalred in file1.c and also in file1.h.  
For instance , function\_to\_test is calling funct\_libray1 provided in the library C:/Workspace/externalLib/lib1.lib  
I was wondering if it is still possible to perform a unit test on a function\_to\_test that needs and external library to be used.  
Or should i also defined mocked/stubs for the fucntion defined in this external library or refactor the code?

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [January 25, 2022, 9:02pm UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902/4 "2022-01-25T21:02:31Z")

</div>

> [@SHOAR](#):
>
> My issue is that when building the unit\_test.obj, it can not resolve the symbol function\_to\_test.

What is the exact error (including error code and error message) from the compiler/linker?

---

<div class="post-metadata">

### Author: ![SHOAR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@SHOAR](https://discourse.cmake.org/u/SHOAR)
#### Post date: [January 25, 2022, 9:04pm UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902/5 "2022-01-25T21:04:13Z")

</div>

The error is  
error LNK2019: unresolved external symbol function\_to\_test referenced in function "private: virtual void  
\_\_cdecl TestMyClass\_First\_Test::

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [January 25, 2022, 9:11pm UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902/6 "2022-01-25T21:11:48Z")

</div>

Are you certain the definition of `function_to_test` in `file1.c` matches its declaration in `file1.h`?

Also, I noticed something odd in your first post. In

> [@SHOAR](#):
>
> add\_library (file\_1\_libray ${Sources} ${Headers})  
> target\_link\_libraries(file\_1\_libray “C:/Workspace/externalLib/lib1.lib”)

you are specifying `file_1_libray`, with a single `r`. But in

> [@SHOAR](#):
>
> tarrget\_link\_libraries(runUnitTests file\_1\_library )

you are using `file_1_library`, with two `r`.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [January 26, 2022, 7:09am UTC](https://discourse.cmake.org/t/googletest-cmake-using-library-including-library/4902/7 "2022-01-26T07:09:56Z")

</div>

Also, since this seems to be Windows: maybe you build it as a DLL (if `BUILD_SHARED_LIBS` is `ON`) and forgot to `dllexport`?
