Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling a C library function from within C++

Reply
Thread Tools

Calling a C library function from within C++

 
 
Glenn C. Rhoads
Guest
Posts: n/a
 
      10-20-2004
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! ]
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-20-2004
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-21-2004
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! ]
 
Reply With Quote
 
GTO
Guest
Posts: n/a
 
      10-21-2004
I assume that you know this

http://www.parashift.com/c++-faq-lit...c-and-cpp.html

Gregor


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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling my custom function same as library function Deniz Bahar C Programming 43 03-02-2005 10:18 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Calling a Managed C++ library which uses a load library function from a WS Sammy ASP .Net Web Services 1 08-18-2003 10:51 PM



Advertisments