我正在尝试构建一个静态库并将其用于编译 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)
然后一切正常(注意库被构建到二进制目录根目录中,但 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 运行期间创建的文件夹?有人能指出我的(可能很明显)错误吗?!