CMake:将Fortran与静态C库链接失败
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CMake:将Fortran与静态C库链接失败相关的知识,希望对你有一定的参考价值。
使用CMake和GCC构建一个带有iso_c_binding
的非常基本的Fortran项目在链接阶段失败。手动编译和链接工作正常:
$ gcc6 -c getkey.c
$ gfortran6 -Wl,-rpath=/usr/local/lib/gcc6/ -o key key.f08 getkey.o
最小的CMakeLists.txt
:
cmake_minimum_required(VERSION 3.9)
project(GetKey)
set(VERSION 1.0)
set(CMAKE_Fortran_COMPILER "gfortran6")
set(GCC_LIB "-Wl,-rpath=/usr/local/lib/gcc6")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${GCC_LIB}")
enable_language(Fortran)
add_library(getkey STATIC getkey.c)
add_executable(key key.f08)
set_target_properties(key PROPERTIES LINKER_LANGUAGE Fortran)
target_link_libraries(key getkey)
构建项目会导致以下链接器错误:
[...]
[100%] Linking Fortran executable key
/usr/lib/crt1.o: In function `_start':
/usr/src/lib/csu/amd64/crt1.c:(.text+0x17b): undefined reference to `main'
collect2: error: ld returned 1 exit status
链接器似乎在搜索一个main()
函数,Fortran没有这个功能。试图强制使用Fortran链接器没有任何区别:
set(CMAKE_Fortran_LINKER_PREFERENCE 100)
set(CMAKE_C_LINKER_PREFERENCE_PROPAGATES False)
感谢帮助。
Example Code
链接器错误不依赖于实际实现。无论如何,一个基本的例子是:
key.f08
:
program main
implicit none
interface
function get_key() bind(c, name='get_key')
use iso_c_binding
integer(kind=c_int) :: get_key
end function get_key
end interface
write(*, '(i0)') get_key()
end program main
和getkey.c
:
int get_key()
{
return 10000;
}
Verbose Make
运行make VERBOSE=1
:
/usr/local/bin/cmake -H"/home/user/fortran" -B"/home/user/fortran" --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start "/home/user/fortran/CMakeFiles" "/home/user/fortran/CMakeFiles/progress.marks"
make -f CMakeFiles/Makefile2 all
make -f CMakeFiles/key.dir/build.make CMakeFiles/key.dir/depend
cd "/home/user/fortran" && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" "/home/user/fortran" "/home/user/fortran" "/home/user/fortran" "/home/user/fortran" "/home/user/fortran/CMakeFiles/key.dir/DependInfo.cmake" --color=
make -f CMakeFiles/key.dir/build.make CMakeFiles/key.dir/build
[ 50%] Linking Fortran executable key
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/key.dir/link.txt --verbose=1
/usr/local/bin/gfortran6 CMakeFiles/key.dir/getkey.c.o -o key
/usr/lib/crt1.o: In function `_start':
/usr/src/lib/csu/amd64/crt1.c:(.text+0x17b): undefined reference to `main'
collect2: error: ld returned 1 exit status
*** Error code 1
Stop.
make[2]: stopped in /usr/home/user/fortran
*** Error code 1
Stop.
make[1]: stopped in /usr/home/user/fortran
*** Error code 1
Stop.
make: stopped in /usr/home/user/fortran
答案
正如您从详细的make输出中看到的那样:
/usr/local/bin/gfortran6 CMakeFiles/key.dir/getkey.c.o -o key
您根本没有使用Fortran代码构建!这是因为CMake不识别.f08作为代码扩展。见这里:cmake, fortran 2008, and .f08 file extension
您可以简单地将.f08
文件重命名为.f
(或.f90
,如下面的评论中所述)。
以上是关于CMake:将Fortran与静态C库链接失败的主要内容,如果未能解决你的问题,请参考以下文章