Calling a C library function from within C++

Discussion in 'C++' started by Glenn C. Rhoads, Oct 20, 2004.

  1. 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++.moderated. First time posters: Do this! ]
     
    Glenn C. Rhoads, Oct 20, 2004
    #1
    1. Advertising

  2. Glenn C. Rhoads wrote:
    > 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(....);


    I trust you don't have the dots in the actual code.

    > 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).


    Not an unusual thing at all.

    > According to
    > the user manual provided with the library, I'm passing in the
    > arguments correctly


    You ought to say "according to my reading of the manual".

    > 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?


    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.

    > 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/


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

    Victor
     
    Victor Bazarov, Oct 21, 2004
    #2
    1. Advertising

  3. Glenn C. Rhoads

    Ron Natalie Guest

    Glenn C. Rhoads wrote:
    > 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(....);
    >

    What is the declaration of the fuction in C?

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ron Natalie, Oct 21, 2004
    #3
  4. Glenn C. Rhoads

    GTO Guest

    GTO, Oct 21, 2004
    #4
    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. Markusek Peter

    calling javascript function within <TABLE> tags

    Markusek Peter, Feb 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    7,692
    ajaymehra
    Feb 9, 2004
  2. Andreas Lagemann
    Replies:
    8
    Views:
    504
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    454
  4. Deniz Bahar
    Replies:
    43
    Views:
    975
    Gordon Burditt
    Mar 2, 2005
  5. Sammy
    Replies:
    1
    Views:
    326
    NET_NET_2003
    Aug 18, 2003
Loading...

Share This Page