Simple Compilation question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ashik

    Simple Compilation question

    I have a simple compilation question, but I haven't been able to find
    an answer for it anywhere online. I've made a simplified version of
    my problem using a few files. The main problem is that when I try
    compiling my code using gcc in cygwin, I get this error:

    $ make
    gcc -L C:/temp/ -o askForHelp askForHelp.c
    /cygdrive/c/DOCUME~1/Ashik/LOCALS~1/Temp/ccB46qsl.o:askF orHelp.c:(.text
    +0x45): undefined reference to `_helper'
    collect2: ld returned 1 exit status
    make: *** [all] Error 1

    This is what my files look like:
    ***askForHelp.c ***
    #include <stdio.h>
    #include "helper.h"

    int main(int argc, char **arvg) {
    int i = 0;
    for (i = 0; i < 10; i++) {
    printf("%d", helper(i));
    }
    }


    ***helper.h***
    #ifndef HELPER_H
    #define HELPER_H

    int helper(int input);

    #endif

    ***helper.c***
    #include <stdio.h>

    int helper(int input) {
    return input+1;
    }

    I don't have that much C compilation background. Please help. Thank
    you.
  • santosh

    #2
    Re: Simple Compilation question

    Ashik wrote:
    I have a simple compilation question, but I haven't been able to find
    an answer for it anywhere online. I've made a simplified version of
    my problem using a few files. The main problem is that when I try
    compiling my code using gcc in cygwin, I get this error:
    >
    $ make
    gcc -L C:/temp/ -o askForHelp askForHelp.c
    /cygdrive/c/DOCUME~1/Ashik/LOCALS~1/Temp/ccB46qsl.o:askF orHelp.c
    (.text
    +0x45): undefined reference to `_helper'
    collect2: ld returned 1 exit status
    make: *** [all] Error 1
    >
    This is what my files look like:
    ***askForHelp.c ***
    #include <stdio.h>
    #include "helper.h"
    >
    int main(int argc, char **arvg) {
    int i = 0;
    for (i = 0; i < 10; i++) {
    printf("%d", helper(i));
    }
    }
    >
    >
    ***helper.h***
    #ifndef HELPER_H
    #define HELPER_H
    >
    int helper(int input);
    >
    #endif
    >
    ***helper.c***
    #include <stdio.h>
    >
    int helper(int input) {
    return input+1;
    }
    >
    I don't have that much C compilation background. Please help. Thank
    you.
    The error you are getting is a "linker error". It is complaining that it
    cannot find the symbol _helper in any of the places it is searching in.
    You need to compiler helper.c to helper.o and include helper.o in the
    command that compiles askForHelp.c to the final executable.

    Something like:

    gcc -Wall -W -ansi -pedantic -o askForHelp.exe askForHelp.c helper.o

    You could also include helper.c instead of helper.o in the above
    command, in which case the compiler will automaticall compile helper.c
    to it's object file before linking all the object files together.

    Comment

    • Fred

      #3
      Re: Simple Compilation question

      On Jun 12, 11:06 am, Ashik <AshikManand... @gmail.comwrote :
      I have a simple compilation question, but I haven't been able to find
      an answer for it anywhere online.  I've made a simplified version of
      my problem using a few files.  The main problem is that when I try
      compiling my code using gcc in cygwin, I get this error:
      >
      $ make
      gcc -L C:/temp/ -o askForHelp askForHelp.c
      /cygdrive/c/DOCUME~1/Ashik/LOCALS~1/Temp/ccB46qsl.o:askF orHelp.c:(.text
      +0x45): undefined reference to `_helper'
      collect2: ld returned 1 exit status
      make: *** [all] Error 1
      >
      This is what my files look like:
      ***askForHelp.c ***
      #include <stdio.h>
      #include "helper.h"
      >
      int main(int argc, char **arvg) {
              int i = 0;
              for (i = 0; i < 10; i++) {
                      printf("%d", helper(i));
              }
      >
      }
      >
      ***helper.h***
      #ifndef HELPER_H
      #define HELPER_H
      >
      int helper(int input);
      >
      #endif
      >
      ***helper.c***
      #include <stdio.h>
      >
      int helper(int input) {
              return input+1;
      >
      }
      >
      I don't have that much C compilation background.  Please help.  Thank
      you.

      You have not told the linker to link in the helper module, nor have
      you told it where to find that module.
      --
      Fred Kleinschmidt

      Comment

      • A. Bolmarcich

        #4
        Re: Simple Compilation question

        On 2008-06-12, santosh <santosh.k83@gm ail.comwrote:
        Ashik wrote:
        >
        >I have a simple compilation question, but I haven't been able to find
        >an answer for it anywhere online. I've made a simplified version of
        >my problem using a few files. The main problem is that when I try
        >compiling my code using gcc in cygwin, I get this error:
        >>
        >$ make
        >gcc -L C:/temp/ -o askForHelp askForHelp.c
        >/cygdrive/c/DOCUME~1/Ashik/LOCALS~1/Temp/ccB46qsl.o:askF orHelp.c
        (.text
        >+0x45): undefined reference to `_helper'
        >collect2: ld returned 1 exit status
        >make: *** [all] Error 1
        >>
        >This is what my files look like:
        >***askForHelp. c***
        >#include <stdio.h>
        >#include "helper.h"
        >>
        >int main(int argc, char **arvg) {
        >int i = 0;
        >for (i = 0; i < 10; i++) {
        >printf("%d", helper(i));
        >}
        >}
        >>
        >>
        >***helper.h* **
        >#ifndef HELPER_H
        >#define HELPER_H
        >>
        >int helper(int input);
        >>
        >#endif
        >>
        >***helper.c* **
        >#include <stdio.h>
        >>
        >int helper(int input) {
        >return input+1;
        >}
        >>
        >I don't have that much C compilation background. Please help. Thank
        >you.
        >
        The error you are getting is a "linker error". It is complaining that it
        cannot find the symbol _helper in any of the places it is searching in.
        You need to compiler helper.c to helper.o and include helper.o in the
        command that compiles askForHelp.c to the final executable.
        >
        Something like:
        >
        gcc -Wall -W -ansi -pedantic -o askForHelp.exe askForHelp.c helper.o
        >
        You could also include helper.c instead of helper.o in the above
        command, in which case the compiler will automaticall compile helper.c
        to it's object file before linking all the object files together.
        Because the make command is being used, another approach is to have the
        Makefile contain the line

        askForHelp: askForHelp.o helper.o

        and have the make command build askForHelp.

        With the example given, the default rules used by make should compile
        the two .c files to .o files and then link the .o files to create the
        executable.

        The Makefile should also include the information that the .c files
        depend on helper.h, say by including lines like

        askForHelp.o: helper.h
        helper.o: helper.h

        Comment

        Working...