Results 1 to 4 of 4

Thread: compiling with gcc and "undefined reference to xxx"

  1. #1
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    compiling with gcc and "undefined reference to xxx"

    I have run up against a wall. I really appreciate any and all help because I am so out of my depth.

    I have installed gcc on a NSLU2.
    Running gcc -o test test.c brings no error and afterwards I can run the program test with ./test.

    test.c
    #include <stdio.h>
    main()
    {
    printf("Linuxquestions.org\n");
    }


    so it seems I did something correct.

    But when I try to compile the file that I really want to use "vclient.c" I get an error message
    /tmp/ccuuPxtN.o(.text+0x3bc): In function `main': vclient.c: undefined reference to `initLog'

    Now as I may have mentioned I am a long way out of my depth but I looked in the source code and found
    #include "common.h"

    common.h is in the directory and is obviously being found because if I add another m to make it commmon.h then the compiler complains that it can not find it.

    When I look in common.h I find
    int initLog(int useSyslog, char *logfile,int debugSwitch);

    and looking in common.c (also in the same directory) I find
    int initLog(int useSyslog, char *logfile,int debugSwitch) {
    /* oeffnet bei Bedarf syslog oder log-Datei */
    xxxxxxxxxxxxxxxxxx
    }

    where xxxx is code.

    So to me everything looks OK!
    But obviously I am missing something fundamental.

    As I wrote, I really would appreciate any help.
    Thank you
    Neill
    I am a newbie. I have been for many years now. Sometimes I feel that I understand things, turn the corner and find that there is even more to understand.

  2. #2
    Join Date
    May 2006
    Beans
    1,790

    Re: compiling with gcc and "undefined reference to xxx"

    Quote Originally Posted by NeillHog View Post
    I have run up against a wall. I really appreciate any and all help because I am so out of my depth.

    I have installed gcc on a NSLU2.
    Running gcc -o test test.c brings no error and afterwards I can run the program test with ./test.

    test.c
    #include <stdio.h>
    main()
    {
    printf("Linuxquestions.org\n");
    }


    so it seems I did something correct.

    But when I try to compile the file that I really want to use "vclient.c" I get an error message
    /tmp/ccuuPxtN.o(.text+0x3bc): In function `main': vclient.c: undefined reference to `initLog'

    Now as I may have mentioned I am a long way out of my depth but I looked in the source code and found
    #include "common.h"

    common.h is in the directory and is obviously being found because if I add another m to make it commmon.h then the compiler complains that it can not find it.

    When I look in common.h I find
    int initLog(int useSyslog, char *logfile,int debugSwitch);

    and looking in common.c (also in the same directory) I find
    int initLog(int useSyslog, char *logfile,int debugSwitch) {
    /* oeffnet bei Bedarf syslog oder log-Datei */
    xxxxxxxxxxxxxxxxxx
    }

    where xxxx is code.

    So to me everything looks OK!
    But obviously I am missing something fundamental.

    As I wrote, I really would appreciate any help.
    Thank you
    Neill
    What are the compilation and linking commands you do to produce vclient?

  3. #3
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: compiling with gcc and "undefined reference to xxx"

    creating an executable is, simply speaking, a two stage process, compile the source code to object files and link the object files together to form an executable
    undefined reference error is a linking error
    the compiled vclient.c needs to be linked with the compiled symbols from common.c

    gcc vclient.c common.c -o output
    which is short for:
    #compile
    gcc -c vclient.c -o vclient.o
    #compile
    gcc -c common.c -o common.o
    #link
    gcc vclient.o common.o -o output

    should solve this problem.
    Last edited by MadCow108; February 14th, 2011 at 10:03 PM.

  4. #4
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: compiling with gcc and "undefined reference to xxx"

    Thank you MadCow108.
    I mess about for weeks and you solve the problem in 5 minutes.
    If you are ever passing through the Alps I owe you a huge beer.
    Thank you!
    I am a newbie. I have been for many years now. Sometimes I feel that I understand things, turn the corner and find that there is even more to understand.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •