473,835 Members | 1,781 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: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.
Jun 27 '08 #1
3 2320
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.

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

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

Similar topics

5
6057
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 update_salary (in emp_number char(6), in rate integer) language sql begin update employee
10
4420
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 as a DOS binary, but mention was made of UNIX in the readme - I am compiling under Mac OS X, fwiw).
3
2239
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 namespace std; ...... vector <Vector> vectorlist;
8
2171
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
3061
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. In C#, there appears to be no analog. I have to compile all my .cs files into a single .dll. This has serious drawbacks in terms of compilation. With Eclipse, I change a file and only that file is re-compiled. With Visual Studio, I
4
1360
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"?> <configuration>
2
1355
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 declare in one of the methods in the cpp file a variable of this typedef I get the "undeclared identifier" compilation error.
6
1730
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
10807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9343
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7765
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6961
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5632
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4430
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 we have to send another system
2
3990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3086
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.