Error message:
/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
.
├── 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
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
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?