本文介绍了对类 ERROR 的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c++/ubuntu 中工作.我有:

I am working in c++ /ubuntu.I have:

#ifndef LIBR
#define LIBR


#include <string>
using namespace std;

class name
{
    public:
    name();
    ~name();
    std::string my_name;
    std::string method (std::string s);

};


#endif

#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;


name::name()
{

}
std::string name::method(std::string s)
{
    return ("YOUR NAME IS: "+s);
}

从这两个我创建了一个 libr.a.

From these two I've created a libr.a.

在 test.cpp 中:

In test.cpp:

#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"

using namespace std;

int main()
{

    name *n = new name();
    n->my_name="jack";
    cout<<n->method(n->my_name)<<endl;
    return 0;
}

我用 g++ 和 libr.a 编译.我有一个错误:name::name() undefined reference",为什么?

I compile with g++ and libr.a. I have an error: "name::name() undefined reference", why?

我想提一下,我在 qmake 的 qt creator 中添加了 .a.当我编译时,我有错误.我该如何解决?

I would like to mention that I've added in qt creator at qmake the .a. When I compile, I have the error. How can I solve it?

推荐答案

您用来编译项目"的步骤是什么?

What are the steps you're using to compile your "project"?

我执行了以下步骤并设法使用警告/错误构建它.

I performed the following steps and managed to build it with warnings/errors.

g++ -Wall -c libr.cpp

ar -cvq libr.a libr.o

g++ -Wall -o libr main.cpp libr.a

最后一件事,如果我改变了最后一个命令的顺序,比如

One last thing, if I change the order off the last command, like

g++ -Wall -o libr libr.a main.cpp

我收到以下错误:

Undefined                       first referenced
 symbol                             in file
name::name()                        /tmp/cc4Ro1ZM.o
name::method(std::basic_string<char, std::char_traits<char>, std::allocator<char
> >)/tmp/cc4Ro1ZM.o
ld: fatal: Symbol referencing errors. No output written to libr
collect2: ld returned 1 exit status

这篇关于对类 ERROR 的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:52