Calling carbon function in a .a archive from gcc C program

Discussion in 'Mac Programming' started by hockgow, Mar 3, 2011.

  1. hockgow, Mar 3, 2011
    Last edited by a moderator: Mar 3, 2011

    hockgow macrumors newbie

    Mar 3, 2011
    Hello all,

    Any help would be great. I've created a Carbon static library (mylib.a) and in it is a c++ function called MyBeep(). I've also created a gcc cpp app which will call MyBeep(). I know I can add the MyBeep() source to my app and use the '-framework carbon' flag to compile everything into 1 app, but I want a separate lib and executable.

    The issue is the linker is unable to locate MyBeep() from within mylib.a. When I link using
    gcc -o myexe file1.o file2.o mylib.a

    I get:
    Undefined symbols"
    "_MyBeep", referenced from _Test in file1.o
    ld: symbol(s) not found

    When I display name list (symbol table) via unix command 'nm' for the mylib.a, I get:
    00007a8 s EH_fram1
    U _SysBeep
    0000000 T __Z6MyBeepv
    00007c4 S
    U __gx_personality_v0

    Displaying the name list for the file1.o that calls MyBeep() yields:
    0000020 T _Test
    U _MyBeep

    So some name mangling is occurring. I tried numerous variations of call MyBeep() from with test.c (_MyBeep, _Z6MyBeepv, __Z6MyBeepv, etc) and havent been able to determine the right convention.

    Anyone have any ideas?

    I should also note mylib.a was created via xcode.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    Try surrounding your C code in your lib with
    #ifdef __cplusplus
    extern "C" {
    // C stuff
    #ifdef __cplusplus
  3. hockgow thread starter macrumors newbie

    Mar 3, 2011
    Thanks kainjow. I completely forgot about extern "c". That fixed the name mangling issue, but brought up other issues I was able to resolve.

    Thanks again!

Share This Page