GCC Question

G

Giuseppe Longo

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
 
B

Barry Schwarz

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.
 
N

Nick Keighley

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
 
N

Noob

Giuseppe said:
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.
 
A

Angel

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.
 
J

Joe Pfeiffer

Angel said:
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().
 
K

Keith Thompson

Joe Pfeiffer said:
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.
 
J

Joe Pfeiffer

Giuseppe Longo said:
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)
 
K

Kaz Kylheku

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top