【问题标题】:C++ linker error OSX MavericksC++ 链接器错误 OSX Mavericks
【发布时间】:2014-04-04 17:49:45
【问题描述】:

我在使用 g++ 链接器时遇到问题。我的代码如下:

prog3.cpp

#include "linkedlist.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

bool readFile(const char* file, linkedlist &l);

int main(int argc, char **argv)
{
int i = 0, j = 0; //Incrementers
linkedlist dictionary;
string word;
ifstream fin;

if(argc != 3)
{
    //give the user an error and exit
}

if(!readFile("dictionary_short.txt", dictionary))
{
    //give the user an error and exit
}
dictionary.printToCommandline();

return 0;
}

bool readFile(const char* file, linkedlist &l)
{
string tmp;

ifstream fin;
fin.open(file, ios::in);
if(fin.is_open())
{
    //Read the file
    while(!fin.eof())
    {
        fin >> tmp;
        l.insert(tmp);
    }

    //close the file and return the array
    fin.close();

    return true;
}
return false;
}

链表.h

class linkedlist
{
public:
    /*!
    * @brief Constructor for the linked list
    */
    linkedlist();
    /*!
     * @brief Deconstructor for the linked list
    */
    ~linkedlist();

    /*!
    * @brief inserts a word into the linked list. Returns true if sucessful.
    */
    bool insert( string str );
    /*!
    * @brief removes a word from the linked list. Returns true if successful
    */
    bool remove( string str );
    /*!
    * @brief finds a word in the linked list. Returns true if sucessful
    */
    bool find( string str );
    /*!
    * @brief Increases the frequency count of a word. Returns true if sucessful
    */
    bool incrementFrequency( string str );
    /*!
    * @brief Returns true if a node is empty.
    */
    bool isEmpty();
    /*!
    * @brief Returns the manimum frequency of a word from the linked list.
    */
    int getMaxFrequency();
    /*!
     * @brief Returns the number of nodes in the list.
    */
    int size();
    /*!
    * @brief Outputs the list into a file formatted correctly.
    */
    void printToFile(ostream &out); 
    /*!
    * @brief Outputs the list to the command line in alphabetical order
    */
    void printToCommandline();

private:
    /*!
     * @brief Structure for a node. Keeps track of the frequency for the word.
     * Stores the word. Also keeps a pointer to the next word in the list.
    */
    struct node
    {
        int frequencyCount;
        string word;
        node *next;
    };
    node *headptr;
};

链接列表.cpp

#include "linkedlist.h"


linkedlist::linkedlist()
{
headptr = nullptr;
}

linkedlist::~linkedlist()
{
    //Function Implementation
}

bool linkedlist::insert( string str )
{
    //Function Implementation
}

bool linkedlist::remove( string str )
{
    //Function Implementation
}

bool linkedlist::find( string str )
{
    //Function Implementation
}

bool linkedlist::incrementFrequency( string str )
{
    //Function Implementation
}

bool linkedlist::isEmpty()
{
    //Function Implementation
}

int linkedlist::getMaxFrequency()
{
    //Function Implementation
}

int linkedlist::size()
{
    //Function Implementation
}

void linkedlist::printToFile( ostream &out )
{
    //Function Implementation
}

void linkedlist::printToCommandline()
{
    //Function Implementation
}

我在编译时遇到的错误(在 Mac OS X 10.9.2 上使用 g++)如下:

Undefined symbols for architecture x86_64:
"linkedlist::printToCommandline()", referenced from:
_main in prog3-717fc0.o
"linkedlist::insert(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
    readFile(char const*, linkedlist&) in prog3-717fc0.o
"linkedlist::linkedlist()", referenced from:
    _main in prog3-717fc0.o
"linkedlist::~linkedlist()", referenced from:
    _main in prog3-717fc0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]

出于好奇,我将文件复制到 Xcode 项目中,但没有收到任何错误。如何解决此链接器问题?

【问题讨论】:

    标签: c++ xcode macos linker g++


    【解决方案1】:

    你需要将linkedlist.o传递给链接器

    g++ -c prog3.cpp
    g++ -c linkedlist.cpp
    g++ prog3.o linkedlist.o -o prog3
    

    或在单个命令中:

    g++ prog3.cpp linkedlist.cpp -o prog3
    

    【讨论】:

    • 谢谢!那行得通。有什么办法可以让 g++ 在一个命令中为我执行此操作吗?
    猜你喜欢
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    相关资源
    最近更新 更多