使用gfortran链接c++时std::basic_string的未定义符号

Undefined symbols for std::basic_string when linking c++ using gfortran?

本文关键字:string 未定义 符号 basic 使用 链接 c++ std gfortran      更新时间:2023-10-16

我正试图将C++文件与我的Fortran 90程序链接,但在使用gfortran时遇到了链接错误。

我的Fortran文件使用:
gfortran -c -o obj/file.o file.f90 -O0 -g3 -ffree-line-length-none -fcheck-array-temporaries -fbounds-check编译,C++文件使用g++-4.7 -c -o obj/cppfile.o cppfile.cpp -O0 -g3 -std=c++11编译

然后,所有这些都与gfortran链接在一起:
gfortran -o program obj/file.o obj/cppfile.o -O0 -g3 -ffree-line-length-none -fcheck-array-temporaries -fbounds-check -lm -llapack -lc -lgfortran -lgcc -lstdc++

当这样做时,我得到以下链接错误:

体系结构x86_64的未定义符号:
"std::basic_string,std::allocater::basic_string(std::basic_string,std::allocater>&)",引用自:

cppfile中的std::basic_string,std::allocater>std::operator+,std:::allocator>(char const*,std:,basic_string;std::分配器>&&),cppfile中的std::allocater>(std::basic_string,std::分配器>&,std::basic_string,std:分配器>&),cppfile中的std::allocator>(std::basic_string,std::分配器>&,std::basic_string,std::分配器>const&)。o
cppfle中的component::component(component&&在cppfile.o 中

ld:未找到体系结构x86_64的符号
collect2:error:ld返回1退出状态
make:*[program]错误1

我的c++文件如下所示:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
struct component
{
string num;     // numerator of component
string den;     // denominator of component
int ind;        // index of variable
};
extern "C"{
void subroutine_ (int num, const int* array)
{
...
return;
}

有没有想过为什么会发生这种事?我确保链接-lsdc++库。这可能与我使用与字符串库相关的C++11标准有关吗?

一个解决方案似乎是从C++代码的编译中删除-std=c++11。有了它,将链接C++代码与字符串向量(或包含字符串的结构)相结合会导致上述错误。

我将在GCC Bugzilla中对此提交一个错误,并在此期间使用我的解决方案作为变通方法。

相关文章: