GCC Question

Discussion in 'C Programming' started by Giuseppe Longo, Sep 9, 2012.

  1. 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
    Giuseppe Longo, Sep 9, 2012
    #1
    1. Advertising

  2. 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
    Barry Schwarz, Sep 9, 2012
    #2
    1. Advertising

  3. On Sep 9, 3:26 pm, 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


    one of the references in your program is not defined
    Nick Keighley, Sep 10, 2012
    #3
  4. Giuseppe Longo

    Noob Guest

    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.
    Noob, Sep 10, 2012
    #4
  5. Giuseppe Longo

    Angel Guest

    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
    Angel, Sep 10, 2012
    #5
  6. Giuseppe Longo

    Joe Pfeiffer Guest

    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().
    Joe Pfeiffer, Sep 10, 2012
    #6
  7. Joe Pfeiffer <> writes:
    > 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) <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"
    Keith Thompson, Sep 10, 2012
    #7
  8. Giuseppe Longo

    Joe Pfeiffer Guest

    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)
    Joe Pfeiffer, Sep 10, 2012
    #8
  9. Giuseppe Longo

    Kaz Kylheku Guest

    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?
    Kaz Kylheku, Sep 10, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    426
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    641
    Kevin P. Fleming
    Nov 6, 2003
  3. Replies:
    5
    Views:
    356
    Nathan Addy
    Sep 17, 2005
  4. ashnin

    GCC 3.4.3 and GCC 4.1.2

    ashnin, Jul 7, 2008, in forum: C++
    Replies:
    1
    Views:
    515
    Michael DOUBEZ
    Jul 7, 2008
  5. kas
    Replies:
    1
    Views:
    315
    red floyd
    Apr 22, 2010
Loading...

Share This Page