使用pthread

时间:2018-05-23 10:02:16

标签: c++ pthreads

我在编译以下文件时遇到问题。只是为了解释一下这种情况:我确实得到了除Makefile之外的所有文件。我的问题是,由于一些未定义的引用,我无法编译程序。我无权访问unit.cpp或plotx11.cpp。也许有人知道导致问题的原因。

生成文件:

CXXFLAGS = -g -Wall
LIBS = -lX11 -pthread
C11 = -std=c++11


vierGewinnt: vierGewinnt.cpp unit.o plotx11.o
    $(CXX) $(CXXFLAGS) $(C11) $(LIBS) -o $@ $^


vierGewinnt.o: vierGewinnt.cpp unit.h
    $(CXX) $(CXXFLAGS) $(C11) $(LIBS)   -c $<

.PHONY.: clean

clean:
    rm -f vierGewinnt vierGewinnt.o

Unit.h仅包含一些方法声明和一些常量变量。

plotx11.h:

#ifndef __PLOTX11__
#define __PLOTX11__

enum PlColors
{
  Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, DarkGrey,
  DarkRed, DarkGreen, DarkYellow, DarkBlue, DarkMagenta, DarkCyan, Grey
};

void PlDelay (int msecs);                   
void PlInit  (int &width, int &height);     

void PlClear ();                            
void PlFlush ();                        
void PlStop  ();                        
void PlLineColor  (PlColors color);         
void PlLineWidth  (int width);              
void PlMarkerSize (int width);              

void PlFrame (int x0, int x1, int y0, int y1);
void PlScale (double x0, double x1, double y0, double y1);

void PlPlot (double x, double y, int mode);

void PlWhere (double &x, double &y);
void HandleXEvents();

#endif

vierGewinnt.cpp:

#include <cmath>
#include <iostream>
#include <random>
#include "unit.h"

void NetzwerkMain();

// ...

// const unsigned int Schwierigkeitsgrad = 0;

enum Feld
{ leer, gelb, rot };



int main()
{
    int Zug, Gegenzug;

    // Netzwerkspiel? Rufe NetzwerkMain() auf.

// ...

//    Start(Schwierigkeitsgrad);

    for(unsigned int Spiel = 1; Spiel <= AnzahlSpiele; Spiel++)
    {

// ...

    }

    return 0;
}

enum class SpielStatus {
    Verbindungsfehler,
    Niederlage,
    Unentschieden,
    Sieg
};

SpielStatus Netzwerkspiel( Feld MeineFarbe ) {

    return SpielStatus::Verbindungsfehler;
}

错误:

    g++ -O -g -Wall -std=c++11 -lX11 -lpthread -o vierGewinnt vierGewinnt.o unit.o plotx11.o
