免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2645 | 回复: 8
打印 上一主题 下一主题

[C++] g++ 编译出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-06 21:55 |只看该作者 |倒序浏览
5可用积分
在学习的时候遇到的问题,请各位大大帮帮忙。
编译的哦时候出错是
singerinsky@ubuntu:~/study$ g++ h.cpp t.h
/tmp/ccaBkhXY.o: In function `MThread::MThread()':
h.cpp.text._ZN7MThreadC1Ev[MThread::MThread()]+0xd): undefined reference to `CThread::CThread()'
/tmp/ccaBkhXY.o: In function `MThread::~MThread()':
h.cpp.text._ZN7MThreadD0Ev[MThread::~MThread()]+0x16): undefined reference to `CThread::~CThread()'
/tmp/ccaBkhXY.o: In function `MThread::~MThread()':
h.cpp.text._ZN7MThreadD1Ev[MThread::~MThread()]+0x16): undefined reference to `CThread::~CThread()'
/tmp/ccaBkhXY.o.rodata._ZTI7MThread[typeinfo for MThread]+0x: undefined reference to `typeinfo for CThread'
collect2: ld returned 1 exit status

这个是 main函数的


   #include "t.h"
  #include "stdio.h"
&nbsp;&nbsp;#include <unistd.h>
&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;int main()
&nbsp;&nbsp;{
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MThread m;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;
&nbsp;&nbsp;}



这个是t.h
  1 #include "stdio.h"
&nbsp;&nbsp;2 #include <unistd.h>
&nbsp;&nbsp;3 #include "CThread.h"
&nbsp;&nbsp;4
&nbsp;&nbsp;5 class MThread:public CThread
&nbsp;&nbsp;6 {
&nbsp;&nbsp;7         public:
&nbsp;&nbsp;8                 MThread(){};
&nbsp;&nbsp;9                 virtual ~MThread(){};
&nbsp;10
&nbsp;11
&nbsp;12         protected:
&nbsp;13                 void Run()
&nbsp;14                 {
&nbsp;15                         while(1)
&nbsp;16                         {
&nbsp;17                                 printf("Thread");
&nbsp;18                                 sleep(1);
&nbsp;19                         }
&nbsp;20                 }
&nbsp;21 };



这个是 CThread.h


  1 #ifndef __G_THREAD__
  2 #define __G_THREAD__
  3
  4 #include <pthread.h>
  5 #include "common.h"
  6 #include <assert.h>
  7 #include "NoCopyable.h"
  8
  9 class CThread:private G_NoCopyable
10 {
11         public:
12                 CThread();
13                 virtual ~CThread();
14                 pthread_t getThreadId();
15                 bool Start();
16
17                 void pause();
18
19                 void continues();
20
21         private:
22                 void maskSIGUSR1();
23
24                 pthread_t m_pid;
25
26                 sigset_t m_waitSig;
27
28                 static void *threadFun(void *arg);
29         protected:
30                 virtual void Run() = 0;
31 };   
32      
33 #endif
34



这个是CThread.cpp

CThread::CThread() : m_pid(0)
&nbsp;26 {
&nbsp;27         maskSIGUSR1();
&nbsp;28         sigemptyset(&m_waitSig);
&nbsp;29         sigaddset(&m_waitSig, SIGUSR1);
&nbsp;30 }
&nbsp;31
&nbsp;32 CThread::~CThread()
&nbsp;33 {
&nbsp;34 }
&nbsp;35
&nbsp;36 void CThread::maskSIGUSR1()
&nbsp;37 {
&nbsp;38         sigset_t sig;
&nbsp;39         sigemptyset(&sig);
&nbsp;40         sigaddset(&sig , SIGUSR1);
&nbsp;41         pthread_sigmask(SIG_BLOCK , &sig , NULL);
&nbsp;42 }
&nbsp;43
&nbsp;44 pthread_t CThread::getThreadId()
&nbsp;45 {
&nbsp;46         return m_pid;
&nbsp;47 }
&nbsp;48
"CThread.cpp" 84L, 1352C   

最佳答案

查看完整内容

CThread.cpp这个文件你不编吗?

论坛徽章:
1
天蝎座
日期:2013-08-25 10:27:22
2 [报告]
发表于 2009-08-06 21:55 |只看该作者
CThread.cpp这个文件你不编吗?

论坛徽章:
0
3 [报告]
发表于 2009-08-06 22:45 |只看该作者
编译时加上-lpthread线程库看看

论坛徽章:
0
4 [报告]
发表于 2009-08-06 23:03 |只看该作者
试了下。不是那个原因。还请大大帮忙哈

论坛徽章:
0
5 [报告]
发表于 2009-08-06 23:56 |只看该作者
g++ h.cpp   CThread.cpp

论坛徽章:
5
狮子座
日期:2013-08-20 10:12:24午马
日期:2013-11-23 18:04:102015年辞旧岁徽章
日期:2015-03-03 16:54:152015亚冠之德黑兰石油
日期:2015-06-29 18:11:1115-16赛季CBA联赛之新疆
日期:2024-02-21 10:00:53
6 [报告]
发表于 2009-08-07 02:35 |只看该作者
最好是CThread.cpp编译一个.o文件,然后h.cpp另一个.o文件,最后链接,就不会有问题了。

论坛徽章:
0
7 [报告]
发表于 2009-08-07 07:43 |只看该作者
原帖由 yangsf5 于 2009-8-6 23:17 发表
CThread.cpp这个文件你不编吗?

嗯,应该就是这个文件的问题

论坛徽章:
0
8 [报告]
发表于 2009-08-07 09:53 |只看该作者
g++ Main.cpp h.cpp CThread.cpp -lpthread

论坛徽章:
0
9 [报告]
发表于 2009-08-07 10:03 |只看该作者

回复 #4 yangsf5 的帖子

恩。昨天已经搞定,就是大大说的原因。大家的意见高度统一,只好给先说的了。:)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP