0

Here is my directory tree

enter image description here

I implemented accident component which have to be a standalone library. Here is CMakeLists.txt for it

set (ACCIDENT accident)
file (GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
file (GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp")
add_library (${ACCIDENT} STATIC ${HEADER_FILES} ${SOURCE_FILES})
target_include_directories (${ACCIDENT} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_subdirectory (test)

and that works fine. Now I am trying to use that library in note part which should be linked with accident. Especially the accident.hpp file should be visible in my IDE without doing things like this

#include "../../accident/include/accident.hpp"

and the code from accident.cpp should also be accessible. My attempts was similar to this one

set (MUSIC_NOTE music_note)
file (GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
file (GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp")

add_library (${MUSIC_NOTE} STATIC ${HEADER_FILES} ${SOURCE_FILES})
target_include_directories (${MUSIC_NOTE}
    PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
    PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../accident/include"
)
target_link_libraries (${MUSIC_NOTE} accident)
add_subdirectory (test)

unfortunately, it does not work - accident.hpp header is not found. Do you know where I am doing a mistake?

EDIT

In response to @Martin's question, here is my top level CMakeLists.txt

cmake_minimum_required (VERSION 3.5)
set (CMAKE_BUILD_TYPE Debug)

set (PROJECT_NAME scales)
project (${PROJECT_NAME} LANGUAGES CXX)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

set (CMAKE_MODULE_PATH "${CMAKE_BINARY_DIR}" "${CMAKE_MODULE_PATH}")

set (CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}" "${CMAKE_PREFIX_PATH}")

add_subdirectory (src)
add_subdirectory (external)

option (BUILD_TESTS "Build tests" ON)
enable_testing ()

if (BUILD_TESTS)
    add_subdirectory (test)
endif ()

# Add google test
include (FetchContent)
FetchContent_Declare (
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG release-1.12.1

)

The note directory is in src, and src includes CMakeLists.txt with only one line

add_subdirectory (note)

In this note directory we have the stuff I pasted above Top level CMakeLists.txt in note directory contains

add_subdirectory (accident)
add_subdirectory (note)

EDIT 2

Problem resolved - see Ave Milia comment below. Thanks!

NiklasMan
  • 43
  • 5
  • What's the content of your top level CMakeLists.txt please ? Seems like you provided accident/CMakeLists.txt and note/CMakeLists.txt but not the main one – Martin Aug 29 '22 at 19:52
  • Can you show usage code? Your `#include` in the code. `#include "accident.hpp"` in e.g. `note.cpp` should work with this code. – Ave Milia Aug 29 '22 at 19:53
  • @AveMilia yes, this is only #include "accident.hpp" and QT creator says to me /projects/scales/src/note/note/include/note.hpp:13: error: accident.hpp: No such file or directory In file included from /projects/scales/src/note/note/test/src/note_test.cpp:15: No such file or directory 13 | #include "accident.hpp" | ^~~~~~~~~~~~~~ – NiklasMan Aug 29 '22 at 20:03
  • Seems like your `note/accident/CMakeLists.txt` is never called (not in CMakeLists.txt) you provided. You are missing and `add_subdirectory(accident)` just above `add_subdirectory(note)` in your `note/CMakeLists.txt` – Martin Aug 29 '22 at 20:20
  • 1
    @PowerCwel you didn't post your `note/test/CMakeLists.txt` file. Perhaps you are not linking the `accident` library to your test executable? If you are linking to `note`, then it should link transitively but just to be sure and follow modern syntax you should write `target_link_libraries (${MUSIC_NOTE} PUBLIC accident)`. – Ave Milia Aug 29 '22 at 20:21
  • @AveMilla You are right! The problem was in the test - I didn't link libraries there. Thank you very much! – NiklasMan Aug 29 '22 at 20:29

1 Answers1

1

As we figured out in the comment section, the test executable was forgotten to be linked to the relevant static library.

Ave Milia
  • 599
  • 4
  • 12