加速的C++:体系结构x86_64的未定义符号
问题描述:
我正在通过加速C++,我无法从第4章获得程序编译;这是第一个标题。我也从here下载了源代码,并得到了与手抄代码完全相同的错误,所以我不确定问题所在。加速的C++:体系结构x86_64的未定义符号
我在OSX El Capitan上使用g ++。下面是我用的确切命令:
$ g++ main.cpp
这里的错误:
Undefined symbols for architecture x86_64:
"read(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Student_info&)", referenced from:
_main in grades-main-e0475a.o
"grade(Student_info const&)", referenced from:
_main in grades-main-e0475a.o
"compare(Student_info const&, Student_info const&)", referenced from:
_main in grades-main-e0475a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
而这里的代码:
的main.cpp
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"
using namespace std;
int main() {
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0; // length of the longest name
while (read(cin, record)) {
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare);
for (vector<Student_info>::size_type i = 0; i != students.size(); i++) {
cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' ');
try {
double final_grade = grade(students[i]);
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec);
} catch (domain_error e) {
cout << e.what();
}
cout << endl;
}
return 0; // success
}
grade.h
#ifndef GUARD_grade_h
#define GUARD_grade_h
#include <vector>
#include "Student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);
#endif
grade.cpp
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using namespace std;
double grade(double midterm, double final, double homework) {
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
double grade(double midterm, double final, const vector<double>& hw) {
if (hw.size() == 0) throw doman_error("student has done no homework");
return grade(midterm, final, median(hw));
}
double grade(const Student_info& s) {
return grade(s.midterm, s.final, s.homework);
}
Student_info.h
#ifndef GUARD_Student_info
#define GUARD_Student_info
#include <iostream>
#include <string>
#include <vector>
struct Student_info {
std::string name;
double midterm, final;
std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif
Student_info.cpp
#include "Student_info.h"
using namespace std;
bool compare(const Student_info& x, const Student_info& y) {
return x.name < y.name;
}
istream& read(istream& is, Student_info& s) {
is >> s.name >> s.midterm >> s.final;
read_hw(is, s.homework);
return is;
}
istream& read_hw(istream& in, vector<double>& hw) {
if (in) {
hw.clear();
double x;
while (in >> x) {
hw.push_back(x);
}
in.clear();
}
return in;
}
median.h
#ifndef GUARD_median_h
#define GUARD_median_h
#include <vector>
double median(std::vector<double>);
#endif
median.cpp
#include <algorithm>
#include <stdexcept>
#include <vector>
using namespace std;
double median(vector<double> vec) {
typedef vector<double>::size_type vec_sz;
vec_sz size = vec.size();
if (size == 0) throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1])/2 : vec[mid];
}
非常感谢您的帮助,任何人!
答
正如@ gudok的有点神秘的评论所暗示的,这很可能是您的第一个带有多个源文件的程序。你不能只编译一个文件并获得可执行文件;您必须编译每个文件并将生成的目标文件链接在一起。要做到这一点最简单的方法是这样的:
g++ main.cpp grade.cpp student_info.cpp median.cpp
如果在代码中没有错误,将产生在当前目录下一个名为“a.out的”可执行文件。
编辑:只注意到该错误消息来自“铛”,而不是“G ++”这没关系;如果你使用。‘铛’,只是改变了命令行
clang++ main.cpp grade.cpp student_info.cpp median.cpp
+0
谢谢,这正是我所需要的,除了我用'g ++',而不是'clang ++ '。如上所示,这是我的第一个C++程序,带有多个源文件,所以我只是不知道编译它的正确命令。 – AutonomousApps
显示命令你用来编译上面的代码 – gudok
如果你正在使用g ++,为什么前面加上'clang:'? – kfsone
@gudok done。我用'g ++ main.cpp' @kfsone,我没有知道错误为什么以“clang”作为前缀。我是一名Java开发人员,自20年前我进入高中以来,我还没有编译C++代码 - 我相信当时我们使用了Borland。 – AutonomousApps