{"id":1248,"date":"2022-08-30T15:14:51","date_gmt":"2022-08-30T15:14:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/with-lpthread-g-compiler-error-undefined-reference-to-semaphore-calls-such-as-sem_open-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:51","modified_gmt":"2022-08-30T15:14:51","slug":"with-lpthread-g-compiler-error-undefined-reference-to-semaphore-calls-such-as-sem_open-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/with-lpthread-g-compiler-error-undefined-reference-to-semaphore-calls-such-as-sem_open-collection-of-common-programming-errors\/","title":{"rendered":"with -lpthread, g++ compiler error, &ldquo;undefined reference to &rdquo; semaphore calls such as `sem_open&#39;-Collection of common programming errors"},"content":{"rendered":"<p>I am new to posix thread library, and I tried to compile a sample code from a tutorial with:<\/p>\n<pre><code>g++ -lpthread agreement.cpp -o agreement\n<\/code><\/pre>\n<p>however I was not able to compile the code and got the following error message:<\/p>\n<pre><code>a3q2.cpp:(.text+0x7e): undefined reference to `sem_open'\na3q2.cpp:(.text+0xab): undefined reference to `sem_wait'\na3q2.cpp:(.text+0x290): undefined reference to `sem_post'\na3q2.cpp:(.text+0x2af): undefined reference to `sem_close'\na3q2.cpp:(.text+0x2bb): undefined reference to `sem_unlink'\ncollect2: ld returned 1 exit status\nmake: *** [a3q2_exe] Error 1\n<\/code><\/pre>\n<p>I am aware that -lpthread is needed for compilation to work, but is there any other options i might need to solve the problem? if not how do I have to install the &#8220;proper&#8221; pthread library?<\/p>\n<p>Thanks for your help!<\/p>\n<ol>\n<li>\n<p>You want the compile option <code>-pthread<\/code> (if you are really using pthreads). If you just need those functions they are in <code>librt<\/code> so use <code>-lrt<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:12:09. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am new to posix thread library, and I tried to compile a sample code from a tutorial with: g++ -lpthread agreement.cpp -o agreement however I was not able to compile the code and got the following error message: a3q2.cpp:(.text+0x7e): undefined reference to `sem_open&#8217; a3q2.cpp:(.text+0xab): undefined reference to `sem_wait&#8217; a3q2.cpp:(.text+0x290): undefined reference to `sem_post&#8217; a3q2.cpp:(.text+0x2af): [&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-1248","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1248","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=1248"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1248\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}