-1

我正在尝试用 test.cpp 文件编译一个库,但即使

我得到了所有需要的东西,我仍然得到:

test.cpp:(.text+0x24): undefined reference to `initdevice(char*)'
test.cpp:(.text+0x4c): undefined reference to `write2device(char*, int)'
test.cpp:(.text+0x68): undefined reference to `closedevice()'
test.cpp:(.text+0x79): undefined reference to `write2device(char*, int)'
collect2: ld returned 1 exit status

主要功能:

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <limits.h>
#include <set>
#include <map>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include "writerThread.h"
#include "outputdevice.h"
using namespace std;

int writeToFile();

#define FILE_NAME   "/cs/stud/elishae/Documents/elisha.txt"

int main()
{
int status, id;

char *buf = (char *) malloc(10);
buf = "elishaefla";




//writeToFile();

status = initdevice("testFile.txt");

printf("status = %d\n", status);

id = write2device(buf, 10);

printf("id = %d\n", id);

closedevice();

id = write2device(buf, 10);

//printf("id = %d\n", id);

}

包含所需功能的文件:

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <limits.h>
#include <set>
#include <map>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include "writerThread.h"
using namespace std;

vector<TaskInfo *> taskQueue;

pthread_t writerThread;

pthread_mutex_t taskQueueMutex, condMutuex;

pthread_cond_t newTasksCond;

bool keepRunning;

int gMaxId;

int initdevice(char *filename)
{

int status;


keepRunning = true;



status = initWriterTrhead(&taskQueue, filename, &newTasksCond, &keepRunning);


status = pthread_mutex_init(&taskQueueMutex, NULL);

status = pthread_cond_init(&newTasksCond, NULL);

status = pthread_create(&writerThread, NULL, writerThreadMain, (void *) 1);

return status;
}


int write2device(char *buffer, int length)
{

/*
 * flow: 1) get mutux for taskMap.
 *       2) iterate over map, find lowest ID open to use - either free entry, or entry with wasItWritten == true.
 *       3) write buffer to map.
 *       4) return.
 */

unsigned int i;
TaskInfo *newTask, *taskTemp;
bool emptyEntryFound = false;

char *bufferCopy = (char *) malloc(length);

memcpy(bufferCopy, buffer, length);

newTask = (TaskInfo *) malloc(2*sizeof(int) + sizeof(bool) + length);
newTask->length = length;
newTask->buffer = bufferCopy;
newTask->wasItWritten = false;


pthread_mutex_lock(&taskQueueMutex);
// insert new task to taskMap TODO: check this code... really not sure it's working
for(i = 0; i < taskQueue.size(); i++)
{
    taskTemp = taskQueue.at(i);

    if(NULL == taskTemp)
    {
        printf("ERROR!!! write2device()\n");
        exit(-1);
    }
    if(taskTemp->wasItWritten == true)
    {
        taskTemp = newTask;
        emptyEntryFound = true;
        break;
    }

}

if(false == emptyEntryFound)
{
    // no empty entries on taskQueue, so we'll insert a new entry
    taskQueue.push_back(newTask);

}

newTask->taskId = i;

pthread_mutex_unlock(&taskQueueMutex);

// signal to writerThread new task was inserted to taskQueue
pthread_cond_signal(&newTasksCond);

printf("vector size = %d\n", taskQueue.size());


return newTask->taskId;
}

生成文件:

.SUFFIXES:      .o .cpp

.cpp.o :
        g++  -Wall -c -o $@ $<


all:    liboutputdevice.a

# remove the old tapestry library and remake the new one
liboutputdevice.a:  outputdevice.o writerThread.o 
        rm -f $@
        ar rc $@ outputdevice.o writerThread.o

# cleaning all created files    
clean:
        rm -f *.o liboutputdevice.a

在我构建了库之后。有了make,这就是我接下来要做的事情。

我试图一起编译的方式:

g++ -Wall -lpthread liboutputdevice.a test.cpp

为什么我会收到此错误?

4

3 回答 3

1

将库放在最后:

g++ -Wall -pthread test.cpp liboutputdevice.a

使用-pthread代替-lpthread(参见gcc - 编译时 -pthread 标志的意义)。

于 2012-05-15T13:33:45.540 回答
1

添加-loutputdevice到您的链接行:

g++ -Wall test.cpp -lpthread -loutputdevice

-L(如果库不在当前目录中,您可能也需要)。

于 2012-05-15T13:33:48.110 回答
0

试试看:g++ -Wall -lpthread -Llibrarypath/liboutputdevice.a test.cpp

于 2012-05-15T13:35:43.870 回答