Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > GCC Question

Reply
Thread Tools

GCC Question

 
 
Giuseppe Longo
Guest
Posts: n/a
 
      09-09-2012
Hello guys,
i've a little problem with gcc.

I've created two project with eclipse, and the first project is used to compile the second.

I try to compile with the command:
gcc -I /prog1/src/ -L /prog1/src -o main main.c

But, i got this error: Undefined reference

Someone can help me?

Thanks
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      09-09-2012
On Sun, 9 Sep 2012 07:26:24 -0700 (PDT), Giuseppe Longo
<> wrote:

>Hello guys,
>i've a little problem with gcc.
>
>I've created two project with eclipse, and the first project is used to compile the second.
>
>I try to compile with the command:
>gcc -I /prog1/src/ -L /prog1/src -o main main.c
>
>But, i got this error: Undefined reference
>
>Someone can help me?


Not unless you are willing to supply some details. Start with the
complete text of the error message. If it is from the compiler, it
will identify the line in your source program. Provide that line and
the lines that define every token used in that line. If it is from
the linker, it will identify an external reference. Show us the
module that is supposed to define the function or object being
referenced.

--
Remove del for email
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      09-10-2012
On Sep 9, 3:26*pm, Giuseppe Longo <giuseppe...@gmail.com> wrote:
> Hello guys,
> i've a little problem with gcc.
>
> I've created two project with eclipse, and the first project is used to compile the second.
>
> I try to compile with the command:
> gcc -I /prog1/src/ -L /prog1/src -o main main.c
>
> But, i got this error: Undefined reference
>
> Someone can help me?
>
> Thanks


one of the references in your program is not defined
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      09-10-2012
Giuseppe Longo wrote:

> I've created two project with eclipse, and the first project is used to compile the second.
>
> I try to compile with the command:
> gcc -I /prog1/src/ -L /prog1/src -o main main.c
>
> But, i got this error: Undefined reference
>
> Someone can help me?


The problem is on line 42.

 
Reply With Quote
 
Angel
Guest
Posts: n/a
 
      09-10-2012
On 2012-09-09, Giuseppe Longo <> wrote:
> Hello guys,
> i've a little problem with gcc.
>
> I've created two project with eclipse, and the first project is used
> to compile the second.
>
> I try to compile with the command:
> gcc -I /prog1/src/ -L /prog1/src -o main main.c
>
> But, i got this error: Undefined reference
>
> Someone can help me?
>
> Thanks



You didn't provide nearly enough information for anyone to determine
what's going on. You should at the very least have copy-pasted the
exact error message which should tell you what's missing.

Most likely though, you forgot to tell gcc to use a certain library
while linking. For example, if you use the functions of the math
library (those defined in math.h), you should include the -lm option in
your command.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      09-10-2012
Angel <angel+> writes:

> On 2012-09-09, Giuseppe Longo <> wrote:
>> Hello guys,
>> i've a little problem with gcc.
>>
>> I've created two project with eclipse, and the first project is used
>> to compile the second.
>>
>> I try to compile with the command:
>> gcc -I /prog1/src/ -L /prog1/src -o main main.c
>>
>> But, i got this error: Undefined reference
>>
>> Someone can help me?
>>
>> Thanks

>
>
> You didn't provide nearly enough information for anyone to determine
> what's going on. You should at the very least have copy-pasted the
> exact error message which should tell you what's missing.
>
> Most likely though, you forgot to tell gcc to use a certain library
> while linking. For example, if you use the functions of the math
> library (those defined in math.h), you should include the -lm option in
> your command.


The other likely bug is you mis-spelled a function, for instance you
called Sin() instead of sin(), or you defined myfunction() but called
myfnction().
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-10-2012
Joe Pfeiffer <> writes:
> Angel <angel+> writes:
>> On 2012-09-09, Giuseppe Longo <> wrote:
>>> Hello guys,
>>> i've a little problem with gcc.
>>>
>>> I've created two project with eclipse, and the first project is used
>>> to compile the second.
>>>
>>> I try to compile with the command:
>>> gcc -I /prog1/src/ -L /prog1/src -o main main.c
>>>
>>> But, i got this error: Undefined reference

>>
>> You didn't provide nearly enough information for anyone to determine
>> what's going on. You should at the very least have copy-pasted the
>> exact error message which should tell you what's missing.
>>
>> Most likely though, you forgot to tell gcc to use a certain library
>> while linking. For example, if you use the functions of the math
>> library (those defined in math.h), you should include the -lm option in
>> your command.

>
> The other likely bug is you mis-spelled a function, for instance you
> called Sin() instead of sin(), or you defined myfunction() but called
> myfnction().


Yes, but that kind of error can be avoided (or at least detected
more easily) by #include'ing the proper headers for any functions
you call, and by invoking your compiler in a mode that causes it
to warn about calls to undeclared functions.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      09-10-2012
Giuseppe Longo <> writes:

> Hello guys,
> i've a little problem with gcc.
>
> I've created two project with eclipse, and the first project is used to compile the second.
>
> I try to compile with the command:
> gcc -I /prog1/src/ -L /prog1/src -o main main.c
>
> But, i got this error: Undefined reference
>
> Someone can help me?
>
> Thanks


Ah, I hadn't read your whole question carefully enough. What do you
mean when you say the first project is used to compile the second? In
particular, do you mean your first project creates some sort of .o file
that you're planning to use with the second one? The gcc command you
show up above only compiles main.c, and uses it and the standard
libraries to create the program main. If you need an object file from
your other project, it should say something like

gcc -I /prog1/src/ -L /prog1/src -o main main.c /prog1/src/otherfile.o

(incidentally, I hope what you gave isn't the actual command line --
putting your projects up at the top level of the filesystem hierarchy
would be really poor. Also, your -L will only help you if you made the
object code in the other project into a library like libotherfile.a)
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-10-2012
On 2012-09-09, Giuseppe Longo <> wrote:
> Hello guys,
> i've a little problem with gcc.
>
> I've created two project with eclipse, and the first project is used to compile the second.
>
> I try to compile with the command:
> gcc -I /prog1/src/ -L /prog1/src -o main main.c


I would guess that prog1/src contains some dependent files containing
code which is needed by main.c.

The above command line provides the search paths for include files
with -I so that the compilation (just up to the linking stage) can succeed:
main.c is able to find the #include files.

The command line also provides the library search path with -L. However,
final linking to make the main executable will stil fail because a library
search path is not enough to bring in the dependent code. Libraries also have
to be specified on the compiler command line. Source files do not specify
which libraries they need to link with. (There is no such thing in the standrad
C language, though some systems, like Microsoft's compiler, have an extension
for expressing, in a C source file, what library it needs.)

If the source files under prog1/src have been linked to form library, such
as for example "libprog1.a", then you can just add "-lprog1" to the
command line. The argument -l<name> will cause the toolchain to look for
a library called "lib<name>.a", and one of the places in which it will
look is the directory specified by -L.

gcc -I /prog1/src/ -L /prog1/src -o main main.c -lprog1

If prog1/src contains only object files, then the -L argument is useless.
You have to bring all those object files into the command line:

gcc -I /prog1/src/ -o main main.c prog1/src/foo.o prog1/src/bar.o ...

It may be possible to use a wildcard:

gcc -I /prog1/src/ -o main main.c prog1/src/*.o

but that's rather playing it loose. What if not all the .o files are
needed by main.c. Also, what if some are missing (havent' bee compiled?).
"File prog1/src/foo.o not found" is a better diagnostic than unresolved symbols.

Anyway, since you're using Eclipse, maybe Eclipse has a way to handle
dependent projects? Why use an interactive development environment,
but link programs with manual command lines?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gcc 3.4.X to Gcc 4.1.X upgrading kas C++ 1 04-22-2010 08:56 PM
GCC 3.4.3 and GCC 4.1.2 ashnin C++ 1 07-07-2008 01:10 PM
Template construction in old gcc 3.3.3 does not compile in gcc 3.4.4 eknecronzontas@yahoo.com C++ 5 09-17-2005 12:27 AM
gcc 2.95 and gcc 3.2 gouqizi.lvcha@gmail.com C++ 8 03-16-2005 02:34 AM
C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1 Kevin P. Fleming C Programming 2 11-06-2003 05:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57