2013-06-18 175 views
0

我在嘗試構建一個靜態庫並將其用於編譯一個 fortran文件。如果我做的:在CMake中查找目錄

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 

如果我離開了最後2行,然後存檔文件並獲得 寫入到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識別在CMake運行期間創建的我的${PROJECT_BINARY_DIR}/lib 文件夾?有人能指出我的 (可能很明顯)的錯誤嗎?!

回答

4

你不應該在你的目標的一個使用find_library,刪除行:

find_library(mylib NAMES mylib PATHS ${archives}) 

CMake的已經的已知,有關mylib庫,因爲它是其目標之一,並呼籲find_library陰影mylib變量。

你可以讓你的target_link_libraries調用相同,因爲參數可以是庫或目標的路徑.r