Calling a C library function from within C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Glenn C. Rhoads

    #1

    Calling a C library function from within C++

    I'm having a problem calling a C function from a publicly available
    library file. To get the code to compile, I had to declare the
    function as an external C function as follows.

    extern "C" lib_function(.. ..);

    But when I run my C++ program, I get a segmentation fault in one
    of the library functions (not the function I directly called but
    one a few levels down in the calling hierarchy). According to
    the user manual provided with the library, I'm passing in the
    arguments correctly and I've allocated space for the arrays being
    passed (each argument is either an integer or an array of integers),
    hence I suspect the problem has something to do with the fact that
    I'm calling a C function from within C++. (the authors claim that
    the C code has no known bugs).

    Any ideas on what the problem could be?

    Note that I am using the gcc/g++ compiler on UNIX. In case you're
    interested, the library package I'm using is called HMETIS and is
    available at http://www-users.cs.umn.edu/~karypis/metis/

    Also if you want to respond via email, then send your message to
    rhoads <at> paul <dot> rutgers <dot> edu (the yahoo.com address
    is bogus).

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.m oderated. First time posters: Do this! ]
  • Victor Bazarov

    #2
    Re: Calling a C library function from within C++

    Glenn C. Rhoads wrote:[color=blue]
    > I'm having a problem calling a C function from a publicly available
    > library file. To get the code to compile, I had to declare the
    > function as an external C function as follows.
    >
    > extern "C" lib_function(.. ..);[/color]

    I trust you don't have the dots in the actual code.
    [color=blue]
    > But when I run my C++ program, I get a segmentation fault in one
    > of the library functions (not the function I directly called but
    > one a few levels down in the calling hierarchy).[/color]

    Not an unusual thing at all.
    [color=blue]
    > According to
    > the user manual provided with the library, I'm passing in the
    > arguments correctly[/color]

    You ought to say "according to my reading of the manual".
    [color=blue]
    > and I've allocated space for the arrays being
    > passed (each argument is either an integer or an array of integers),
    > hence I suspect the problem has something to do with the fact that
    > I'm calling a C function from within C++. (the authors claim that
    > the C code has no known bugs).
    >
    > Any ideas on what the problem could be?[/color]

    Nope. Run it under a debugger. Let it crash and analyse the contents
    of the variables. If you can find the source code for the library,
    build it again with debugging information, and debug it.
    [color=blue]
    > Note that I am using the gcc/g++ compiler on UNIX. In case you're
    > interested, the library package I'm using is called HMETIS and is
    > available at http://www-users.cs.umn.edu/~karypis/metis/[/color]

    Post the short version of how you use it (unless you manage to find
    the error before that).

    Victor

    Comment

    • Ron Natalie

      #3
      Re: Calling a C library function from within C++

      Glenn C. Rhoads wrote:[color=blue]
      > I'm having a problem calling a C function from a publicly available
      > library file. To get the code to compile, I had to declare the
      > function as an external C function as follows.
      >
      > extern "C" lib_function(.. ..);
      >[/color]
      What is the declaration of the fuction in C?

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.m oderated. First time posters: Do this! ]

      Comment

      • GTO

        #4
        Re: Calling a C library function from within C++

        I assume that you know this



        Gregor


        [ See http://www.gotw.ca/resources/clcm.htm for info about ]
        [ comp.lang.c++.m oderated. First time posters: Do this! ]

        Comment

        Working...