{"id":675,"date":"2022-08-30T15:05:18","date_gmt":"2022-08-30T15:05:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/cmake-undefined-reference-to-main-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:18","modified_gmt":"2022-08-30T15:05:18","slug":"cmake-undefined-reference-to-main-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/cmake-undefined-reference-to-main-collection-of-common-programming-errors\/","title":{"rendered":"CMake undefined reference to main-Collection of common programming errors"},"content":{"rendered":"<p>When I try to compile my program with <code>make<\/code>, I am getting an undefined reference to main error. Yet, main exists within my src directory, and I feel lost as to what I&#8217;m doing wrong.<\/p>\n<p>I assume that <code>add_executable([title] [source])<\/code> is the command used to add source files to the compilation.<\/p>\n<p><strong>Based on the cmake tutorial<\/strong><\/p>\n<pre><code>cmake_minimum_required(VERSION 2.6)\nproject(opengl_02)\nadd_executable(opengl_02 opengl_02.cpp)\nadd_executable(main main.cpp)\nadd_executable(geometrics geometrics.cpp)\nset (opengl_02_version_major 1)\nset (openfl_02_version_minor 0)\n\n#configure the header file to pass some of the CMake settings \n#to the source code\n\nconfigure_file(\n    \"${PROJECT_SOURCE_DIR}\/opengl_02_config.h.in\"\n    \"${PROJECT_BINARY_DIR}\/opengl_02_config.h\"\n    )\n\n#add the binary tree to the search path for include files\n#so that it will find tutorialconfig.h\n\ninclude_directories(\"{PROJECT_BINARY_DIR}\")\n\nadd_executable(opengl_02_config opengl_02_config.cpp)\n<\/code><\/pre>\n<p><strong>Question<\/strong><\/p>\n<p>Why is my main file not getting referenced?<\/p>\n<ol>\n<li>\n<p><code>int main (int argc, char *argv[])<\/code> (or its equivalent without parameters) must be present in every program. It is hard to say what is wrong with your setup without looking at source code, but I have a feeling that main function is not present in every file that you are trying to compile into executable, i.e. <code>opengl_02.cpp<\/code>, <code>geometrics.cpp<\/code> or <code>main.cpp<\/code>. If you really want to create three executables, <code>main<\/code> function should be present in all three source files in your example. If you want to create executable from three source files, you have to specify all of them for a single executable, like <code>add_executable(main main.cpp opengl_02.cpp geometrics.cpp)<\/code>. Hope it helps.<\/p>\n<\/li>\n<li>\n<p>int main() must NOT be present in every program. If you write tests with Gtest, there is only one &#8220;int main&#8221; in gtest. All other tests start with &#8220;Test()&#8221; and also create binaries.<\/p>\n<p>I have build multiple tests which created a binary for each, but this &#8220;undefined reference to&#8221; problem is NOT related to it.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:26:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>When I try to compile my program with make, I am getting an undefined reference to main error. Yet, main exists within my src directory, and I feel lost as to what I&#8217;m doing wrong. I assume that add_executable([title] [source]) is the command used to add source files to the compilation. Based on the cmake [&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-675","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/675","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=675"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/675\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}