unit.o: In function `asio::ip::resolver_service<asio::ip::tcp>::shutdown_service()':
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEE16shutdown_serviceEv[_ZN4asio2ip16resolver_serviceINS0_3tcpEE16shutdown_serviceEv]+0x1d6): undefined reference to `pthread_join'
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEE16shutdown_serviceEv[_ZN4asio2ip16resolver_serviceINS0_3tcpEE16shutdown_serviceEv]+0x1f9): undefined reference to `pthread_detach'
unit.o: In function `asio::ip::resolver_service<asio::ip::tcp>::~resolver_service()':
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEED0Ev[_ZN4asio2ip16resolver_serviceINS0_3tcpEED5Ev]+0x314): undefined reference to `pthread_detach'
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEED0Ev[_ZN4asio2ip16resolver_serviceINS0_3tcpEED5Ev]+0x336): undefined reference to `pthread_join'
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEED0Ev[_ZN4asio2ip16resolver_serviceINS0_3tcpEED5Ev]+0x359): undefined reference to `pthread_detach'
unit.o: In function `asio::ip::resolver_service<asio::ip::tcp>::~resolver_service()':
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEED2Ev[_ZN4asio2ip16resolver_serviceINS0_3tcpEED5Ev]+0x304): undefined reference to `pthread_detach'
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEED2Ev[_ZN4asio2ip16resolver_serviceINS0_3tcpEED5Ev]+0x326): undefined reference to `pthread_join'
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEED2Ev[_ZN4asio2ip16resolver_serviceINS0_3tcpEED5Ev]+0x349): undefined reference to `pthread_detach'
unit.o: In function `asio::ip::resolver_service<asio::ip::tcp>::fork_service(asio::io_service::fork_event)':
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEE12fork_serviceENS_10io_service10fork_eventE[_ZN4asio2ip16resolver_serviceINS0_3tcpEE12fork_serviceENS_10io_service10fork_eventE]+0x10d): undefined reference to `pthread_create'
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEE12fork_serviceENS_10io_service10fork_eventE[_ZN4asio2ip16resolver_serviceINS0_3tcpEE12fork_serviceENS_10io_service10fork_eventE]+0x176): undefined reference to `pthread_join'
unit.cpp:(.text._ZN4asio2ip16resolver_serviceINS0_3tcpEE12fork_serviceENS_10io_service10fork_eventE[_ZN4asio2ip16resolver_serviceINS0_3tcpEE12fork_serviceENS_10io_service10fork_eventE]+0x18d): undefined reference to `pthread_detach'
plotx11.o: In function `PlLineWidth(int)':
plotx11.cpp:(.text+0x1c2): undefined reference to `XChangeGC'
plotx11.o: In function `PlClear()':
plotx11.cpp:(.text+0x203): undefined reference to `XClearWindow'
plotx11.o: In function `PlStop()':
plotx11.cpp:(.text+0x263): undefined reference to `XClearWindow'
plotx11.cpp:(.text+0x2ab): undefined reference to `XDestroyWindow'
plotx11.o: In function `PlInit(int&, int&)':
plotx11.cpp:(.text+0x2d4): undefined reference to `XOpenDisplay'
plotx11.cpp:(.text+0x316): undefined reference to `XSynchronize'
plotx11.cpp:(.text+0x328): undefined reference to `XDefaultColormap'
plotx11.cpp:(.text+0x3d0): undefined reference to `XAllocColor'
plotx11.cpp:(.text+0x42e): undefined reference to `XCreateSimpleWindow'
plotx11.cpp:(.text+0x463): undefined reference to `XSetStandardProperties'
plotx11.cpp:(.text+0x47d): undefined reference to `XSelectInput'
plotx11.cpp:(.text+0x4be): undefined reference to `XChangeWindowAttributes'
plotx11.cpp:(.text+0x4d5): undefined reference to `XCreateGC'
plotx11.cpp:(.text+0x50e): undefined reference to `XSetState'
plotx11.cpp:(.text+0x521): undefined reference to `XMapWindow'
plotx11.cpp:(.text+0x52f): undefined reference to `XSync'
plotx11.o: In function `pplot(double, double, int)':
plotx11.cpp:(.text+0x733): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x79d): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x7cf): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x858): undefined reference to `XFillArc'
plotx11.cpp:(.text+0x89e): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x8cc): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x8fe): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x951): undefined reference to `XFillRectangle'
plotx11.cpp:(.text+0x992): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x9c4): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xa1b): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xa78): undefined reference to `XDrawArc'
plotx11.o: In function `PlPlot(double, double, int)':
plotx11.cpp:(.text+0xbd5): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xc3d): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xc6f): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xcf8): undefined reference to `XFillArc'
plotx11.cpp:(.text+0xd3e): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xd6c): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xd9e): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xdf1): undefined reference to `XFillRectangle'
plotx11.cpp:(.text+0xe32): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xe64): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xebb): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0xf18): undefined reference to `XDrawArc'
plotx11.o: In function `XGetNextEvent(_XEvent*)':
plotx11.cpp:(.text+0xf5b): undefined reference to `XNextEvent'
plotx11.cpp:(.text+0xf9d): undefined reference to `XEventsQueued'
plotx11.cpp:(.text+0xfb0): undefined reference to `XPeekEvent'
plotx11.cpp:(.text+0xfed): undefined reference to `XEventsQueued'
plotx11.cpp:(.text+0x1000): undefined reference to `XPeekEvent'
plotx11.cpp:(.text+0x1056): undefined reference to `XNextEvent'
plotx11.cpp:(.text+0x106d): undefined reference to `XEventsQueued'
plotx11.cpp:(.text+0x1084): undefined reference to `XPeekEvent'
plotx11.o: In function `HandleXEvents()':
plotx11.cpp:(.text+0x10dd): undefined reference to `XEventsQueued'
plotx11.cpp:(.text+0x10f6): undefined reference to `XNextEvent'
plotx11.cpp:(.text+0x111d): undefined reference to `XRefreshKeyboardMapping'
plotx11.cpp:(.text+0x1141): undefined reference to `XClearWindow'
plotx11.cpp:(.text+0x1179): undefined reference to `XSetForeground'
plotx11.cpp:(.text+0x1275): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x12f0): undefined reference to `XFillArc'
plotx11.cpp:(.text+0x134a): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x137c): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x13ae): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x13f1): undefined reference to `XFillRectangle'
plotx11.cpp:(.text+0x142d): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x145b): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x148c): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x14dc): undefined reference to `XDrawLine'
plotx11.cpp:(.text+0x1538): undefined reference to `XDrawArc'
plotx11.cpp:(.text+0x157b): undefined reference to `XDrawLine'
plotx11.o: In function `PlLineColor(PlColors)':
plotx11.cpp:(.text+0x190): undefined reference to `XSetForeground'
plotx11.o: In function `PlFlush()':
plotx11.cpp:(.text+0x1ea): undefined reference to `XSync'
plotx11.o: In function `PlStop()':
plotx11.cpp:(.text+0x2bb): undefined reference to `XCloseDisplay'
collect2: error: ld returned 1 exit status
Makefile:7: recipe for target 'vierGewinnt' failed
make: *** [vierGewinnt] Error 1

0 个答案:

没有答案