{"id":21637,"date":"2022-11-14T14:23:49","date_gmt":"2022-11-14T08:53:49","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/"},"modified":"2022-11-14T14:23:49","modified_gmt":"2022-11-14T08:53:49","slug":"solved-makefile-in-c-ubuntu-multiple-definition","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/","title":{"rendered":"[Solved] Makefile in C (ubuntu) multiple definition"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-53790581\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"53790581\" data-parentid=\"53790348\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h2>read documentation!<\/h2>\n<p>First, take a few hours to read documentation of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.gnu.org\/software\/make\/manual\/make.html\">GNU make<\/a>, and read how to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Invoking-GCC.html\">invoke GCC<\/a>. You also need to understand more about the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/C_preprocessor\">preprocessor<\/a>, so read <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/gcc.gnu.org\/onlinedocs\/cpp\/\">documentation of <code>cpp<\/code><\/a>. You want to take advantage of builtin GNU <code>make<\/code> rules (so run <code>make -p<\/code> to understand them) and variables. See also this answer. You could use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/bashdb.sourceforge.net\/remake\/\">remake<\/a> (as <code>remake -x<\/code>) to debug your <code>Makefile<\/code>. You apparently don&#8217;t understand how <code>make<\/code> and how <code>gcc<\/code> should be used, so you need to read more. Read also a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.tutorialspoint.com\/cprogramming\/\">C tutorial<\/a>, look into some <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.cppreference.com\/w\/c\">C reference<\/a>, and glance when needed into the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/C11_(C_standard_revision)\">C11<\/a> standard <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n1570.pdf\">n1570<\/a>. Of course, read the documentation of <em>every<\/em> function you use (e.g. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/printf.3.html\">printf(3)<\/a> etc..). For Linux system programming, read a book like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.makelinux.net\/alp\/\">ALP<\/a> and relevant <code>man<\/code> pages from <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man2\/syscalls.2.html\">syscalls(2)<\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/intro.3.html\">intro(3)<\/a> etc&#8230;<\/p>\n<p>Then read <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/ericlippert.com\/2014\/03\/05\/how-to-debug-small-programs\/\"><em>How to debug small programs<\/em><\/a>. You certainly want to compile with all warnings and debug info.<\/p>\n<hr>\n<h2>a better <code>Makefile<\/code><\/h2>\n<p>You might try something like:<\/p>\n<pre><code># a better Makefile\n# your C compiler\nCC= gcc\n\n# the verbose remove\nRM= rm -vf\n\n# your C compilation flags\nCFLAGS= -Wall -Wextra -g\n\n# your C source files\nMY_CSOURCES= logfind.c cmdargutils.c filesystem_utils.c file_utils.c strutils.c\n\n# the corresponding object files\nMY_OBJECTS= $(patsubst %.c, %.o, $(MY_CSOURCES))\n\n# the conventional phony targets\n.PHONY: all clean\n\n# the only program is for the default target all\nall: logfind\nlogfind: $(MY_OBJECTS)\n     $(LINK.c) $&lt; -o $@\n\n# cleaning the mess\nclean: \n     $(RM) logfind *.o *~\n<\/code><\/pre>\n<p>Of course, you need dependencies for object files on header files. You could compute them automatically, but it is simpler to explicit them, so add something like:<\/p>\n<pre><code>strutils.o: strutils.c strutils.h\n<\/code><\/pre>\n<p>and so on for each other object files.<\/p>\n<p>BTW my <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/bstarynk\/misc-basile\/tree\/master\/HelloWorld\">HelloWorld\/<\/a> directory on github is a tutorial example for using <code>make<\/code><\/p>\n<hr>\n<h2>your <em>multiple definition<\/em> bug<\/h2>\n<p>You are getting <code>multiple definition of MAX_LINE<\/code> because it is <em>defined<\/em> in a header file included by several <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Translation_unit_(programming)\">translation units<\/a>, hence several translation units define it.<\/p>\n<p>So either make it a preprocessor constant <code>#define MAX_LINE 1024<\/code> in your header <code>file_utils.h<\/code>, or put there only a <em>declaration<\/em> like <code>extern const int MAX_LINE;<\/code> and <em>define<\/em> it only once in a single translation unit, as <code>const int MAX_LINE=1024;<\/code> in <code>file_utils.c<\/code><\/p>\n<hr>\n<h2>general hints<\/h2>\n<p>I strongly recommend doing some <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Iterative_and_incremental_development\">iterative and incremental development<\/a>: code only one or two dozen lines at once, then compile them, improve them to get no warnings, debug them with the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/sourceware.org\/gdb\/current\/onlinedocs\/gdb\/\">GDB debugger<\/a> and test them. At last repeat all this till satisfied. I do recommend using also a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Version_control\">version control<\/a> system (like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/git-scm.com\/\">git<\/a>) even for school homework.<\/p>\n<p>You might want to use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/valgrind.org\/\">valgrind<\/a> to hunt <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Memory_leak\">memory leaks<\/a> and other <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/C_dynamic_memory_allocation\">dynamic memory allocation<\/a> bugs.<\/p>\n<p>You could also use some static source analyzer like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/clang-analyzer.llvm.org\/\">clang-analyzer<\/a> or even <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/frama-c.com\/\">Frama-C<\/a>.<\/p>\n<p>Once your program is debugged, you might add <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Optimizing_compiler\">optimization flags<\/a> like <code>-O2<\/code> into your <code>CFLAGS<\/code> (in particular if you benchmark it with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/time.1.html\">time(1)<\/a>).<\/p>\n<p>You could be interested by <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/nftw.3.html\">ntfw(3)<\/a>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Makefile in C (ubuntu) multiple definition <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] read documentation! First, take a few hours to read documentation of GNU make, and read how to invoke GCC. You also need to understand more about the preprocessor, so read documentation of cpp. You want to take advantage of builtin GNU make rules (so run make -p to understand them) and variables. See also &#8230; <a title=\"[Solved] Makefile in C (ubuntu) multiple definition\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\" aria-label=\"More on [Solved] Makefile in C (ubuntu) multiple definition\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,950],"class_list":["post-21637","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-makefile"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Makefile in C (ubuntu) multiple definition - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Makefile in C (ubuntu) multiple definition - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] read documentation! First, take a few hours to read documentation of GNU make, and read how to invoke GCC. You also need to understand more about the preprocessor, so read documentation of cpp. You want to take advantage of builtin GNU make rules (so run make -p to understand them) and variables. See also ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-14T08:53:49+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Makefile in C (ubuntu) multiple definition\",\"datePublished\":\"2022-11-14T08:53:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\"},\"wordCount\":386,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"makefile\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\",\"name\":\"[Solved] Makefile in C (ubuntu) multiple definition - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-14T08:53:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Makefile in C (ubuntu) multiple definition\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Makefile in C (ubuntu) multiple definition - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Makefile in C (ubuntu) multiple definition - JassWeb","og_description":"[ad_1] read documentation! First, take a few hours to read documentation of GNU make, and read how to invoke GCC. You also need to understand more about the preprocessor, so read documentation of cpp. You want to take advantage of builtin GNU make rules (so run make -p to understand them) and variables. See also ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/","og_site_name":"JassWeb","article_published_time":"2022-11-14T08:53:49+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Makefile in C (ubuntu) multiple definition","datePublished":"2022-11-14T08:53:49+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/"},"wordCount":386,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","makefile"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/","url":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/","name":"[Solved] Makefile in C (ubuntu) multiple definition - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-14T08:53:49+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-makefile-in-c-ubuntu-multiple-definition\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Makefile in C (ubuntu) multiple definition"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=21637"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21637\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=21637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=21637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=21637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}