{"id":927,"date":"2022-08-30T15:09:30","date_gmt":"2022-08-30T15:09:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/c-compiling-with-python-h-undefined-symbols-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:30","modified_gmt":"2022-08-30T15:09:30","slug":"c-compiling-with-python-h-undefined-symbols-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/c-compiling-with-python-h-undefined-symbols-collection-of-common-programming-errors\/","title":{"rendered":"C++ Compiling with Python.h Undefined Symbols-Collection of common programming errors"},"content":{"rendered":"<p>So, I&#8217;ve been trying to start using Python.h for a little project I want to work on that seems pretty \/simple\/. But before I start I want to try to learn how to use Python.h. So I found this little example online.<\/p>\n<pre><code>#include \"Python\/Python.h\"  \n\nint main(int argc, char** argv)  \n{  \n    Py_Initialize();  \n    PyRun_SimpleString(\"print 'Test'\");  \n    PyRun_SimpleString(\"print str(3 + 5)\"); \n    Py_Exit(0);  \n}\n<\/code><\/pre>\n<p>Seems pretty straight forward. When i first used<\/p>\n<pre><code>gcc test.cpp\n<\/code><\/pre>\n<p>to compile, i got some undefined symbols. I quickly found out I should use<\/p>\n<pre><code>-lpython2.7\n<\/code><\/pre>\n<p>then I found out I could also use<\/p>\n<pre><code>-L\/Library\/Frameworks\/Python.framework\/Versions\/2.7\/lib\/\n<\/code><\/pre>\n<p>that didn&#8217;t work (I made sure that \/Library\/Frameworks\/Python\/Versions\/2.7\/lib\/ existed) I&#8217;m stuck, what do I do? I get<\/p>\n<pre><code>Undefined symbols:\n  \"_Py_Initialize\", referenced from:\n      _main in ccoUOSlc.o\n  \"_PyRun_SimpleStringFlags\", referenced from:\n      _main in ccoUOSlc.o\n      _main in ccoUOSlc.o\n  \"___gxx_personality_v0\", referenced from:\n      _main in ccoUOSlc.o\n      CIE in ccoUOSlc.o\n  \"_Py_Exit\", referenced from:\n      _main in ccoUOSlc.o\nld: symbol(s) not found\ncollect2: ld returned 1 exit status\n<\/code><\/pre>\n<p>EDIT: I just tried using the -Framework argument, and tried adding after the -L the -l python2.7 argument, and I now get<\/p>\n<pre><code>Undefined symbols:\n  \"___gxx_personality_v0\", referenced from:\n      _main in ccfvtJ4j.o\n      CIE in ccfvtJ4j.o\nld: symbol(s) not found\ncollect2: ld returned 1 exit status\n<\/code><\/pre>\n<p>Now what?<\/p>\n<ol>\n<li>\n<p>If you are using an Python framework installation on OS X as it appears you are based on the paths, you can use the <code>-framework<\/code> argument to the Apple compiler drivers:<\/p>\n<pre><code>cc test.cpp -framework Python\n<\/code><\/pre>\n<p>Alternatively, you can explicitly specify the directory path and library name:<\/p>\n<pre><code>cc test.cpp -L \/Library\/Frameworks\/Python.framework\/Versions\/2.7\/lib\/ -l python2.7\n<\/code><\/pre>\n<p>Update: With the configuration you report in the comments (<code>Xcode 3.2.6<\/code>, <code>gcc-4.2<\/code>), it appears you need to explicitly invoke the <code>c++<\/code> variant of <code>gcc<\/code>. Either:<\/p>\n<pre><code>g++ test.cpp -framework Python\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>c++ test.cpp -framework Python\n<\/code><\/pre>\n<p>should work.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:59:17. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>So, I&#8217;ve been trying to start using Python.h for a little project I want to work on that seems pretty \/simple\/. But before I start I want to try to learn how to use Python.h. So I found this little example online. #include &#8220;Python\/Python.h&#8221; int main(int argc, char** argv) { Py_Initialize(); PyRun_SimpleString(&#8220;print &#8216;Test'&#8221;); PyRun_SimpleString(&#8220;print str(3 [&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-927","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/927","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=927"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}