{"id":887,"date":"2022-08-30T15:08:50","date_gmt":"2022-08-30T15:08:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/create-a-cmakefiles-txt-for-newbies-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:50","modified_gmt":"2022-08-30T15:08:50","slug":"create-a-cmakefiles-txt-for-newbies-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/create-a-cmakefiles-txt-for-newbies-collection-of-common-programming-errors\/","title":{"rendered":"Create a CMakeFiles.txt for newbies-Collection of common programming errors"},"content":{"rendered":"<p>I have a project with the following structure<\/p>\n<pre><code>  \/cmake_modules\/\n    FindSFML.cmake\n  \/includes\/\n    car.hpp\n    motor.hpp\n    tires.hpp\n  \/sources\/\n    car.cpp\n    motor.cpp\n    tires.cpp    \n  \/main.cpp\n  \/main.hpp\n<\/code><\/pre>\n<p>I have the following <code>CMakeFiles.txt<\/code><\/p>\n<pre><code>cmake_minimum_required(VERSION 2.8)\n\nproject (MYGAME)\nset (MYGAME_VERSION_MAJOR 1)\nset (MYGAME_VERSION_MINOR 0)\n\nset (EXECUTABLE_NAME \"mygame\")\n\ninclude_directories (\"${MYGAME_BINARY_DIR}\")\ninclude_directories (\"${MYGAME_BINARY_DIR}\/includes\")\nlink_directories (\"${MYGAME_BINARY_DIR}\/sources\")\n\nadd_executable(${EXECUTABLE_NAME} main.cpp)\n\nset(CMAKE_MODULE_PATH \"${CMAKE_SOURCE_DIR}\/cmake_modules\" ${CMAKE_MODULE_PATH})\nfind_package(SFML 2.0 REQUIRED system window graphics network audio)\n\ntarget_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})\n<\/code><\/pre>\n<p>When I try to execute <code>make<\/code> i get this<\/p>\n<pre><code>[100%] Building CXX object CMakeFiles\/mygame.dir\/main.cpp.o\nLinking CXX executable mygame\nCMakeFiles\/mygame.dir\/main.cpp.o: In function `main':\nmain.cpp:(.text+0x11): undefined reference to `mynamespace::Car::Instance()'\nmain.cpp:(.text+0x21): undefined reference to `mynamespace::Car::start()'\ncollect2: error: ld returned 1 exit status\nmake[2]: *** [mygame] Error 1\nmake[1]: *** [CMakeFiles\/mygame.dir\/all] Error 2\nmake: *** [all] Error 2\n<\/code><\/pre>\n<p>How to fix it<\/p>\n<ol>\n<li>\n<p>You need to include the rest of your sources (car.cpp, motor.cpp and tires.cpp) in the build in some way.<\/p>\n<p>You can either add them along with main.cpp in the executable directly:<\/p>\n<pre><code>set(MySources sources\/car.cpp sources\/motor.cpp sources\/tires.cpp main.cpp)\nadd_executable(${EXECUTABLE_NAME} ${MySources})\n<\/code><\/pre>\n<p>or you can make these into a library and link that:<\/p>\n<pre><code>set(MyLibSources sources\/car.cpp sources\/motor.cpp sources\/tires.cpp)\nadd_library(MyLib ${MyLibSources})\nadd_executable(${EXECUTABLE_NAME} main.cpp)\n...\ntarget_link_libraries(${EXECUTABLE_NAME} MyLib ${SFML_LIBRARIES})\n<\/code><\/pre>\n<p>A couple of other points to note:<\/p>\n<p>You should avoid the use of <code>link_directories<\/code> if possible (its own documentation discourages its use), and it&#8217;s often helpful to include the headers in the list of files added via <code>add_executable<\/code> or <code>add_library<\/code> since these then show up in IDEs like MS Visual Studio.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:53:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a project with the following structure \/cmake_modules\/ FindSFML.cmake \/includes\/ car.hpp motor.hpp tires.hpp \/sources\/ car.cpp motor.cpp tires.cpp \/main.cpp \/main.hpp I have the following CMakeFiles.txt cmake_minimum_required(VERSION 2.8) project (MYGAME) set (MYGAME_VERSION_MAJOR 1) set (MYGAME_VERSION_MINOR 0) set (EXECUTABLE_NAME &#8220;mygame&#8221;) include_directories (&#8220;${MYGAME_BINARY_DIR}&#8221;) include_directories (&#8220;${MYGAME_BINARY_DIR}\/includes&#8221;) link_directories (&#8220;${MYGAME_BINARY_DIR}\/sources&#8221;) add_executable(${EXECUTABLE_NAME} main.cpp) set(CMAKE_MODULE_PATH &#8220;${CMAKE_SOURCE_DIR}\/cmake_modules&#8221; ${CMAKE_MODULE_PATH}) find_package(SFML 2.0 REQUIRED system window graphics [&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-887","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/887","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=887"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/887\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}