I am trying to setup a modern cmake project so it can be easily maintainable for my team. But when I compile I get a linker error in the main that my library functions are undefined.
I have a project with the following layout.
CMakeLists.txt
src/
├─ CMakeLists.txt
├─ main.cpp
├─ lib/
│ ├─ include/
│ │ ├─ lib.h
│ ├─ tests/
│ ├─ CMakeLists.txt
│ ├─ src/
│ │ ├─ lib.c
My CMakeLists contains:
root/CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32_gcc.cmake)
project(PROJECTNAME CXX C ASM)
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
add_subdirectory(src)
src/CMakeLists.txt:
add_subdirectory(freertos)
add_subdirectory(bsp)
set(PROJECT_SOURCES
main.c
)
add_executable(stm32-freertos.elf ${PROJECT_SOURCES})
target_link_libraries(stm32-freertos.elf
PRIVATE
PROJECTNAME::LIBNAME
)
src/lib/CMakeLists.txt:
set(LIB_NAME "projectname-libame")
set(LIB_ALIAS "PORJECTNAME::LIBNAME")
set(LIB_SOURCES
src/lib.cpp
)
set(LIB_HEADERS
include/lib.h
)
add_library(${LIB_NAME} ${LIB_SOURCES} ${LIB_HEADERS})
add_library(${LIB_ALIAS} ALIAS ${LIB_NAME})
target_include_directories(${LIB_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
src
)
main.cpp:
include <lib.h>
int main(){
call_lib_fun();
}
I am pretty new to cmake so any help or comment even if not directly associated on the problem is appreciated.
And here is the error
/home/user/bin/gcc-arm-none-eabi-9-2020-q2-update/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld: CMakeFiles/stm32-freertos.elf.dir/main.c.obj: in function `main':
main.c:(.text+0xa): undefined reference to `call_lib_fun'
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/stm32-freertos.elf.dir/build.make:194: src/stm32-freertos.elf] Error 1
make[2]: Target 'src/CMakeFiles/stm32-freertos.elf.dir/build' not remade because of errors.