Simple Compilation question

A

Ashik

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:askForHelp.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.
 
S

santosh

Ashik said:
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:askForHelp.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.
 
F

Fred

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:askForHelp.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top