{"id":1108,"date":"2022-08-30T15:12:31","date_gmt":"2022-08-30T15:12:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/cmake-issue-undefined-reference-to-a-mysql-library-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:31","modified_gmt":"2022-08-30T15:12:31","slug":"cmake-issue-undefined-reference-to-a-mysql-library-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/cmake-issue-undefined-reference-to-a-mysql-library-collection-of-common-programming-errors\/","title":{"rendered":"cmake issue: undefined reference to a MySQL library-Collection of common programming errors"},"content":{"rendered":"<p>With the following CMakeFiles.txt files for cmake, I get the linker error:<\/p>\n<pre><code>CMakeFiles\/MyPrj.dir\/src1.cpp.o: In function ...:\nsrc1.cpp: undefined reference to 'sql::mysql::get_driver_instance()'\ncollect2: ld returned 1 exit status\n<\/code><\/pre>\n<p>The same program compiles correctly in the NetBeans environment (without using cmake).<\/p>\n<p>Top level CMakeFiles.txt:<\/p>\n<pre><code>cmake_minimum_required(VERSION 2.8.0)\n\nproject(MyPrj)\n\n# --- MySQL Connector path\nset(MYSQL_CONNECTOR_PATH \"\/path\/to\/mysql-connector-c++-1.1.0\")\n\n# --- Automatically built configuration header file\nconfigure_file(\n    \"MyPrjConfig.h.in\"\n    \"${MyPrj_BINARY_DIR}\/MyPrjConfig.h\"\n    )\n\n# --- Add the binary tree to the search path for include files\ninclude_directories(\n    \"${MyPrj_BINARY_DIR}\"\n    )\n\nadd_subdirectory(src)\n<\/code><\/pre>\n<p>Lower level CMakeFiles.txt:<\/p>\n<pre><code>include_directories(${MYSQL_CONNECTOR_PATH}) \n\nlink_directories (\n    \/lib64\n    \/lib\n    ${MYSQL_CONNECTOR_PATH}\/driver\n)\n\nadd_executable ( myProg \n    src1.cpp \n    src2.cpp \n    src3.cpp \n)\n\ntarget_link_libraries ( myProg \n    mysqlcppconn-static\n    mysqlclient\n)\n<\/code><\/pre>\n<p>Thank you!<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:28:40. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>With the following CMakeFiles.txt files for cmake, I get the linker error: CMakeFiles\/MyPrj.dir\/src1.cpp.o: In function &#8230;: src1.cpp: undefined reference to &#8216;sql::mysql::get_driver_instance()&#8217; collect2: ld returned 1 exit status The same program compiles correctly in the NetBeans environment (without using cmake). Top level CMakeFiles.txt: cmake_minimum_required(VERSION 2.8.0) project(MyPrj) # &#8212; MySQL Connector path set(MYSQL_CONNECTOR_PATH &#8220;\/path\/to\/mysql-connector-c++-1.1.0&#8221;) # &#8212; Automatically [&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-1108","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1108","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=1108"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1108\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}