Undefined symbol in gcc & c++

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Undefined symbol in gcc & c++

    Dear All,

    I built the following little C program into a library (libtt.a) with gcc:

    /* Filename: tt1.c */
    #include <stdio.h>
    int foo() {
    return 1;
    }

    gcc -c tt1.c
    ar -rs libtt.a tt1.o

    Then I wrote the following C++ program and want to link with libtt.a and
    failed.

    /* Filename: tt2.cc */
    int foo();
    int main() {
    int k;
    k = foo();
    return 0;
    }

    % c++ tt2.cc -L. -ltt
    Undefined first referenced
    symbol in file
    foo() /var/tmp//cc02ILOd.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    % c++ tt2.cc tt1.o
    Undefined first referenced
    symbol in file
    foo() /var/tmp//ccgMpZee.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    % g++ tt2.cc libtt.a
    Undefined first referenced
    symbol in file
    foo() /var/tmp//cc8Kx1jd.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    % gcc tt2.cc tt1.o
    Undefined first referenced
    symbol in file
    foo() /var/tmp//ccUM2g0c.o
    __gxx_personality_v0 /var/tmp//ccUM2g0c.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    Did I do something wrong? How can I get these 2 pieces (one is C, the
    other is C++) linked together?

    Thank you very much!
    -Stan
    Stanley Yao Guest

  2. Similar Questions and Discussions

    1. GD-1.38: Undefined symbol: .__fixsfsi
      Hi ! Try to compile GD-1.38 under AIX 5.1 and run into the following error when running make: Undefined symbol: .__fixsfsi I did install...
    2. Undefined symbol perl_get_sv
      I'm trying to use the LibXML module on our Web server, but I get the undefined symbol error. I don't seem to have a perl shared library on the...
    3. Undefined symbol?
      Hey there, Wasn't sure which mailing list to send this to but maybe someone can point me in the right direction. When perl is invoked (presumably...
    4. undefined symbol in GTop.so
      I'm trying to ultimately get Apache::VMonitor running on a server running turbolinx server 8. I'm stuck on GTop, which Apache::VMonitor requires....
    5. undefined symbol identification problem
      hpy_azizy@yahoo.com (ehab) wrote in message news:<b4f59294.0307220019.53d57117@posting.google.com>... 1. Why to name the file as such a long name?...
  3. #2

    Default Re: Undefined symbol in gcc & c++

    > I built the following little C program into a library (libtt.a) with gcc:
    [snip]
    > Then I wrote the following C++ program and want to link with libtt.a and
    > failed.
    [snip}
    > Did I do something wrong? How can I get these 2 pieces (one is C, the
    > other is C++) linked together?

    Google for "mixing C and C++", or
    [url]http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html[/url]

    Oscar del Rio Guest

  4. #3

    Default Re: Undefined symbol in gcc & c++

    In article <Pine.GSO.4.55.0307220956230.1661@lectura.CS.Arizo na.EDU>,
    Stanley Yao <yao@CS.Arizona.EDU> wrote:
    >/* Filename: tt1.c */
    >#include <stdio.h>
    >int foo() {
    > return 1;
    >}
    >
    >gcc -c tt1.c
    >ar -rs libtt.a tt1.o
    This makes foo have `C' linkage
    >
    >Then I wrote the following C++ program and want to link with libtt.a and
    >failed.
    >
    >/* Filename: tt2.cc */
    >int foo();
    here you say foo has c++ linkage
    change to
    extern "C" int foo();
    >Did I do something wrong? How can I get these 2 pieces (one is C, the
    >other is C++) linked together?
    yes, the c++ compile is really looking for a namemangledfoo and not
    foo.

    --
    [url]http://www.math.fsu.edu/~bellenot[/url]
    bellenot <At/> math.fsu.edu
    +1.850.644.7189 (4053fax)
    Steve Bellenot Guest

  5. #4

    Default Re: Undefined symbol in gcc & c++

    Stanley Yao wrote:
    >
    > Dear All,
    >
    > I built the following little C program into a library (libtt.a) with gcc:
    >
    > /* Filename: tt1.c */
    > #include <stdio.h>
    > int foo() {
    > return 1;
    > }
    >
    > gcc -c tt1.c
    > ar -rs libtt.a tt1.o
    >
    > Then I wrote the following C++ program and want to link with libtt.a and
    > failed.
    >
    > /* Filename: tt2.cc */
    > int foo();
    > int main() {
    > int k;
    > k = foo();
    > return 0;
    > }
    >
    > % c++ tt2.cc -L. -ltt
    > Undefined first referenced
    > symbol in file
    > foo() /var/tmp//cc02ILOd.o
    > ld: fatal: Symbol referencing errors. No output written to a.out
    > collect2: ld returned 1 exit status
    >
    > % c++ tt2.cc tt1.o
    > Undefined first referenced
    > symbol in file
    > foo() /var/tmp//ccgMpZee.o
    > ld: fatal: Symbol referencing errors. No output written to a.out
    > collect2: ld returned 1 exit status
    >
    > g++ tt2.cc libtt.a
    > Undefined first referenced
    > symbol in file
    > foo() /var/tmp//cc8Kx1jd.o
    > ld: fatal: Symbol referencing errors. No output written to a.out
    > collect2: ld returned 1 exit status
    >
    > % gcc tt2.cc tt1.o
    > Undefined first referenced
    > symbol in file
    > foo() /var/tmp//ccUM2g0c.o
    > __gxx_personality_v0 /var/tmp//ccUM2g0c.o
    > ld: fatal: Symbol referencing errors. No output written to a.out
    > collect2: ld returned 1 exit status
    >
    > Did I do something wrong? How can I get these 2 pieces (one is C, the
    > other is C++) linked together?
    >
    > Thank you very much!
    > -Stan
    You must declare your foo() function as: extern "C",
    or the linker will not know how to find the symbol.
    The problem is that to accomodate polymorphism, the
    C++ symbols are "mangled" to include type information.
    The C compiler does no such thing, and for the linker to
    find your symbol, it must be told that the symbol is unmangled.

    --
    Fletcher Glenn
    email [email]f-g-l-e-n-n@quest.com[/email] (remove the dashes)
    Fletcher Glenn Guest

  6. #5

    Default Re: Undefined symbol in gcc & c++

    Thank you very much for the help from both of you!
    I modified the code with extern "C" and it's working correctly now.
    According to your answer, I am now reading some materials about "mangled name".
    Have a great day!
    Stan

    On Tue, 22 Jul 2003, Raymundo M. Vega wrote:
    > Check the change to main program.
    >
    > raymundo
    Stanley Yao Guest

Posting Permissions

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

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139