使用dir-dir源构建库

时间:2016-01-03 22:31:35

标签: cmake static-libraries libraries

我遇到了CMake和add_library()命令的问题 设置如下:
main.cpp需要源文件位于MyLib的库dir0/libraries。由于我有很多项目,我不想复制每个目录中的源文件(不幸的是,在这个特定的设置中,我无法安装MyLib),所以GetModules.cmake应该采取来源文件并在我当前的libMyLib.a中构建project
但是我无法构建libMyLib.a。它不只是在错误的目录中,它在任何地方都不存在。


               build 
              /
       project - main.cpp, CMakeLists.txt
     /
    /
dir0 - GetModules.cmake
    \
     \
      libraries - mylib.cpp, mylib.hpp

CMakeLists.txt:包含GetModules.cmake并运行get_modules()


    cmake_minimum_required(VERSION 2.8)
    project(ExampleProject)

    include(../GetModules.cmake)
    get_modules()
    include_directories(${MYLIB_DIR})

    add_executable(main main.cpp)
    target_link_libraries(main ${MYLIB_LIBRARY})

GetModules.cmake:(应该)在libMyLib.a

中构建dir0/project/build

    cmake_minimum_required(VERSION 2.8)
    project(GetModules)

    function(get_modules)
    set(MYLIB_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/../libraries PARENT_SCOPE) 
    message(STATUS "Trying to get it from ${MYLIB_INCLUDE_DIR} ...")

    # these two lines are to check if the necessary files exist
    if (EXISTS ${MYLIB_INCLUDE_DIR}/mylib.cpp)
       message("found mylib.cpp!")
    endif()
    if (EXISTS ${MYLIB_INCLUDE_DIR}/mylib.hpp)
       message("found mylib.hpp!")
    endif()

    # MyLib needs the OtherLib library
    find_package(OtherLib REQUIRED)
    include_directories(${OTHERLIB_DIRS})

    # adding the library -> in dir0/project/build ?
    add_library(MyLib ${MYLIB_INCLUDE_DIR}/mylib.cpp)
    target_link_libraries(MyLib ${OTHERLIB_LIBRARIES}) 

    # set variable for easy linking in dir0/project/CMakeLists.txt
    set(MYLIB_LIBRARY libMyLib.a PARENT_SCOPE) 

    # check if MyLib was successfully built and all files exist
    if (EXISTS ${MYLIB_LIBRARY} AND EXISTS ${MYLIB_INCLUDE_DIR}/mylib.hpp)
        message(STATUS "Function GET_MODULES: Set MyLib directory to variable MYLIB_INCLUDE_DIR")
        message(STATUS "Function GET_MODULES: Set MyLib library to variable MYLIB_LIBRARY")
    else()
        message(FATAL_ERROR "FATAL -- Couldn't build MyLib!")
    endif()

    endfunction(get_modules)

执行cmake ..时,我收到mylib.*pp已找到的消息,但也有FATAL_ERROR! 为什么我不能建立我的库MyLib?

我非常感谢任何帮助,想法或暗示!

0 个答案:

没有答案