# Linker error is being generated when I try to generate a seperate binary for tests only

**URL:** <https://discourse.cmake.org/t/linker-error-is-being-generated-when-i-try-to-generate-a-seperate-binary-for-tests-only/11399>\
**Category:** Code\
**Created:** [August 6, 2024, 9:09pm UTC](https://discourse.cmake.org/t/linker-error-is-being-generated-when-i-try-to-generate-a-seperate-binary-for-tests-only/11399 "2024-08-06T21:09:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony](https://discourse.cmake.org/user_avatar/discourse.cmake.org/anthony/32/4838_2.png) [@Anthony](https://discourse.cmake.org/u/Anthony)\
**Post date:** [August 6, 2024, 9:09pm UTC](https://discourse.cmake.org/t/linker-error-is-being-generated-when-i-try-to-generate-a-seperate-binary-for-tests-only/11399/1 "2024-08-06T21:09:56Z")

</div>

Error message:

```auto
/usr/bin/ld: CMakeFiles/test.dir/test_lexer.c.o: in function `test_next_token':
test_lexer.c:(.text+0x11f): undefined reference to `lexer_create'
/usr/bin/ld: test_lexer.c:(.text+0x144): undefined reference to `lexer_next_token'
/usr/bin/ld: test_lexer.c:(.text+0x1d7): undefined reference to `lexer_destroy'
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/test.dir/build.make:98: test/test] Error 1
make[1]: *** [CMakeFiles/Makefile2:143: test/CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

```

File structure

```auto
.  
├── CMakeLists.txt
├── include
│ ├── lexer.h
│ └── token.h
├── LICENSE
├── README.md
├── src
│ ├── lexer.c
│ ├── main.c
│ └── token.c
└── test
    ├── CMakeLists.txt
    └── test_lexer.c

```

./CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.25)
project(monkeylang)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include_directories(include)

link_directories(test)

add_subdirectory(test)

add_executable(${PROJECT_NAME} 
    src/main.c
    src/lexer.c
    src/token.c
    include/lexer.h
    include/token.h
)

```

test/CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.25)
project(test)

set(CMAKE_C_STANDARD 99)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(FetchContent)

FetchContent_Declare(
    unity
    GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
    GIT_TAG v2.6.0
)

FetchContent_MakeAvailable(unity)

add_executable(${PROJECT_NAME} test_lexer.c)

target_link_libraries(${PROJECT_NAME} unity)

```

I want to generate a seperate binary for testing only, but a linker error occurs when I try to reference function definitions from `include/` and `src` , Any idea how to resolve this?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 7, 2024, 12:22pm UTC](https://discourse.cmake.org/t/linker-error-is-being-generated-when-i-try-to-generate-a-seperate-binary-for-tests-only/11399/2 "2024-08-07T12:22:29Z")

</div>

> [@Anthony](#):
>
> a linker error occurs when I try to reference function definitions from `include/` and `src`

From what I see, the `test` target doesn’t link to anything that compiles those sources, so that error is to be expected. You could link to `monkeylang` target (_if its sources contain the symbols you need in `test`_), although it isn’t very usual to link to executables; or perhaps you could make a library and link to that instead.
