我正在尝试构建一个静态库并将其用于编译 fortran档案。如果我这样做:
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
add_library(mylib STATIC ${lib_src}/mylib.for)
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(bin/out ${PROJECT_SOURCE_DIR}/src/program.f)
target_link_libraries(bin/out mylib)
然后一切正常(请注意该库内置于二进制目录中 root,但是fortran被编译成子目录);但是,如果我这样做
set(archives ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
add_library(mylib STATIC ${lib_src}/mylib.for)
find_library(mylib NAMES mylib PATHS ${archives})
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(bin/out ${PROJECT_SOURCE_DIR}/src/program.f)
target_link_libraries(bin/out mylib)
运行cmake时出错:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
mylib linked by target "bin/out" in directory /home/chris/project
如果我遗漏了最后两行,那么归档文件确实会得到 写入lib子目录,如预期的那样libmylib.a。如果我做
set(archives ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
add_library(mylib STATIC ${lib_src}/mylib.for)
find_library(mylib NAMES mylib PATHS ${archives})
include_directories(${archives})
set(libs ${libs} ${mylib})
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(bin/out ${PROJECT_SOURCE_DIR}/src/program.f)
target_link_libraries(bin/out {LIBS})
然后cmake命令运行正常,但运行make然后生成 编译错误(我知道set命令和target_link_libraries 变量是不同的情况 - 我不明白的事情之一 这就是为什么这只在make运行时失败,而不是cmake;如果 变量是相同的情况,然后我得到与上面相同的错误。)
那么,我怎样才能让CMake认出我的${PROJECT_BINARY_DIR}/lib
在CMake运行期间创建的文件夹?有人可以指出我的
(可能很明显)错误?!
答案 0 :(得分:4)
您不应在其中一个目标上使用find_library
,请删除该行:
find_library(mylib NAMES mylib PATHS ${archives})
CMake已经知道mylib
库,因为它是其目标之一,并且调用find_library
阴影mylib
变量。
您可以让target_link_libraries
调用相同的内容,因为参数可以是库或目标的路径.r