0

我试图让一个 cmake 构建系统在 linux 上运行。该项目包含一堆可执行文件和两个库。其中一个可执行文件首先构建为库,然后将该库与包含 man 子例程的目标文件链接。这样做是因为其余的可执行文件依赖于该库。棘手的部分是主子例程是在其余源依赖的模块中定义的,因此需要在其余源之前编译它。效果是将主子例程添加到生成的库中。这似乎在 Mac OS X 上运行良好,但在 Linux 上链接状态失败。

失败部分的 cmake 文件看起来像

cmake_minimum_required (VERSION 2.8)

# Create an empty variable to hold all the source files.
set (vmec_sources "")

# Add subdirectory for all the sources.
add_subdirectory (Sources)

add_library (vmec STATIC ${vmec_sources})
add_dependencies (vmec stell)

# Define an executable and link all libraries.
add_executable (xvmec ${CMAKE_CURRENT_SOURCE_DIR}/Sources/General/vmec_main.f)
add_dependencies (xvmec vmec)
target_link_libraries (xvmec vmec stell)

if ((NOT ${NETCDF_C} STREQUAL "") AND (NOT ${NETCDF_F} STREQUAL ""))
    target_link_libraries (xvmec ${NETCDF_C} ${NETCDF_F})
endif ()

运行 cmake 时,一切都配置良好,并在我运行 make Mac OS X 时生成一个 make 文件,一切正常。当我在 Linux 上运行 make 时,它​​失败了。

make VERBOSE=1On Linux的输出产生

Linking Fortran executable ../build/bin/xvmec
cd /home/user/reconstruction/VMEC2000 && /usr/bin/cmake -E cmake_link_script CMakeFiles/xvmec.dir/link.txt --verbose=1
/usr/bin/gfortran      -cpp -D NETCDF -I /usr/include CMakeFiles/xvmec.dir/Sources/General/vmec_main.f.o  -o ../build/bin/xvmec -rdynamic ../build/lib/libvmec.a ../build/lib/libstell.a /usr/lib/libnetcdf.so /usr/lib/libnetcdff.so 
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/libgfortranbegin.a(fmain.o): In function `main':
(.text+0x26): undefined reference to `MAIN__'
collect2: ld returned 1 exit status
make[2]: *** [build/bin/xvmec] Error 1
make[2]: Leaving directory `/home/user/reconstruction'
make[1]: *** [VMEC2000/CMakeFiles/xvmec.dir/all] Error 2
make[1]: Leaving directory `/home/user/reconstruction'
make: *** [all] Error 2

在 Mac OS X 上,我得到

Linking Fortran executable ../build/bin/xvmec
cd /Users/user/repo/trunk/VMEC2000 && "/Applications/CMake 2.8-8.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/xvmec.dir/link.txt --verbose=1
/usr/local/bin/gfortran     -framework Accelerate   -cpp -D DARWIN -D NETCDF -I /Users/user/NetCDF/include -O3 -ftree-vectorize -m64 -march=native -fomit-frame-pointer -falign-functions -mfpmath=sse CMakeFiles/xvmec.dir/Sources/General/vmec_main.f.o  -o ../build/bin/xvmec  ../build/lib/libvmec.a ../build/lib/libstell.a /Users/user/NetCDF/lib/libnetcdf.dylib /Users/user/NetCDF/lib/libnetcdff.dylib 
"/Applications/CMake 2.8-8.app/Contents/bin/cmake" -E cmake_progress_report /Users/user/repo/trunk/CMakeFiles  100
[100%] Built target xvmec

链接行看起来像是以正确的顺序链接所有相同的东西,所以我不明白为什么这在 Linux 上会失败。

4

1 回答 1

0

原来我列出了包含主要方法的错误文件。似乎更高版本的 gfortran 可以从库内部链接“MAIN__”,而 gfortran-4.4 不能。

于 2012-07-18T21:46:23.137 回答