CMAKE:库缺少头文件

时间:2017-01-22 03:32:15

标签: c++ linker cmake header-files

我有一个子目录,可以创建一个共享库,该库可以构建到我的根项目之外的文件夹中。我在我的cmake文件中链接它没有任何问题。但是,无论我如何重新配置​​它,编译器都找不到头文件。

文件夹结构类似于:

  • 构建
    • LIB
  • SRC
    • 文库
  • 测试
    • library_test

基本上我想链接编译到lib文件夹的库链接到它的测试。但是,无论我如何配置CMakelists,它都无法在编译期间找到库的头文件。但是,在cmake过程中没有错误。 实际的错误消息如下:

  

致命错误:找不到'file.h'文件

我最近的帖子尝试:

find_library(LESSIONS_LIBS chp_two PATHS {CMAKE_SOURCE_DIR}/build/lib/)
add_library(test_chptwo SHARED ${TEST})
target_link_libraries(test_chptwo gtest LESSIONS_LIBS)

有什么想法吗?

编辑一个

以下是我更新的CMakelists:

图书馆CMakeLists:

cmake_minimum_required(VERSION 3.7.0)

#Project Name
project (chp_two)

#Build Problem Sets as Library
#Header Files 
include_directories(include ${CMAKE_CURRENT_SOURCE_DIR}/inc/)
#Source files
set(LIB random_queue.cpp ${include})
add_library(chp_two SHARED ${LIB})

测试CMakeLists:

cmake_minimum_required(VERSION 3.7.0)

#Project Name
project(TESTING)

#Setting Test sources
set(TEST test_random_queue.cpp)

#Search for libraries
find_library(LESSIONS_LIBS chp_two PATHS {CMAKE_SOURCE_DIR}/build/lib/)

enable_testing()

#turn test into a library
add_library(test_chptwo SHARED ${TEST})

target_link_libraries(test_chptwo gtest LESSIONS_LIBS) 

1 个答案:

答案 0 :(得分:1)

如果没有您的cmake文件,很难确定。但是,您是否有机会错过include_directories调用?此调用将设置.h文件的路径以进行编译。

include_directories (path_to_my_headers)

下面是我在评论中提到的CMAKE示例。无论如何,相同的概念或多或少地适用于libs / exe。

cmake_minimum_required(VERSION 3.7.0)
# 
project (MyProj)
#
include_directories (.)
include_directories (path_to_a_dir_with_files_I_need)
include_directories (path_to_another_dir_with_files_I_need)

#
file (GLOB HEADERS *.h)
file (GLOB SOURCES *.cpp)

# Finish with your CXX flags, add_executable, target_link_libraries, install, etc