加速 C++:体系结构的未定义符号 x86_64
Accelerated C++: Undefined symbols for architecture x86_64
我正在研究 Accelerated C++,但我无法编译第 4 章中的程序;这是 headers 的第一个。我还从 here 下载了源代码,并得到了与我在 hand-copied 代码中看到的完全相同的错误,所以我不确定问题出在哪里。
我在 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 有点神秘的评论所暗示的那样,这很可能是您的第一个包含多个源文件的程序。您不能只编译一个文件并获得可执行文件;您必须将每个文件和 link 生成的目标文件一起编译。最简单的方法是这样的:
g++ main.cpp grade.cpp student_info.cpp median.cpp
如果代码没有错误,将在当前目录中生成一个名为 "a.out" 的可执行文件。
编辑:刚刚注意到错误消息来自 "clang",而不是来自“g++”。没关系;如果您使用的是 "clang",只需将命令行更改为
clang++ main.cpp grade.cpp student_info.cpp median.cpp
我正在研究 Accelerated C++,但我无法编译第 4 章中的程序;这是 headers 的第一个。我还从 here 下载了源代码,并得到了与我在 hand-copied 代码中看到的完全相同的错误,所以我不确定问题出在哪里。
我在 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 有点神秘的评论所暗示的那样,这很可能是您的第一个包含多个源文件的程序。您不能只编译一个文件并获得可执行文件;您必须将每个文件和 link 生成的目标文件一起编译。最简单的方法是这样的:
g++ main.cpp grade.cpp student_info.cpp median.cpp
如果代码没有错误,将在当前目录中生成一个名为 "a.out" 的可执行文件。
编辑:刚刚注意到错误消息来自 "clang",而不是来自“g++”。没关系;如果您使用的是 "clang",只需将命令行更改为
clang++ main.cpp grade.cpp student_info.cpp median.cpp