Dear All,
I’ve starting using cmake only a couple of days ago to upgrade the build system (Makefiles pretty much) of my Fortran projects.
I’ve run into the problem described below and apparently it has not been treated before in this forum…
LIB_A is an already compiled, static library written in C.
LIB_B is an already compiled, static library written in Fortran. It is the interface to LIB_A.
LIB_C is an already compiled, static library written in Fortran.
LIB_D is the library I’m trying to build in this project.
LIB_D needs LIB_B and LIB_C.
Since the project uses the interface LIB_B, it does not need the header files .h of LIB_A.
How do I specify that, at linking time, that ld needs to link the test program to libLIB_A.a ?
It also should be done in a particular order since LIB_B depends on LIB_A.
The CMakeLists.txt file below fails to build if I run “cmake --build build”.
Would you be so kind to help me?
Thank you so much
cmake_minimum_required(VERSION 3.1)
project(P44 VERSION 1.0
DESCRIPTION "LIB_D: A library to do this and that"
LANGUAGES Fortran)
enable_language(Fortran)
add_library(LIB_A UNKNOWN IMPORTED)
find_library(LIB_A NAMES lib_a HINTS path_a)
add_library(LIB_B UNKNOWN IMPORTED)
find_library(LIB_B NAMES lib_b HINTS path_b)
add_library(LIB_C UNKNOWN IMPORTED)
find_library(LIB_C NAMES lib_c HINTS path_c)
add_dependencies(LIB_B LIB_A)
set(SRC_DIR src)
set(SRC_FILES
${SRC_DIR}/lib_d_file1.f90
${SRC_DIR}/lib_d_file2.f90
)
set( CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/modules)
add_library(LIB_D STATIC ${SRC_FILES} )
target_include_directories(LIB_D PUBLIC path_b/modules)
target_include_directories(LIB_D PUBLIC path_c/modules)
target_link_libraries(LIB_D ${LIB_B})
target_link_libraries(LIB_D ${LIB_C})
add_executable(test_lib_d_program test/test_lib_d.f90)
target_link_libraries(test11 LIB_D)
install(DIRECTORY ${CMAKE_Fortran_MODULE_DIRECTORY} DESTINATION ${CMAKE_INSTALL_PREFIX})
install(TARGETS LIB_D DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)