如何将C++17 stdc++fs库链接到混合语言(C++和Fortran)CMake项目
How to link C++17 stdc++fs library to mixed language (C++ and Fortran) CMake project?
我正试图用CMake将一些Fortran代码添加到我的C++项目中。该项目使用C++17文件系统库没有问题,直到我将这个Fortran文件添加到可执行文件中,这似乎破坏了链接器。我已经将这个问题简化为一个最小的例子,除了产生错误之外,没有其他功能。
原始CMakeLists.txt(编译良好(:
cmake_minimum_required( VERSION 3.10.2 )
enable_language( Fortran )
project( Minimal C CXX Fortran )
# Using C++17.
set( CMAKE_CXX_STANDARD 17 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
add_executable( Executable test.cpp )
target_link_libraries( Executable stdc++fs )
修改了CMakeLists.txt(添加了test.f90,不再编译(:
cmake_minimum_required( VERSION 3.10.2 )
enable_language( Fortran )
project( Minimal C CXX Fortran )
# Using C++17.
set( CMAKE_CXX_STANDARD 17 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
add_executable( Executable test.cpp test.f90 )
target_link_libraries( Executable stdc++fs )
后者编译/链接失败,出现以下错误(与前者省略target_link_libraries
行时的错误相同(:
CMakeFiles/Minimal.dir/test.cpp.o: In function `std::filesystem::__cxx11::path::path<char [5], std::filesystem::__cxx11::path>(char const (&) [5], std::filesystem::__cxx11::path::format)':
test.cpp:(.text._ZNSt10filesystem7__cxx114pathC2IA5_cS1_EERKT_NS1_6formatE[_ZNSt10filesystem7__cxx114pathC5IA5_cS1_EERKT_NS1_6formatE]+0x6d): undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
CMakeFiles/Minimal.dir/build.make:120: recipe for target 'Minimal' failed
make[2]: *** [Minimal] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Minimal.dir/all' failed
make[1]: *** [CMakeFiles/Minimal.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
为什么会发生这种情况,我该如何解决?我试过set_property( TARGET Executable PROPERTY LINK_LANGUAGE Fortran )
,但没用。
add_library( TestLibrary test.f90 )
...
target_link_libraries( Executable TestLibrary )
也不起作用。非常感谢任何指导。
完整参考:
test.cpp:
#include <filesystem>
int main()
{
std::filesystem::path test ("test");
return EXIT_SUCCESS;
}
测试f90:
subroutine do_nothing()
end
命令行调用:
cmake -DCMAKE_CXX_COMPILER=g++-8 ..
make
我的GCC版本是8.3.0。
这很可能是不同编译器版本混合的结果。您只覆盖C++编译器,而且C和Fortran编译器也应该与之一致。当我使用GCC 8.2中的gcc
和gfortran
,但使用GCC 9.2中的g++
(使用CMake 3.10.2(时,我能够在链接过程中收到类似的错误消息。
这是因为CMake从C编译器获得了一些重要的路径(比如查找运行库的默认目录(。但是,如果您使用不同的C++编译器编译C++代码,它会使用后者的头,从而导致不兼容。
您应该始终使用一致的编译器集;在这种情况下:
cmake
-D CMAKE_C_COMPILER=gcc-8
-D CMAKE_CXX_COMPILER=g++-8
-D CMAKE_Fortran_COMPILER=gfortran-8
..
相关文章:
- 使用 VS2015 在 Windows 10 上构建 Fortran .lib x64 位并将其链接到 C++
- 将 OR 逻辑运算符从 C++ 转换为 Fortran
- Fortran 和 C++ 的混合编程:Fortran 不能调用C++子程序
- 如何在C++中获取真实值精度 与Fortran相同(Pararel Studio XE编译器)
- 读取 C++ 中的 Fortran 数据类型
- 重新编译我们的FORTRAN代码并在C++中使用后,我们的system()或C_str()命令无法正常工作
- 使用C++和fortran构建R包,Mac
- 如何将C++17 stdc++fs库链接到混合语言(C++和Fortran)CMake项目
- 混合编程 - 包括C++标头到 Fortran
- 编译 Fortran 和C++程序并从英特尔编译器链接
- 与 MSVC 一起使用的 MinGW 的 Fortran 模块导致未解析的符号"__exmod_MOD_value"
- 将指向 1D Fortran 数组的指针传递给使用 C_LOC() 进行C++会导致垃圾值
- 将分配给C++数组传递给 Fortran 子例程
- 在 Fortran 中泛化特定声明类型的操作
- CPP -D 选项,用于预处理 Fortran 代码
- 通过矢量下标访问 Fortran 数组,相当于 cpp
- 如何将矢量从C 传递到Fortran
- 将字符串从 Fortran 传递到 c++
- 是否可以从C 调用FORTRAN接口
- C /FORTRAN项目由Visual Studio 2010/Intel 2013创建,将不会在Visual St