我试图用我的Fortran 90程序链接一个C++文件,但是在使用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::allocator::basic_string(std::basic_string,std::basic_string::分配程序>&)”,引用自cppfile.o中的std::basic_string,std::allocator > std::operator+,std::allocator >( const*,std::basic_string,std::allocator >&&) std::basic_string,std::std::operator+> std::operator+,std::allocator >(std::basic_string,std::allocator >&&,const*) std::basic_string,std::std::operator+ >std::operator+::allocator >(std::basic_string,std::allocator >&&,std::basic_string,std::allocator >&&) std::basic_string,std::std::operator+ >std::operator+::allocator >(std::basic_string,std::allocator >&&,std::basic_string,std::allocator > const&) 组件: cppfile.o中的组件(component&&) "std::basic_string,std::allocator >::operator=(std::basic_string,std::allocator >&)“,引用如下: 函数在cppfile.o中 Component::operator=(component&)在cppfile.ld:符号(S)没有找到架构x86_64 collect2:错误: ld返回1退出状态 制作:*程序错误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;
}对为什么会发生这种事有什么想法吗?我确保链接了-lstdc++库。这与我使用与字符串库相关的C++11标准有关吗?
发布于 2012-12-14 19:51:09
一个解决方案似乎是从编译-std=c++11代码中删除C++。使用它,将C++代码与字符串(或包含字符串的结构)的向量相结合会导致上述错误。
我将在GCC Bugzilla中提交一个bug,并在此期间使用我的解决方案作为解决方案。
https://stackoverflow.com/questions/13882765
复制相似问题