CMake链接库未找到标题

yv5phkfx  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(166)

这是我的目录树

我实现了accident组件,它必须是一个独立的库。下面是它的CMakeLists.txt

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)

现在我尝试在note部分中使用这个库,它应该与accident链接。特别是accident.hpp文件应该在我的IDE中可见,而不需要执行这样的操作


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

cpp中的代码也应该是可访问的。

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)

不幸的是,它不工作-意外。hpp头没有找到。你知道我在哪里做错了吗?

编辑

为了回答@Martin的问题,这里是我的顶级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

note目录在src中,src中包含CMakeLists.txt,只有一行

add_subdirectory (note)

在这个 note 目录中,我们有我粘贴在 note 目录中的顶级CMakeLists.txt上面的内容,包含

add_subdirectory (accident)
add_subdirectory (note)

编辑2

问题已解决-请参阅下面的Ave Milia评论。谢谢!

n8ghc7c1

n8ghc7c11#

正如我们在注解部分所指出的,测试可执行文件被忘记链接到相关的静态库。

相关问题