{"id":1019,"date":"2022-08-30T15:11:02","date_gmt":"2022-08-30T15:11:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/more-gcc-link-time-issues-undefined-reference-to-main-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:02","modified_gmt":"2022-08-30T15:11:02","slug":"more-gcc-link-time-issues-undefined-reference-to-main-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/more-gcc-link-time-issues-undefined-reference-to-main-collection-of-common-programming-errors\/","title":{"rendered":"More GCC link time issues: undefined reference to main-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m writing software for a Cortex-A8 processor and I have to write some ARM assembly code to access specific registers. I&#8217;m making use of the gnu compilers and related tool chains, these tools are installed on the processor board(Freescale i.MX515) with Ubuntu. I make a connection to it from my host PC(Windows) using WinSCP and the PuTTY terminal.<\/p>\n<p>As usual I started with a simple C project having <strong>main.c<\/strong> and <strong>functions.s<\/strong>. I <strong>compile<\/strong> the main.c using <strong>GCC<\/strong>, <strong>assemble<\/strong> the functions.s using <strong>as<\/strong> and <strong>link<\/strong> the generated object files using once again <strong>GCC<\/strong>, but I get strange errors during this process.<\/p>\n<p><strong>An important finding &#8211;<\/strong><\/p>\n<p>Meanwhile, I found out that my assembly code may have some issues because when I individually assemble it using the command <code>as -o functions.o functions.s<\/code> and try running the generated <strong>functions.o<\/strong> using <code>.\/functions.o<\/code> command, the bash shell is failing to recognize this file as an executable(on pressing tab functions.o is not getting selected\/PuTTY is not highlighting the file).<\/p>\n<p>Can anyone suggest whats happening here? Are there any <strong>specific options I have to send, to GCC<\/strong> during the linking process? The errors I see are strange and beyond my understanding, I don&#8217;t understand to what the GCC is referring.<\/p>\n<p>I&#8217;m pasting here the contents of main.c, functions.s, the Makefile and the list of errors.<\/p>\n<h2>Help, please!!!<\/h2>\n<p><strong>Latest errors included after the makfile was edited as suggested by guys here &#8211;<\/strong><\/p>\n<pre><code>ubuntu@ubuntu-desktop:~\/Documents\/Project\/Others\/helloworld$ make\ngcc -c -mcpu=cortex-a8 main.c\nas -mcpu=cortex-a8 -o functions.o functions.s\ngcc -o hello main.o functions.o\nfunctions.o: In function `_start':\n(.text+0x0): multiple definition of `_start'\n\/usr\/lib\/gcc\/arm-linux-gnueabi\/4.3.3\/..\/..\/..\/crt1.o:init.c:(.text+0x0): first defined here\ncollect2: ld returned 1 exit status\nmake: *** [hello] Error 1\n<\/code><\/pre>\n<p><strong>main.c<\/strong><\/p>\n<pre><code>#include \n#include \n\nint main(void) {\n\n    puts(\"!!!Hello World!!!\"); \/* prints !!!Hello World!!! *\/\n    return EXIT_SUCCESS;\n}\n<\/code><\/pre>\n<p><strong>functions.s<\/strong><\/p>\n<pre><code>* Main program *\/\n    .equ      STACK_TOP, 0x20000800\n    .text\n    .global _start\n    .syntax unified\n\n_start:\n    .word STACK_TOP, start\n    .type start, function\n\nstart:\n    movs  r0, #10\n    movs  r1, #0\n    .end\n<\/code><\/pre>\n<p><strong>Makefile<\/strong><\/p>\n<pre><code>all: hello\n\nhello: main.o functions.o\n    gcc hello -o main.o functions.o\n<\/code><\/pre>\n<p>&#8212; hello was included here after suggested here by guys at stackoverflow, but the problem still persists, I still get the same errors.<\/p>\n<pre><code>main.o: main.c\n    gcc -c -mcpu=cortex-a8 main.c\n\nfunctions.o: functions.s\n    as -mcpu=cortex-a8 -o functions.o functions.s\n<\/code><\/pre>\n<p><strong>Errors<\/strong><\/p>\n<pre><code>ubuntu@ubuntu-desktop:~\/Documents\/Project\/Others\/helloworld$ make\ngcc -c -mcpu=cortex-a8 main.c\nas -mcpu=cortex-a8 -o functions.o functions.s\ngcc -o main.o functions.o\nfunctions.o: In function `_start':\n(.text+0x0): multiple definition of `_start'\n\/usr\/lib\/gcc\/arm-linux-gnueabi\/4.3.3\/..\/..\/..\/crt1.o:init.c:(.text+0x0): first defined here\n\/usr\/lib\/gcc\/arm-linux-gnueabi\/4.3.3\/..\/..\/..\/crt1.o: In function `_start':\ninit.c:(.text+0x30): undefined reference to `main'\ncollect2: ld returned 1 exit status\nmake: *** [hello] Error 1\n<\/code><\/pre>\n<ol>\n<li>\n<p>In the makefile:<\/p>\n<pre><code>hello: main.o functions.o\n    gcc -o main.o functions.o\n<\/code><\/pre>\n<p>should be:<\/p>\n<pre><code>hello: main.o functions.o\n    gcc -o hello main.o functions.o\n<\/code><\/pre>\n<p>As it stands, you are linking functions.o, but not main.o, and producing an output executable called main.o, which is overwriting your existing main.o.<\/p>\n<\/li>\n<li>\n<p>Shouldn&#8217;t<\/p>\n<pre><code>hello: main.o functions.o\n    gcc -o main.o functions.o\n<\/code><\/pre>\n<p>be<\/p>\n<pre><code>hello: main.o functions.o\n    gcc -o hello main.o functions.o\n<\/code><\/pre>\n<\/li>\n<li>\n<p>As Bigbohne suggests, gcc is trying to link in the standard runtime library. Try adding the -nostdlib option to your gcc call:<\/p>\n<pre><code>gcc -nostdlib -o hello main.o functions.o\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I think that has something to do with the Runtime library the gcc is linking at the end.<\/p>\n<p>And in <strong>this<\/strong> library there already is a &#8220;_start&#8221;.<\/p>\n<p>I think you have to compile without &#8220;std library&#8221;. but than you wont have printf,getchar and all the other useful stuff.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:14:16. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing software for a Cortex-A8 processor and I have to write some ARM assembly code to access specific registers. I&#8217;m making use of the gnu compilers and related tool chains, these tools are installed on the processor board(Freescale i.MX515) with Ubuntu. I make a connection to it from my host PC(Windows) using WinSCP and [&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-1019","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1019","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=1019"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1019\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}