• 13265阅读
  • 2回复

在QT应用程序中如何使用pthread线程?等待高手 [复制链接]

上一主题 下一主题
离线zl198183
 

只看楼主 正序阅读 楼主  发表于: 2008-03-28
在QT应用程序中如何使用pthread线程?等待高手
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
QT-x11-3.3.4
Redhat9
现有一QT GUI ,调用pthread线程,
使用qmake生成makefile文件后编译,错误如下:
thr.o(.text+0x14): In function `thr(int)':
: undefined reference to `pthread_create'
thr.o(.text+0x3c): In function `thr(int)':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
make: *** [44] Error 1
thr.cpp代码如下:
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <sys/io.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/types.h>
  10. #include <termios.h>
  11. #include <sys/time.h>
  12. #include <errno.h>
  13. /* Main program */
  14.     void *thread_send(void *arg);
  15.     void *thread_result;
  16.     char buf[250];
  17.     int ressend,resrcv;   
  18. int thr(int argc){
  19.     pthread_t th;
  20.     ressend=pthread_create(&th,NULL,thread_send,NULL);
  21.    
  22.     if(ressend!=0){
  23.             perror("Thread filed");
  24.             printf("send thread filed\n");
  25.             exit(EXIT_FAILURE);
  26.     }
  27.     printf("thread start\n");
  28.     ressend=pthread_join(th,&thread_result);
  29.     if(ressend!=0){
  30.             perror("Thread filed");
  31.             printf("send thread filed\n");
  32.             exit(EXIT_FAILURE);
  33.     }
  34.     exit(EXIT_SUCCESS);
  35. }
  36. void *thread_send(void *arg){
  37.    
  38.     printf("ok\n");
  39.     pthread_exit(NULL);
  40.     }

  1. #include <qapplication.h>
  2. #include "form1.h"
  3. int sh( int argc, char ** argv )
  4. {
  5.     QApplication a( argc, argv );
  6.     Form1 w;
  7.     w.show();
  8.     a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
  9.     thr();
  10.     return a.exec();
  11. }
离线chocolate
只看该作者 2楼 发表于: 2008-05-04
LIBS    = $(SUBLIBS) -L$(QTDIR)/lib -L/usr/X11R6/lib -lqt -lXext -lX11 -lm  -lpthread
离线zl198183

只看该作者 1楼 发表于: 2008-03-28
makefile如下:
  1. #############################################################################
  2. # Makefile for building: 44
  3. # Generated by qmake (1.07a) (Qt 3.3.4) on: Fri Mar 28 09:32:57 2008
  4. # Project:  44.pro
  5. # Template: app
  6. # Command: $(QMAKE) -o Makefile 44.pro
  7. #############################################################################
  8. ####### Compiler, tools and options
  9. CC      = gcc
  10. CXX      = g++
  11. LEX      = flex
  12. YACC    = yacc
  13. CFLAGS  = -pipe -Wall -W -O2  -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT
  14. CXXFLAGS = -pipe -Wall -W -O2  -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT
  15. LEXFLAGS =
  16. YACCFLAGS= -d
  17. INCPATH  = -I/usr/local/qt/mkspecs/default -I. -I. -I$(QTDIR)/include
  18. LINK    = g++
  19. LFLAGS  =  -Wl,-rpath,$(QTDIR)/lib
  20. LIBS    = $(SUBLIBS) -L$(QTDIR)/lib -L/usr/X11R6/lib -lqt -lXext -lX11 -lm
  21. AR      = ar cqs
  22. RANLIB  =
  23. MOC      = $(QTDIR)/bin/moc
  24. UIC      = $(QTDIR)/bin/uic
  25. QMAKE    = qmake
  26. TAR      = tar -cf
  27. GZIP    = gzip -9f
  28. COPY    = cp -f
  29. COPY_FILE= $(COPY)
  30. COPY_DIR = $(COPY) -r
  31. INSTALL_FILE= $(COPY_FILE)
  32. INSTALL_DIR = $(COPY_DIR)
  33. DEL_FILE = rm -f
  34. SYMLINK  = ln -sf
  35. DEL_DIR  = rmdir
  36. MOVE    = mv -f
  37. CHK_DIR_EXISTS= test -d
  38. MKDIR    = mkdir -p
  39. ####### Output directory
  40. OBJECTS_DIR = ./
  41. ####### Files
  42. HEADERS = sh.h \
  43.         thr.h
  44. SOURCES = main.cpp \
  45.         sh.cpp \
  46.         thr.cpp
  47. OBJECTS = main.o \
  48.         sh.o \
  49.         thr.o \
  50.         form1.o
  51. FORMS = form1.ui
  52. UICDECLS = form1.h
  53. UICIMPLS = form1.cpp
  54. SRCMOC  = moc_form1.cpp
  55. OBJMOC = moc_form1.o
  56. DIST       = 44.pro
  57. QMAKE_TARGET = 44
  58. DESTDIR  =
  59. TARGET  = 44
  60. first: all
  61. ####### Implicit rules
  62. .SUFFIXES: .c .o .cpp .cc .cxx .C
  63. .cpp.o:
  64.     $(CXX) -c -lpthread $(CXXFLAGS) $(INCPATH) -o $@ $<
  65. .cc.o:
  66.     $(CXX) -c -lpthread $(CXXFLAGS) $(INCPATH) -o $@ $<
  67. .cxx.o:
  68.     $(CXX) -c -lpthread $(CXXFLAGS) $(INCPATH) -o $@ $<
  69. .C.o:
  70.     $(CXX) -c -lpthread $(CXXFLAGS) $(INCPATH) -o $@ $<
  71. .c.o:
  72.     $(CC) -c -lpthread $(CFLAGS) $(INCPATH) -o $@ $<
  73. ####### Build rules
  74. all: Makefile $(TARGET)
  75. $(TARGET):  $(UICDECLS) $(OBJECTS) $(OBJMOC) 
  76.     $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJMOC) $(OBJCOMP) $(LIBS)
  77. mocables: $(SRCMOC)
  78. uicables: $(UICDECLS) $(UICIMPLS)
  79. $(MOC):
  80.     ( cd $(QTDIR)/src/moc && $(MAKE) )
  81. Makefile: 44.pro  /usr/local/qt/mkspecs/default/qmake.conf /usr/local/qt/lib/libqt.prl
  82.     $(QMAKE) -o Makefile 44.pro
  83. qmake:
  84.     @$(QMAKE) -o Makefile 44.pro
  85. dist:
  86.     @mkdir -p .tmp/44 && $(COPY_FILE) --parents $(SOURCES) $(HEADERS) $(FORMS) $(DIST) .tmp/44/ && ( cd `dirname .tmp/44` && $(TAR) 44.tar 44 && $(GZIP) 44.tar ) && $(MOVE) `dirname .tmp/44`/44.tar.gz . && $(DEL_FILE) -r .tmp/44
  87. mocclean:
  88.     -$(DEL_FILE) $(OBJMOC)
  89.     -$(DEL_FILE) $(SRCMOC)
  90. uiclean:
  91.     -$(DEL_FILE) $(UICIMPLS) $(UICDECLS)
  92. yaccclean:
  93. lexclean:
  94. clean: mocclean uiclean
  95.     -$(DEL_FILE) $(OBJECTS)
  96.     -$(DEL_FILE) *~ core *.core
  97. ####### Sub-libraries
  98. distclean: clean
  99.     -$(DEL_FILE) $(TARGET) $(TARGET)
  100. FORCE:
  101. ####### Compile
  102. main.o: main.cpp sh.h \
  103.         thr.h
  104. sh.o: sh.cpp form1.h
  105. thr.o: thr.cpp
  106. form1.h: form1.ui
  107.     $(UIC) form1.ui -o form1.h
  108. form1.cpp: form1.h form1.ui
  109.     $(UIC) form1.ui -i form1.h -o form1.cpp
  110. form1.o: form1.cpp form1.h
  111. moc_form1.o: moc_form1.cpp  form1.h
  112. moc_form1.cpp: $(MOC) form1.h
  113.     $(MOC) form1.h -o moc_form1.cpp
  114. ####### Install
  115. install: 
  116. uninstall: 
快速回复
限100 字节
 
上一个 下一个