Undefined reference to C++ libraries-Collection of common programming errors
I’m trying to compile a library with a test.cpp file, but even though
i get all the includes needed, i still get:
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
The main function:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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);
}
The file that holds the wanted functions:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "writerThread.h"
using namespace std;
vector 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;
}
the Makefile:
.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
After i built the lib. with make, this is what im trying to do next.
the way im trying to compile all together:
g++ -Wall -lpthread liboutputdevice.a test.cpp
why am i getting this error?
-
Add
-loutputdevice
to your link line:g++ -Wall test.cpp -lpthread -loutputdevice
(You might need
-L
too, if the library is not in the current directory). -
You first have to build
liboutputdevice.a
. It looks like all you have to do is runmake all
then
g++ -Wall -pthread test.cpp liboutputdevice.a
This is assuming that liboutputdevice.a actually contains the symbols you are missing. On a posix platrofm, you could check this with
nm -s –demangle liboptpurdevice.a
Note the
nm
command line options may vary, this example is for GNU/Linux. -
try that : g++ -Wall -lpthread -Llibrarypath/liboutputdevice.a test.cpp
Originally posted 2013-11-09 21:09:51.