2012-05-15 6 views
-1

Я пытаюсь скомпилировать библиотеку с файлом test.cpp, но даже еслиНеопределенная ссылка на библиотеки C++

я получаю все включает в себя нужно, я все еще получаю:

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 [email protected] $< 


all: liboutputdevice.a 

# remove the old tapestry library and remake the new one 
liboutputdevice.a: outputdevice.o writerThread.o 
     rm -f [email protected] 
     ar rc [email protected] outputdevice.o writerThread.o 

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

После того, как я построил LIB. с make, это то, что я пытаюсь сделать дальше.

путь им пытаются собрать все вместе:

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

почему я получаю эту ошибку?

+1

вы построили .a? – juanchopanza

+0

да, я построил .a – Itzik984

ответ

1

Добавить -loutputdevice свою ссылку линии:

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

(Вы, возможно, потребуется -L тоже, если библиотека не в текущем каталоге).

+0

Я согласен - если это библиотека, ссылку на нее, как будто это библиотека! – AAT

0

попробовать что: г ++ -Wall -lpthread -Llibrarypath/liboutputdevice.a test.cpp

Смежные вопросы