cmake执行add_subdirectory命令的顺序

时间:2015-11-01 06:22:20

标签: cmake dependencies

我的cmake配置中存在依赖性问题。 当我从项目的根目录开始构建时,它总是会出错。当我在add_subdirectory(application)处禁用LibCalcBin时,它会成功构建library。然后,我可以构建application

为什么cmake不按照我在add_subdirectory命令中指定的顺序构建library然后application。有什么方法可以解决这个问题吗?谢谢。

错误

Please set them or make sure they are set and tested correctly in the CMake files:
LibCalcBin
    linked by target "run" in directory ...

./ CMakeList.txt(根目录)

cmake_minimum_required(VERSION 3.3)
project(DLLAbstract)

# specify where to put executable
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

# specify where to put binaries
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

# compile and build library
add_subdirectory(library)

# compile and build application
add_subdirectory(application)

./文库

set(src LibCalc.cpp Calculator.cpp)

add_definitions(-DDLL_EXPORT)

add_library(LibCalc SHARED ${src})

./应用

set(Src main.cpp)

find_path(LibCalcHeader
    NAMES 
        LibCalc.hpp
    PATHS 
        ${PROJECT_SOURCE_DIR}/library
)

find_library(LibCalcBin
    NAMES 
        LibCalc
    PATHS 
        ${PROJECT_SOURCE_DIR}/bin/Debug
        ${PROJECT_SOURCE_DIR}/bin/Release
        ${PROJECT_SOURCE_DIR}/bin
)

include_directories(${LibCalcHeader})

add_executable(run ${Src})

target_link_libraries(run ${LibCalcBin})

1 个答案:

答案 0 :(得分:1)

find_library命令用于查找不属于项目的库。对于由add_library命令创建的库,不需要特殊处理,您可以在target_link_libraries调用中使用目标名称:

target_link_libraries(run LibCalc)