对具有实现的函数的未定义引用
undefined reference to functions that have implementation
我正在使用带有标头和 cpp 文件的线程类。 当我把它们都放在空测试文件时,它写道:
g++ -g -pedantic -ansi -Wall -Werror -std=c++03 -I../include -c -o test.o test.cpp
g++ -g test.o thread.o -o test
thread.o: In function `Thread::~Thread()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:15: undefined reference to `pthread_detach'
thread.o: In function `Thread::start()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:40: undefined reference to `pthread_create'
thread.o: In function `Thread::join()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:49: undefined reference to `pthread_join'
thread.o: In function `Thread::cancel()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:58: undefined reference to `pthread_cancel'
thread.o: In function `Thread::detach()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:66: undefined reference to `pthread_detach'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'test' failed
make: *** [test] Error 1
我只是试图将Thread.h和Thread组合在一起.cpp
//Thread.h looks like this:
#ifndef THREAD_H
#define THREAD_H
#include <cstddef>
#include <pthread.h>
#include <string>
class Thread
{
public:
Thread(size_t a_userID = 0);
virtual ~Thread();
bool start();
void join();
void cancel();
void detach();
private:
static void* threadMainFunction(void *);
virtual void run() = 0;
bool isAlive(std::string a_msg);
private:
bool m_joinable;
protected:
pthread_t m_threadID;
size_t m_userID;
};
#endif
//Thread.cpp looks like this:
#include <exception>
#include "Thread.h"
#include <iostream>
Thread::Thread(size_t a_userID)
: m_joinable(true)
, m_threadID(0)
, m_userID(a_userID)
{
}
Thread::~Thread()
{
if(m_joinable)
{
pthread_detach(m_threadID);
}
}
void* Thread::threadMainFunction(void *a_thread)
{
Thread* thread = reinterpret_cast<Thread*>(a_thread);
try
{
thread->run();
}
catch(const std::exception& e)
{
std::cout<<"what exepctionn";
std::cerr << e.what() << 'n';
}
catch(...)
{
throw;
}
return 0;
}
bool Thread::start()
{
int r = pthread_create(&m_threadID, 0, threadMainFunction, this);
return r == 0;
}
void Thread::join()
{
if(isAlive("Thread::join on thread not started"))
{
void *status;
pthread_join(m_threadID, &status);
m_joinable = false;
}
}
void Thread::cancel()
{
if(isAlive("Thread::cancel on thread not started"))
{
pthread_cancel(m_threadID);
}
}
void Thread::detach()
{
if(isAlive("Thread::detach on thread not started"))
{
pthread_detach(m_threadID);
}
}
bool Thread::isAlive(std::string a_msg)
{
if(m_threadID == 0)
{
throw(std::runtime_error(a_msg));
return false;
}
return true;
}
您在这里面临的问题不是构建问题,而是链接问题。当构建thread.o
时,编译器知道pthread_create
存在,并在某处定义,因为在pthread.h
头中声明。
如果您使用nm
查看thread.o
中使用的符号,您将看到类似以下内容的内容:
U _pthread_create
U _pthread_detach
...
这告诉您thread.o
引用多个Undefined 符号,包括pthread_create
。换句话说,此时pthread_create
的机器代码是未知的。在您需要将目标文件链接到可执行文件(链接器的角色(之前,这完全没问题。
在此阶段,必须告知链接器在哪里可以找到这些未定义的符号,可能来自另一个对象文件或静态/共享库。对于pthread
,符号在libpthread
中定义,你可以在系统目录中找到libpthread.a
。您可以通过添加-lpthread
来告诉g++
链接此库(请注意,使用-l
时省略了libpthread
中的lib
(:
g++ -g test.o thread.o -o test -lpthread
通常,如果使用静态库中引用的符号lib${LIBNAME}.a
${LIBDIR}
目录中可用的符号,则可以告诉链接器将其用于:
g++ -g *.o -L$LIBDIR -l${LIBNAME}
相关文章:
- 区分接受常量参数的函数引用/指针和与函数参数同名的非常量参数
- 类 Referention 中C++回调函数引用非静态函数
- 使用函数引用指向节点的指针删除链表中的节点?
- 解释通过从函数引用返回数组的语法
- "Class1"类"Class2"对象作为私有数据成员。如何通过"Class 2"函数引用"Class1"对象?
- 使用默认构造函数引用成员变量初始化错误
- 无法调用函数引用 c++
- 使用 decltype(this) 获取函数引用
- 我应该如何定义返回指针的函数?(引用指针与指针指针)
- 从内联函数引用文件静态变量
- Boost::将sigaction函数引用绑定到实例
- 一种比函数引用更有效的方法
- 奇怪的未定义函数引用,函数调用C++不存在
- 是否可以检测绑定成员函数引用的对象是否被删除或销毁
- 构造函数引用参数导致seg错误
- 是否可以使函数模板从函数引用中获取“decltype”
- 无法让 Lua 函数引用"self"
- 如何解析变量和函数引用(Linker & Compiler)?
- 从不同模块调用函数 - 引用错误
- 如何在主函数中连接到数据库,然后从其他函数引用它