{"id":1139,"date":"2022-08-30T15:13:02","date_gmt":"2022-08-30T15:13:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/linux-linking-assembly-with-gcc-gives-many-errors-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:02","modified_gmt":"2022-08-30T15:13:02","slug":"linux-linking-assembly-with-gcc-gives-many-errors-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/linux-linking-assembly-with-gcc-gives-many-errors-collection-of-common-programming-errors\/","title":{"rendered":"linux linking assembly with gcc gives many errors-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to compile and link a simple &#8220;hello, world!&#8221; program with GCC. This program uses the &#8220;printf&#8221; C function. The problem that I am having is that the terminal throws back multiple errors. I am running Archlinux, compiling with NASM, linking with GCC. Here is my code:<\/p>\n<pre><code>; ----------------------------------------------------------------------------\n; helloworld.asm\n; \n; Compile: nasm -f elf32 helloworld.asm\n; Link: gcc helloworld.o\n; ----------------------------------------------------------------------------\nSECTION .data\n    message db \"Hello, World\",0\nSECTION .text\n    global  main\n    extern  printf\n\n    section .text\n_main:\n    push    message\n    call    printf\n    add     esp, 4\n    ret\n<\/code><\/pre>\n<p>The errors that I receive are as follows:<\/p>\n<pre><code>\/usr\/bin\/ld: skipping incompatible \/usr\/lib\/gcc\/x86_64-unknown-linux-gnu\/4.7.2\/libgcc.a     when searching for -lgcc\n\/usr\/bin\/ld: cannot find -lgcc\ncollect2: error: ld returned 1 exit status\n<\/code><\/pre>\n<p>Can someone tell me what is causing these errors and what I need to do to fix them?<\/p>\n<p>Thanks in advance,<\/p>\n<p>RileyH<\/p>\n<ol>\n<li>\n<p>For such things, you should first understand what exactly <code>gcc<\/code> is doing. So use<\/p>\n<pre><code> gcc -v helloworld.o -o helloworld\n<\/code><\/pre>\n<p>and what is happenning is that you have a 64 bits Linux and linking a 32 bits object in it. So try with<\/p>\n<pre><code> gcc -m32 -v helloworld.o -o helloworld\n<\/code><\/pre>\n<p>But I think that you should avoid coding assembly today (optimizing compilers do a better work than you can reasonably do). If you absolutely need a few assembly instructions, put some <code>asm<\/code> in your C code.<\/p>\n<p>BTW, you could compile with <code>gcc -fverbose-asm -O -wall -S helloworld.c<\/code> and look inside the generated <code>helloworld.s<\/code>; and you could also pass <code>.s<\/code> files to <code>gcc<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:31:51. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to compile and link a simple &#8220;hello, world!&#8221; program with GCC. This program uses the &#8220;printf&#8221; C function. The problem that I am having is that the terminal throws back multiple errors. I am running Archlinux, compiling with NASM, linking with GCC. Here is my code: ; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- ; helloworld.asm ; ; [&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-1139","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1139","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=1139"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1139\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}