在CMakeLists中添加包含预编译模块的目录

时间:2015-10-22 15:59:36

标签: compilation linker cmake fortran

我正在为一个fortran代码修改CMakeLists.txt文件。我需要添加一个包含预编译模块的目录。 我怎么能在CMakeLists.txt中做到这一点?

1 个答案:

答案 0 :(得分:0)

有一个名为Fortran_MODULE_DIRECTORY

的目标属性

为了更好地衡量,我还将模块目录添加为include目录。 gfortran在包含目录中查找.mod个文件。

set_target_properties( Target PROPERTIES Fortran_MODULE_DIRECTORY "dir" )
target_include_directories(Target PUBLIC "dir" )

修改

再次阅读您的问题,我发现您感兴趣的模块已经编译完毕。 Fortran_MODULE_DIRECTORY指定PUT .mod文件的位置,并将该目录添加为查找它们的位置。 target_include_directories语句只指定在哪里查找它们。我最熟悉使用gfortran,在其手册页中有这个:

  -Idir
       These affect interpretation of the "INCLUDE" directive (as well as of the "#include" directive of the
       cpp preprocessor).

       Also note that the general behavior of -I and "INCLUDE" is pretty much the same as of -I with
       "#include" in the cpp preprocessor, with regard to looking for header.gcc files and other such things.

       This path is also used to search for .mod files when previously compiled modules are required by a
       "USE" statement.

   -Jdir
       This option specifies where to put .mod files for compiled modules.  It is also added to the list of
       directories to searched by an "USE" statement.

       The default is the current directory.

这就是CMake将为您和您的编译器做的事情;您不需要显式指定这些标志。在您的情况下,只需包含就足够了,因为模块已经编译好了。

但是,如果您发现它不能与编译器一起使用,则可能必须通过设置变量CMAKE_Fortran_FLAGS来手动指定它们。

希望这有帮助。