【问题标题】:Xcode linker error: linker command failed with exit code 1 (use -v to see invocation)Xcode 链接器错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
【发布时间】:2019-01-22 22:20:41
【问题描述】:

使用 C++ 和 Xcode 我总是遇到链接器命令失败的问题。我不知道这是什么意思。我正在创建一个包含 3 个文件的基本程序。一个 main.cpp 和一个带有头文件的类文件。主文件为空。其他两个是这样的:

student.hpp:

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <sstream>

std::string line;
class Student{
public:
    std::string fullName;
    int projectGrade;
    int quizGrade;
    int midtermGrade;
    int finalGrade;
    int finish;
    int start = 14;

    std::string getStudentName();
    int getProjectGrade();
    int getQuizGrade();
    int getMidtermGrade();
    int getFinalGrade();
    double getOverallGrade();
    bool login(std::string username, std::string password);
    bool login(std::string username);
};

student.cpp

#include "student.hpp"

std::string Student::getStudentName(){
    finish = line.find("\t", start);
    fullName = line.substr(start, finish);
    return fullName;
}
int Student::getProjectGrade(){
    start = line.find("\t", start) + finish;
    finish = start + 2;
    std::stringstream stream(line.substr(start, 2));
    stream >> projectGrade;
    return projectGrade;
}
int Student::getQuizGrade(){
    start+=3;
    std::stringstream stream(line.substr(start, 2));
    stream >> quizGrade;
    return quizGrade;
}
int Student::getMidtermGrade(){
    start+=3;
    std::stringstream stream(line.substr(start, 2));
    stream >> midtermGrade;
    return midtermGrade;
}
int Student::getFinalGrade(){
    start+=3;
    std::stringstream stream(line.substr(start, 2));
    stream >> finalGrade;
    return finalGrade;
}
double Student::getOverallGrade(){
    return round((projectGrade + quizGrade + midtermGrade + finalGrade)/40)*10;
}
bool Student::login(std::string username, std::string password){
    std::ifstream studentFile;
    studentFile.open("/Users/griffin/desktop/Data Structures/Data Structures1/Data Structures1/Students.txt");
    if(studentFile.is_open()){
        while(getline (studentFile,line))
        {
            if ((username.length() == 7 && username.substr(0,4).compare("u000") == 0 && line.find(username) != std::string::npos) && (password.length() == 6 && password.substr(0,2).compare("pw") == 0 && line.find(password) != std::string::npos))
                return true;
        }
    }
    return false;
}
bool Student::login(std::string username){

    std::ifstream studentFile;
    studentFile.open("/Users/griffin/desktop/Data Structures/Data Structures1/Data Structures1/Students.txt");
    if(studentFile.is_open()){
        while(getline (studentFile,line))
        {
            if (username.length() == 7 && username.substr(0,4).compare("u000") == 0 && line.find(username) != std::string::npos)
                return true;
        }
    }
    return false;
}

主文件包括student.hpp。这是 Xcode 端的问题还是我的代码有错误?

Main.cpp

#include "student.hpp"    
int main(int argc, const char * argv[]) {
    return 0;
}

【问题讨论】:

  • 主文件应该包括student.hpp,而不是student.cpp。那是错字吗?你能发布完整的错误信息吗?
  • 还有包含 student.cpp 的 main.cpp 会导致多个定义错误。所有标准副本。
  • Spiller 打错了,我已经更新了代码以包含主要功能
  • @Griffin,现在错误信息怎么样?
  • "链接器命令失败,退出代码为 1(使用 -v 查看调用)"

标签: c++ xcode linker-errors


【解决方案1】:

你的问题是你已经多次声明了全局变量line

您的代码中没有充分的理由使用全局变量,因此简单的解决方案是在两个登录方法中声明 line

为了将来参考,这里是你应该如何声明全局变量

How do I use extern to share variables between source files?

【讨论】:

  • std::string 行;仅在 student.hpp 中声明一次,即使我也在 student.cpp 中使用 line 变量
  • @Griffen,但 student.hpp 在 student.cpp 和 main.cpp 中包含 两次,因此 line 被声明两次,导致链接器错误。请参阅我的链接以了解如何正确执行全局变量。
猜你喜欢
  • 2019-09-28
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2023-03-28
  • 2016-01-04
  • 2016-04-15
相关资源
最近更新 更多