473,508 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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: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.
Jun 27 '08 #1
3 2290
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: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.

Jun 27 '08 #2
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: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.
--
Fred Kleinschmidt
Jun 27 '08 #3
On 2008-06-12, santosh <sa*********@gmail.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: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.
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
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6035
by: Lili | last post by:
I'm having problems creating a simple stored procedure in DB2. Can someone help? Here is the screen dump when I tried to load the stored procedure. Thanks for any help. Create procedure...
10
4381
by: Brennan Young | last post by:
Hi there, First I'll apologise for my ignorance! I have been attempting to compile some code that I found on the net. AFAICT it's intended to be portable code (it was available as source and...
3
2222
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using...
8
2156
by: Jeff | last post by:
I'm new to Visual Web 2005 using VB: ....trying to get something like the below to run. Apparently, I need something other than the eval function, but what? Thanks in advance Jeff
14
333
by: viv342 | last post by:
i am a beginner in c,c++.i wanted to know that what is the benifit of declaring the prototype of a function earlier and defining it later rather than defining it earlier
35
2983
by: mwelsh1118 | last post by:
Why doesn't C# allow incremental compilation like Java? Specifically, in Java I can compile single .java files in isolation. The resulting individual .class files can be grouped into .jar files....
4
1345
by: =?Utf-8?B?SmFtZXMgUGFnZQ==?= | last post by:
Am i missing somthing simple? I can't access any of the profile names in my code behind i.e; textBox1.text = Profile.test = "some text" here's the web.config: <?xml version="1.0"?> ...
2
1338
by: liorjj | last post by:
Hi, I'm new in this area so just a very simple question, I have declared typedef in the header file says (header.h), and some functions in this header as well; My problem is that when i...
6
1716
by: ebony.soft | last post by:
Dear all Hi I encountered a simple but IMO important problem about the C++ linkage model and One Definition Rule. Why the following code link? file1.cpp int x; file2.cpp
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.