免费注册 查看新帖 |

Chinaunix

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

请教一个问题:为什么在编译的时候会出现“段错误” [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-11-09 23:06 |只看该作者 |倒序浏览
我写了一个工程,包括两个类(SerialPort和EstablishPort)和一个接口文件,其中SerialPort是EstablishPort的基类,想编译成一个.so文件
但是在编译的时候,其他文件都编译过了,就是在编译SerialPort的时候总是提示段错误
输出如下:
make

/usr/local/qt/bin/moc serialport.h -o .moc/moc_serialport.cpp
make: *** [.moc/moc_serialport.cpp] 段错误 (core dumped)

我是使用qmake 生成Makefile文件的

serialport.h

#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <stdio.h>;       
#include <string.h>;       
#include <unistd.h>;       
#include <fcntl.h>;               
#include <errno.h>;       
#include <termios.h>;

#include <sys/ioctl.h>;
#include <qobject.h>;
#include <pthread.h>;
#include <semaphore.h>;

#include "constdefinition.h"

//缓冲区记录信息
struct BUFRECORD
{
        bool Empty;
        bool Full;
        int  Front;
        int  Tail;
};

class SerialPort : public QObject
{
    Q_OBJECT
public:
        SerialPort(const char& comno='a', int cardtype=1);
        ~SerialPort();
        bool initPort(const tcflag_t& baud=B9600, const uint& databit=8, const uint& parity=0, const uint& stopbit=1);
        int WriteToPort(const unsigned char * lpByte, int dwBytesToWrite );
        void StartMonitoring();
        void StopMonitoring();
        virtual void ProcessReceiveData();
        virtual void EndProcessData();
       
protected:
        static void WriteChar(SerialPort * port);
        static void ReceiveChar(SerialPort * port);
        static void*        CommThread(void* ptr);

        bool bThreadAlive;
        int ReceivePos;
        pthread_t comm_thread;
        pthread_mutex_t        write_lock;
        pthread_mutex_t        read_lock;
        sem_t        shutdown_sem;
        sem_t        write_semaphore;
        sem_t        buf_full_semaphore;
        unsigned char *        szWriteBuffer[ARRAYLEN];
        int        nBytesToWrite[ARRAYLEN];
        int nWriteBufferSize;
        BUFRECORD        BufInfo;
        unsigned char*        szReceiveBuffer;
        int        fd;
};
#endif

论坛徽章:
0
2 [报告]
发表于 2004-11-10 08:25 |只看该作者

请教一个问题:为什么在编译的时候会出现“段错误”

段错误大多是无效的内存访问引起的,检查一下指针看看

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2004-11-10 09:13 |只看该作者

请教一个问题:为什么在编译的时候会出现“段错误”

二楼的瞧好了,
人家说的是“编译”的时候,和指针没关系。

论坛徽章:
0
4 [报告]
发表于 2004-11-10 09:18 |只看该作者

请教一个问题:为什么在编译的时候会出现“段错误”

这个真的不好说啊, 我也遇到过这样的情况不过至今也不知道是咋回事,换了个源码就过去了,我想应该是编译器的支持问题吧,有一次一个同事重装系统后编译一个库老是出现段错误,可是其它机器的都没问题,于是就给他重新装了一下GCC问题解决了

论坛徽章:
0
5 [报告]
发表于 2004-11-10 10:47 |只看该作者

请教一个问题:为什么在编译的时候会出现“段错误”

也许是你的Qt的问题,moc的程序出的错误,
你使的Qt是什么版本?

论坛徽章:
0
6 [报告]
发表于 2004-11-10 21:23 |只看该作者

请教一个问题:为什么在编译的时候会出现“段错误”

我用的qt是3.0.2

我尝试用gcc分别编译各文件时,没有出现错误,编译过程如下
gcc -Wall -I/usr/local/qt/include -c serialport.h serialport.cpp
gcc:Compilation of header file requested

gcc -Wall -I/usr/local/qt/include -c establishserialport.h establishserialport.cpp
gcc:Compilation of header file requested
establishserialport.cpp:120:2:warning:no newline at end of file

gcc -Wall -I/usr/local/qt/include -c interface.cpp
interface.cpp:40:1:warning:no newline at end of file

gcc -shared -o libwe.so interface.o establishserialport.o serialport.o

我做了一个测试程序
如果以这种方式编译:gcc -o testComm testComm.cpp -ldl
在load的时候出错,我将出错原因显示出来
./testComm
handle=0
/lib/libwe.so: undefined symbol: __7QObjectP7QObjectPC

如果以这种方式编译:gcc -o testComm testComm.cpp -ldl -L/usr/local/qt/lib -lqt
出错原因是
./testComm
handle=0
/lib/libwe.so: undefined symbol: __vt_10SerialPort


不只是什么原因,请高手帮忙分析一下,多谢多谢!!

论坛徽章:
0
7 [报告]
发表于 2004-11-10 21:52 |只看该作者

请教一个问题:为什么在编译的时候会出现“段错误”

我以前遇到过类似的问题
最后发现还是错在代码上
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP