public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: undefined reference to `foo()'
@ 2006-12-11 21:55 Gengis Kanhg Toledo Ramírez
  2006-12-11 22:10 ` John Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: Gengis Kanhg Toledo Ramírez @ 2006-12-11 21:55 UTC (permalink / raw)
  To: gcc-help; +Cc: eljay

Thanks, it works.

The simple example was only a sample from the real
problem I have.
I have 10 .cc files and other amount of .h files, I
realize that order 
of files is important at the g   command. In my OOP
project I have some 
files as my own libraries used in several others
files, then it is not 
clear for me the order to be followed.

What is the order criteria?
Where can I get information about?

Thanks you very much.


John Love-Jensen escribió:
> Hi Ramirez,
> 
> Try this:
> 
> g   main.cc foo.cc -o prueba
> 
> HTH,
> --Eljay
> 
> 
> 

The problem:

$ more *.cc *.h
::::::::::::::
foo.cc
::::::::::::::
#include "foo.h"

int foo()
{
     return 42;
}
::::::::::::::
main.cc
::::::::::::::
#include "foo.h"

int main()
{
     return foo();
}
::::::::::::::
foo.h
::::::::::::::
int foo();

$ g   ./main.cc -o prueba
/home/<myname>/tmp/ccEOfejM.o(.text 0x11): In function
`main':
: undefined reference to `foo()'
collect2: ld returned 1 exit status


 
____________________________________________________________________________________
Cheap talk?
Check out Yahoo! Messenger's low PC-to-Phone call rates.
http://voice.yahoo.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `foo()'
  2006-12-11 21:55 undefined reference to `foo()' Gengis Kanhg Toledo Ramírez
@ 2006-12-11 22:10 ` John Love-Jensen
  0 siblings, 0 replies; 6+ messages in thread
From: John Love-Jensen @ 2006-12-11 22:10 UTC (permalink / raw)
  To: Ramírez Gengis Kanhg Toledo, MSX to GCC

Hi Ramirez,

> What is the order criteria?

The GCC tool chain driver processes items left to right.

Object files (or source files which compile to object files) are always
taken in their entirety.

Archive libraries only bring in the objects from within the archive that
satisfy missing symbols.

If archive libfoo.a has undefined symbols that are satisfied in libbar.a,
then libbar.a needs to appear after libfoo.a.  Regardless if they are
specified via -lfoo -lbar, or libfoo.a libbar.a or -lfoo libbar.a or
libfoo.a -libbar.a.

Shared object libraries are brought in as SSO (static shared object) as
specified on the command line.  An SSO may have its own dependencies.  NOTE:
if you bring in a shared object library programmatically, that's called a
DSO (dynamic shared object).

There are some situations where there is both a libfoo.a and libfoo.so.  If
left to -lfoo to resolve, the one that is brought in is dependent on your
particular platform and if you overrode the platform bias.

Note:  I'm using "libfoo.a" and "libfoo.so", but you may have a platform
like the Amiga that uses "foo.a" and "foo.library", or Windows which uses
"foo.lib" and "foo.dll", or OS X which uses "libfoo.a" and "libfoo.dylib".
So your mileage may vary.

The rule of thumb is:
specify all your .o files first (in any order)
specify all your .a and .so libraries in their dependency order

And if you are running into a circular dependency between libfoo.a and
libbar.a, you'll have to do something utterly horrible like...

g++ -o myapp one.o two.o three.o -lfoo -lbar -lfoo

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `foo()'
  2006-12-14  2:05 Gengis Kanhg Toledo Ramírez
@ 2006-12-14 12:58 ` John Love-Jensen
  0 siblings, 0 replies; 6+ messages in thread
From: John Love-Jensen @ 2006-12-14 12:58 UTC (permalink / raw)
  To: Ramírez Gengis Kanhg Toledo, MSX to GCC

Hi Ramirez,

I recommend you look for where the missing symbol is located (which .o or .a
or .so), and then investigate why that symbol is not available to the
linker.

The 'nm' command is helpful to see what symbols are in what files.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `foo()'
@ 2006-12-14  2:05 Gengis Kanhg Toledo Ramírez
  2006-12-14 12:58 ` John Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: Gengis Kanhg Toledo Ramírez @ 2006-12-14  2:05 UTC (permalink / raw)
  To: gcc-help

I am working recycling a project of my own than works.
The new project 
is similar in 60% of the old one. Several classes
files are used with 
out any change. Other ones have been created and
proved.

Note.
I am using PNGwriter library with freetype options (in
old and new 
projects)

I only have *.c and *.h files extensions in my
proyect.



With the old proyect I used for compilation:

$g   ./prueba.cc -o prueba `freetype-config --cflags` 
-I/usr/local/include -L/usr/local/lib -lpng
-lpngwriter -lz -lfreetype


The same I am using to the new one until de "undefined
reference to 
<function name> problem appears.


I am trying with your ideas, but I can not solve the
problem.

I proved with
$gcc -c *.cc `freetype-config --cflags`


$gcc <list of *.o> -o prueba `freetype-config
--cflags` 
-I/usr/local/include -L/usr/local/lib -lpng
-lpngwriter -lz -lfreetype


Compilation works, but linking no. The same error
messagge from the 
direct command.


I was looking solutions from google, google groups, I
was commented 
several parts of the crucial files but anything works.
I prove with the 
simple main.cc, foo.c and foo.h example and I realize
the same problem 
is given so the problem is complex for me.

Why the solution should be change the compilation
command? Why if it 
works before?
Is it possible than my compiler files were damaged?
Why similar projects have different behavior?


Thanks for your help.


John Love-Jensen escribió:
 > Hi Ramirez,
 >
 >> What is the order criteria?
 >
 > The GCC tool chain driver processes items left to
right.
 >
 > Object files (or source files which compile to
object files) are always
 > taken in their entirety.
 >
 > Archive libraries only bring in the objects from
within the archive that
 > satisfy missing symbols.
 >
 > If archive libfoo.a has undefined symbols that are
satisfied in libbar.a,
 > then libbar.a needs to appear after libfoo.a. 
Regardless if they are
 > specified via -lfoo -lbar, or libfoo.a libbar.a or
-lfoo libbar.a or
 > libfoo.a -libbar.a.
 >
 > Shared object libraries are brought in as SSO
(static shared object) as
 > specified on the command line.  An SSO may have its
own dependencies. 
  NOTE:
 > if you bring in a shared object library
programmatically, that's called a
 > DSO (dynamic shared object).
 >
 > There are some situations where there is both a
libfoo.a and 
libfoo.so.  If
 > left to -lfoo to resolve, the one that is brought
in is dependent on your
 > particular platform and if you overrode the
platform bias.
 >
 > Note:  I'm using "libfoo.a" and "libfoo.so", but
you may have a platform
 > like the Amiga that uses "foo.a" and "foo.library",
or Windows which uses
 > "foo.lib" and "foo.dll", or OS X which uses
"libfoo.a" and 
"libfoo.dylib".
 > So your mileage may vary.
 >
 > The rule of thumb is:
 > specify all your .o files first (in any order)
 > specify all your .a and .so libraries in their
dependency order
 >
 > And if you are running into a circular dependency
between libfoo.a and
 > libbar.a, you'll have to do something utterly
horrible like...
 >
 > g   -o myapp one.o two.o three.o -lfoo -lbar -lfoo
 >
 > HTH,
 > --Eljay
 >
 >
 >

Gengis Kanhg Toledo Ramirez escribió:
> Thanks, it works.
> 
> The simple example was only a sample from the real
problem I have.
> I have 10 .cc files and other amount of .h files, I
realize that order 
> of files is important at the g   command. In my OOP
project I have some 
> files as my own libraries used in several others
files, then it is not 
> clear for me the order to be followed.
> 
> What is the order criteria?
> Where can I get information about?
> 
> Thanks you very much.
> 
> 
> John Love-Jensen escribió:
>> Hi Ramirez,
>>
>> Try this:
>>
>> g   main.cc foo.cc -o prueba
>>
>> HTH,
>> --Eljay
>>
>>
>>
> 
> The problem:
> 
> $ more *.cc *.h
> ::::::::::::::
> foo.cc
> ::::::::::::::
> #include "foo.h"
> 
> int foo()
> {
>     return 42;
> }
> ::::::::::::::
> main.cc
> ::::::::::::::
> #include "foo.h"
> 
> int main()
> {
>     return foo();
> }
> ::::::::::::::
> foo.h
> ::::::::::::::
> int foo();
> 
> $ g   ./main.cc -o prueba
> /home/<myname>/tmp/ccEOfejM.o(.text 0x11): In
function `main':
> : undefined reference to `foo()'
> collect2: ld returned 1 exit status
> 


 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `foo()'
  2006-12-11 18:38 Gengis Kanhg Toledo Ramírez
@ 2006-12-11 19:29 ` John Love-Jensen
  0 siblings, 0 replies; 6+ messages in thread
From: John Love-Jensen @ 2006-12-11 19:29 UTC (permalink / raw)
  To: Ramírez Gengis Kanhg Toledo, MSX to GCC

Hi Ramirez,

Try this:

g++ main.cc foo.cc -o prueba

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* undefined reference to `foo()'
@ 2006-12-11 18:38 Gengis Kanhg Toledo Ramírez
  2006-12-11 19:29 ` John Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: Gengis Kanhg Toledo Ramírez @ 2006-12-11 18:38 UTC (permalink / raw)
  To: gcc-help

$ more *.cc *.h
::::::::::::::
foo.cc
::::::::::::::
#include "foo.h"

int foo()
{
     return 42;
}
::::::::::::::
main.cc
::::::::::::::
#include "foo.h"

int main()
{
     return foo();
}
::::::::::::::
foo.h
::::::::::::::
int foo();

$ g   ./main.cc -o prueba
/home/<myname>/tmp/ccEOfejM.o(.text 0x11): In function
`main':
: undefined reference to `foo()'
collect2: ld returned 1 exit status


 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-12-14 12:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-11 21:55 undefined reference to `foo()' Gengis Kanhg Toledo Ramírez
2006-12-11 22:10 ` John Love-Jensen
  -- strict thread matches above, loose matches on Subject: below --
2006-12-14  2:05 Gengis Kanhg Toledo Ramírez
2006-12-14 12:58 ` John Love-Jensen
2006-12-11 18:38 Gengis Kanhg Toledo Ramírez
2006-12-11 19:29 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).