473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Undefined reference

I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.

I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.

I've pared down the code to a simple example:

---------------------------------square.c
---------------------------------------------------

#include "square.h"

main() {

static int square (int a)
{
return a * a;
}

int sq_plus (int a, int b)
{
return square(a) - b;
}

} /* end main */

-----------------------------------------------------
square.h---------------------------------

#include <stdio.h>

int sq_plus(int a, int b);

----------------------------------------
main.c----------------------------------------------------
#include <stdio.h>
#include "square.h"

int main()
{
int a,b;

printf( "Enter two digits: " );
scanf( "%d%d", &a, &b );
printf( "Given %d and %d, squarePlus is %d", a, b,
square(a,b) );
return 0;
}

------------------------------------------------------------------------------------------------

Some questions:

(1) I thought that main() was only supposed to be in the main function
file, but if I don't have a main() in square.c, I get errors.

(2) I don't understand why I'm getting the undefined reference error.
I'm using the Dev compiler and it seems that it is ANSI-compatible.
There were supposedly issues with non-ANSI-compatible compilers, but
that doesn't seem to be the issue. The problem may be as described in
this FAS:

"In the general case of calling code in an external library, using
#include to pull in the right header file(s) is only half of the
story; you also have to tell the linker to search the external library
itself. The declarations in the header file only tell the compiler how
to call the external functions; the header file doesn't supply the
definitions of the external functions, or tell the compiler/linker
where to find those definitions.

In some cases (especially if the functions are nonstandard) obtaining
those definitions may require explicitly asking for the correct
libraries to be searched when you link the program. (Some systems may
be able to arrange that whenever you #include a header, its associated
library, if nonstandard, is automatically requested at link time, but
such a facility is not widespread.)"

If that is the solution to my problem, I'm still confused on how to
"tell the linker to search the external library itself. "

(3) Does the "ld returned 1 exit status" error go away when the
undefined reference error is solved as I'm assuing it does?

Apr 5 '07 #1
8 5594

The line in main.c should actually read:

printf( "Given %d and %d, squarePlus is %d", a, b,
sq_plus(a,b) );

instead of

printf( "Given %d and %d, squarePlus is %d", a, b,
square(a,b) );

Apr 5 '07 #2
wd****@gmail.com wrote:
I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.

I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.
You'll have to read your compiler documentation to see how to compile
more than one source module into a single executable. Generally,

$CC=your compiler

$CC main.c square.c

should be enough to get you going.

--
Ian Collins.
Apr 5 '07 #3
On Apr 5, 6:26 pm, Ian Collins <ian-n...@hotmail.comwrote:
wdh...@gmail.com wrote:
I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.
I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.

You'll have to read your compiler documentation to see how to compile
more than one source module into a single executable. Generally,

$CC=your compiler

$CC main.c square.c

should be enough to get you going.

--
Ian Collins.
I'm using the Dev-C++ IDE. I'm just pressing the "Compile" button on
there. Shouldn't the IDE know what to do if it's compiling a Main
file?

Sice it's not command line compilation, I thought it would know what
to do.
Apr 6 '07 #4
wd****@gmail.com wrote:
On Apr 5, 6:26 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>wdh...@gmail.com wrote:
>>>I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.
>>>I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.

You'll have to read your compiler documentation to see how to compile
more than one source module into a single executable. Generally,

$CC=your compiler

$CC main.c square.c

should be enough to get you going.
*Please don't quote signatures.
>
I'm using the Dev-C++ IDE. I'm just pressing the "Compile" button on
there. Shouldn't the IDE know what to do if it's compiling a Main
file?
Can't help you there, you had better ask somewhere where the IDE (or
your platform) is topical.

--
Ian Collins.
Apr 6 '07 #5
Can't help you there, you had better ask somewhere where the IDE (or
your platform) is topical.

--
Ian Collins.
I had gcc'd through telnet and gotten the same error, so I figured it
was an error in what I'd coded in general, and not a compiler-specific
issue.

Apr 6 '07 #6
On 5 Apr 2007 16:09:42 -0700, wd****@gmail.com wrote:
>I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.

I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.

I've pared down the code to a simple example:

---------------------------------square.c
---------------------------------------------------

#include "square.h"

main() {
Delete this and its corresponding } because ...
>
static int square (int a)
{
You are not allowed to define one function within another. Did your
compiler not issue a diagnostic here.
return a * a;
}

int sq_plus (int a, int b)
{
return square(a) - b;
}

} /* end main */

-----------------------------------------------------
square.h---------------------------------

#include <stdio.h>
Why? Neither function in square.c needs it and you manually include
it in main.c
>
int sq_plus(int a, int b);

----------------------------------------
main.c----------------------------------------------------
#include <stdio.h>
#include "square.h"

int main()
int main(void) is more complete.
{
int a,b;

printf( "Enter two digits: " );
scanf( "%d%d", &a, &b );
printf( "Given %d and %d, squarePlus is %d", a, b,
square(a,b) );
square is an unknown identifier at this point. square.h declares
sq_plus but not square.
return 0;
}

------------------------------------------------------------------------------------------------

Some questions:

(1) I thought that main() was only supposed to be in the main function
file, but if I don't have a main() in square.c, I get errors.
It doesn't matter where it is defined but there should be only one
main.

What errors? Show the exact code and diagnostic.
>
(2) I don't understand why I'm getting the undefined reference error.
I'm using the Dev compiler and it seems that it is ANSI-compatible.
There were supposedly issues with non-ANSI-compatible compilers, but
that doesn't seem to be the issue. The problem may be as described in
this FAS:

