{"id":641,"date":"2022-08-30T15:04:44","date_gmt":"2022-08-30T15:04:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/undefined-reference-to-c-libraries-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:44","modified_gmt":"2022-08-30T15:04:44","slug":"undefined-reference-to-c-libraries-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/undefined-reference-to-c-libraries-collection-of-common-programming-errors\/","title":{"rendered":"Undefined reference to C++ libraries-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to compile a library with a test.cpp file, but even though<\/p>\n<p>i get all the includes needed, i still get:<\/p>\n<pre><code>test.cpp:(.text+0x24): undefined reference to `initdevice(char*)'\ntest.cpp:(.text+0x4c): undefined reference to `write2device(char*, int)'\ntest.cpp:(.text+0x68): undefined reference to `closedevice()'\ntest.cpp:(.text+0x79): undefined reference to `write2device(char*, int)'\ncollect2: ld returned 1 exit status\n<\/code><\/pre>\n<p>The main function:<\/p>\n<pre><code>#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \"writerThread.h\"\n#include \"outputdevice.h\"\nusing namespace std;\n\nint writeToFile();\n\n#define FILE_NAME   \"\/cs\/stud\/elishae\/Documents\/elisha.txt\"\n\nint main()\n{\nint status, id;\n\nchar *buf = (char *) malloc(10);\nbuf = \"elishaefla\";\n\n\n\n\n\/\/writeToFile();\n\nstatus = initdevice(\"testFile.txt\");\n\nprintf(\"status = %d\\n\", status);\n\nid = write2device(buf, 10);\n\nprintf(\"id = %d\\n\", id);\n\nclosedevice();\n\nid = write2device(buf, 10);\n\n\/\/printf(\"id = %d\\n\", id);\n\n}\n<\/code><\/pre>\n<p>The file that holds the wanted functions:<\/p>\n<pre><code>#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \"writerThread.h\"\nusing namespace std;\n\nvector taskQueue;\n\npthread_t writerThread;\n\npthread_mutex_t taskQueueMutex, condMutuex;\n\npthread_cond_t newTasksCond;\n\nbool keepRunning;\n\nint gMaxId;\n\nint initdevice(char *filename)\n{\n\nint status;\n\n\nkeepRunning = true;\n\n\n\nstatus = initWriterTrhead(&amp;taskQueue, filename, &amp;newTasksCond, &amp;keepRunning);\n\n\nstatus = pthread_mutex_init(&amp;taskQueueMutex, NULL);\n\nstatus = pthread_cond_init(&amp;newTasksCond, NULL);\n\nstatus = pthread_create(&amp;writerThread, NULL, writerThreadMain, (void *) 1);\n\nreturn status;\n}\n\n\nint write2device(char *buffer, int length)\n{\n\n\/*\n * flow: 1) get mutux for taskMap.\n *       2) iterate over map, find lowest ID open to use - either free entry, or entry with wasItWritten == true.\n *       3) write buffer to map.\n *       4) return.\n *\/\n\nunsigned int i;\nTaskInfo *newTask, *taskTemp;\nbool emptyEntryFound = false;\n\nchar *bufferCopy = (char *) malloc(length);\n\nmemcpy(bufferCopy, buffer, length);\n\nnewTask = (TaskInfo *) malloc(2*sizeof(int) + sizeof(bool) + length);\nnewTask-&gt;length = length;\nnewTask-&gt;buffer = bufferCopy;\nnewTask-&gt;wasItWritten = false;\n\n\npthread_mutex_lock(&amp;taskQueueMutex);\n\/\/ insert new task to taskMap TODO: check this code... really not sure it's working\nfor(i = 0; i &lt; taskQueue.size(); i++)\n{\n    taskTemp = taskQueue.at(i);\n\n    if(NULL == taskTemp)\n    {\n        printf(\"ERROR!!! write2device()\\n\");\n        exit(-1);\n    }\n    if(taskTemp-&gt;wasItWritten == true)\n    {\n        taskTemp = newTask;\n        emptyEntryFound = true;\n        break;\n    }\n\n}\n\nif(false == emptyEntryFound)\n{\n    \/\/ no empty entries on taskQueue, so we'll insert a new entry\n    taskQueue.push_back(newTask);\n\n}\n\nnewTask-&gt;taskId = i;\n\npthread_mutex_unlock(&amp;taskQueueMutex);\n\n\/\/ signal to writerThread new task was inserted to taskQueue\npthread_cond_signal(&amp;newTasksCond);\n\nprintf(\"vector size = %d\\n\", taskQueue.size());\n\n\nreturn newTask-&gt;taskId;\n}\n<\/code><\/pre>\n<p>the Makefile:<\/p>\n<pre><code>.SUFFIXES:      .o .cpp\n\n.cpp.o :\n        g++  -Wall -c -o $@ $&lt;\n\n\nall:    liboutputdevice.a\n\n# remove the old tapestry library and remake the new one\nliboutputdevice.a:  outputdevice.o writerThread.o \n        rm -f $@\n        ar rc $@ outputdevice.o writerThread.o\n\n# cleaning all created files    \nclean:\n        rm -f *.o liboutputdevice.a\n<\/code><\/pre>\n<p>After i built the lib. with make, this is what im trying to do next.<\/p>\n<p>the way im trying to compile all together:<\/p>\n<pre><code>g++ -Wall -lpthread liboutputdevice.a test.cpp\n<\/code><\/pre>\n<p>why am i getting this error?<\/p>\n<ol>\n<li>\n<p>Add <code>-loutputdevice<\/code> to your link line:<\/p>\n<pre><code>g++ -Wall test.cpp -lpthread -loutputdevice\n<\/code><\/pre>\n<p>(You might need <code>-L<\/code> too, if the library is not in the current directory).<\/p>\n<\/li>\n<li>\n<p>You first have to build <code>liboutputdevice.a<\/code>. It looks like all you have to do is run<\/p>\n<blockquote>\n<p>make all<\/p>\n<\/blockquote>\n<p>then<\/p>\n<blockquote>\n<p>g++ -Wall -pthread test.cpp liboutputdevice.a<\/p>\n<\/blockquote>\n<p>This is assuming that liboutputdevice.a actually contains the symbols you are missing. On a posix platrofm, you could check this with<\/p>\n<blockquote>\n<p>nm -s &#8211;demangle liboptpurdevice.a<\/p>\n<\/blockquote>\n<p>Note the <code>nm<\/code> command line options may vary, this example is for GNU\/Linux.<\/p>\n<\/li>\n<li>\n<p>try that : g++ -Wall -lpthread -Llibrarypath\/liboutputdevice.a test.cpp<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:09:51. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;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*)&#8217; test.cpp:(.text+0x4c): undefined reference to `write2device(char*, int)&#8217; test.cpp:(.text+0x68): undefined reference to `closedevice()&#8217; test.cpp:(.text+0x79): undefined reference to `write2device(char*, int)&#8217; collect2: ld returned 1 exit status The main function: #include #include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-641","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=641"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/641\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}