尝试创建新的QTcpSocket时出现分段错误:
QTcpSocket* socket = new QTcpSocket();
我跑步时看到以下内容:
$ ./hello
Hello World
Segmentation fault (core dumped)
我在Unbuntu 17.04上使用Qt 5.11,但没有在使用qmake。我创建了自己的Makefile。我已经检查并仔细检查了Qt 5.11的包含和库,但不确定是不是问题所在,因为我可以编译,链接和运行。
Qt分布所在的区域似乎有2套,引起了一些混乱:
$(HOME)Qt5.11.1/5.11.1/gcc_64 /usr/include/x86_64-linux-gnu/qt5
这是我的Makefile和代码。
hello.h
#ifndef HELLO_H
#define HELLO_H
#include <QObject>
#include <QTcpSocket>
class hello : public QObject
{
Q_OBJECT
public:
explicit hello(QObject *parent = 0);
signals:
int test();
public slots:
private:
private slots:
};
#endif // HELLO_H
hello.cpp
#include "hello.h"
#include <QTcpSocket>
#include <QDebug>
hello::hello(QObject *parent) :
QObject(parent)
{
qDebug() << "Hello World\n";
QTcpSocket* socket = new QTcpSocket(this);
emit test();
}
main.cpp
#include <QCoreApplication>
#include "hello.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
hello h;
return a.exec();
}
制作文件
####### Compiler, tools and options
CC = g++
DEFINES = -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB
CFLAGS = -Wall -W -D_REENTRANT -fPIC $(DEFINES)
INCPATH = -I. \
-I$(HOME)/Qt5.11.1/5.11.1/gcc_64/include \
-I$(HOME)/Qt5.11.1/5.11.1/gcc_64/include/QtCore \
-I$(HOME)/Qt5.11.1/5.11.1/gcc_64/include/QtNetwork \
-I$(HOME)/Qt5.11.1/5.11.1/gcc_64/mkspecs/linux-g++
LINK = g++
LFLAGS = -Wl,-O1 -Wl,-rpath,$(HOME)/Qt5.11.1/5.11.1/gcc_64/lib
LIBS = -L$(HOME)Qt5.11.1/5.11.1/gcc_64/lib \
-L/usr/include/x86_64-linux-gnu/qt5/QtCore \
-L/usr/include/x86_64-linux-gnu/qt5/QtNetwork \
-lQt5Core -lQtNetwork
MOC = $(HOME)Qt5.11.1/5.11.1/gcc_64/bin/moc
####### Files
HEADERS = hello.h
SOURCES = hello.cpp \
main.cpp
OBJECTS = hello.o \
main.o
SRCMOC = moc_hello.cpp
OBJMOC = moc_hello.o
TARGET = hello
####### Implicit rules
.SUFFIXES: .cpp
.cpp.o:
$(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
####### Build rules
all: $(TARGET)
$(TARGET): $(OBJECTS) $(OBJMOC)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
moc: $(SRCMOC)
clean:
-rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(TARGET)
-rm -f *~ core
####### Compile
hello.o: hello.cpp \
hello.h \
main.o: main.cpp \
hello.h \
moc_hello.o: moc_hello.cpp \
hello.cpp \
moc_hello.cpp: hello.h
以下是“ ldd hello”的输出:
anshah@anshah-linux-laptop:~/test/hello$ ldd hello
linux-vdso.so.1 => (0x00007fff2bff4000)
libQt5Core.so.5 => /home/anshah/Qt5.11.1/5.11.1/gcc_64/lib/libQt5Core.so.5 (0x00007f5c4b903000)
libQtNetwork.so.4 => /usr/lib/x86_64-linux-gnu/libQtNetwork.so.4 (0x00007f5c4b582000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f5c4b1fa000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f5c4afe3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5c4ac1c000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5c4a9fc000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f5c4a7e0000)
libicui18n.so.56 => /home/anshah/Qt5.11.1/5.11.1/gcc_64/lib/libicui18n.so.56 (0x00007f5c4a347000)
libicuuc.so.56 => /home/anshah/Qt5.11.1/5.11.1/gcc_64/lib/libicuuc.so.56 (0x00007f5c49f8f000)
libicudata.so.56 => /home/anshah/Qt5.11.1/5.11.1/gcc_64/lib/libicudata.so.56 (0x00007f5c485ac000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5c483a8000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f5c481a4000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f5c47e90000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f5c47b87000)
/lib64/ld-linux-x86-64.so.2 (0x000056357a3b1000)
libQtCore.so.4 => /usr/lib/x86_64-linux-gnu/libQtCore.so.4 (0x00007f5c47695000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f5c47422000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f5c47218000)
答案 0 :(得分:1)
Qt分布所在的区域似乎有2套,引起了一些混乱:
这很可能是您崩溃的根本原因。如果您根据一个Qt版本的源文件(头文件)进行编译,然后在运行时使用不同版本,则很可能发生崩溃。
但是,根本原因似乎有所不同:您使用的是{em {em} {em}的两个版本:
libQtCore
如果这些库定义了相同的符号(这很有可能),那么由于符号冲突和/或使用了错误的符号,几乎可以保证您的经历很糟糕。
libQt5Core.so.5 => /home/anshah/Qt5.11.1/5.11.1/gcc_64/lib/libQt5Core.so.5
libQtNetwork.so.4 => /usr/lib/x86_64-linux-gnu/libQtNetwork.so.4
libQtCore.so.4 => /usr/lib/x86_64-linux-gnu/libQtCore.so.4
可能是作为libQtCore.so.4
的依赖项引入的,您可能根本不应该依赖libQtNetwork.so.4
。
您的libQtNetwork.so.4
具有:
Makefile
最后一个条目可能应该是-lQt5Core -lQtNetwork
。