"In the general case of calling code in an external library, using
#include to pull in the right header file(s) is only half of the
story; you also have to tell the linker to search the external library
itself. The declarations in the header file only tell the compiler how
to call the external functions; the header file doesn't supply the
definitions of the external functions, or tell the compiler/linker
where to find those definitions.

In some cases (especially if the functions are nonstandard) obtaining
those definitions may require explicitly asking for the correct
libraries to be searched when you link the program. (Some systems may
be able to arrange that whenever you #include a header, its associated
library, if nonstandard, is automatically requested at link time, but
such a facility is not widespread.)"

If that is the solution to my problem, I'm still confused on how to
"tell the linker to search the external library itself. "
Since your square.c has major errors, there is no telling what is
actually in the object file the compiler builds from it.

How you tell your linker to find private object code is a detail of
your implementation. Check your documentation, help file, man page,
whatever.
>
(3) Does the "ld returned 1 exit status" error go away when the
undefined reference error is solved as I'm assuing it does?
Another detail of your implementation.
Remove del for email
Apr 6 '07 #7
At about the time of 4/5/2007 4:09 PM, wd****@gmail.com stated the
following:
I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.

I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.

I've pared down the code to a simple example:

---------------------------------square.c
---------------------------------------------------

#include "square.h"

main() {

static int square (int a)
{
return a * a;
}

int sq_plus (int a, int b)
{
return square(a) - b;
}

} /* end main */

-----------------------------------------------------
square.h---------------------------------

#include <stdio.h>

int sq_plus(int a, int b);

----------------------------------------
main.c----------------------------------------------------
#include <stdio.h>
#include "square.h"

int main()
{
int a,b;

printf( "Enter two digits: " );
scanf( "%d%d", &a, &b );
printf( "Given %d and %d, squarePlus is %d", a, b,
square(a,b) );
return 0;
}
Try this:

#include <stdio.h>

int square(int a);
int sq_plus(int a, int b);
int main(void)
{
int a, b;

printf("Enter two digits: ");
scanf("%d%d", &a, &b);
printf("Given %d and %d, squarePlus is %d\n", a, b, sq_plus(a, b));
return(0);
}

int square(int a)
{
return(a * a);
}

int sq_plus(int a, int b)
{
return(square(a) - b);
}

As for your specific environment, you will need to ask in a
platform/compiler specific forum.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Apr 6 '07 #8
<wd****@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.

I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.

I've pared down the code to a simple example:
That example is very, very broken. Try this:

/* square.h */
int sq_plus (int a, int b);

/* square.c */
#include "square.h"

static int square (int a) {
return a * a;
}

int sq_plus (int a, int b) {
return square(a) - b;
}

/* main.c */

#include <stdio.h>
#include "square.h"

int main() {
int a,b;

printf( "Enter two digits: " );
scanf( "%d%d", &a, &b );
printf( "Given %d and %d, squarePlus is %d", a, b, sq_plus(a,b) );
return 0;
}

/* end source files */

How you compile these files together is platform-specific.

<OT>If you're using GCC it'll go something like this:

gcc -ansi -pedantic -W -Wall -c square.c
gcc -ansi -pedantic -W -Wall -c main.c
gcc square.o main.o -o square

The -c option tells GCC not to link yet because you're compiling multiple
source files; it will produce a .o file for the .c file it's given. The
last line actually links the various .o files together (by calling ld,
usually); since the default output file is "a.out" for historical reasons,
you need the -o option to give the program a sensible name.</OT>
Some questions:

(1) I thought that main() was only supposed to be in the main function
file, but if I don't have a main() in square.c, I get errors.

(2) I don't understand why I'm getting the undefined reference error.
I'm using the Dev compiler and it seems that it is ANSI-compatible.
This has nothing to do with ANSI; your code is broken, and on top of that
you're not compiling/linking it correctly.
(3) Does the "ld returned 1 exit status" error go away when the
undefined reference error is solved as I'm assuing it does?
Yes.

BTW, why do you call the function "square plus" if you're _subtracting_ the
second argument? Shouldn't it be "square minus"?

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Apr 6 '07 #9

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

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
13
by: david | last post by:
Hi, I have some problems to link a simple hello world program using g++ (version 3.2.3 or 3.3) and dinkumware 402. //hallo world... #include <iostream> main () { std::cout << "bla" <<...
1
by: Codemutant | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I just cannot find what is undefined in this code.
1
by: Foolster41 | last post by:
I'm rather new to C++ programing. I'm using the dev-C++ program on a windows XP OS. I'm trying to compile the code for a multi user dungeon (MUD) called circle-mud. When I compile I get the...
1
by: Dom | last post by:
I'm new to c++. Just started learning it 24 hours ago. Am running into a compile problem. Please, no one waste the effort telling me to google it. I've been researching it for quite a while with no...
1
by: Andre Janssen | last post by:
Hi.... I tried to compile the following src with this command: "g++ -Wall -o bla alsaswitch.cpp". The src is an example src of xosd package. #include <xosd.h> int main (int argc, char...
3
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)':...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
3
by: prakash.mirji | last post by:
Hello, I am getting below mention linker error when I tried to link my class test.C I use below command to compile test.C /usr/bin/g++ -g -fpic -fvisibility=default -D_POSIX_SOURCE...
2
by: zqiang320 | last post by:
Hello: I execute make ,then get error: $ make Making all in libsbml/src make: Entering directory `/home/internet/mydoc/test_pj/libsbml/src' ........ /bin/sh ./libtool --tag=CC --mode=link...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.