473,789 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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! ]
Jul 22 '05 #1
3 2560
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
Jul 22 '05 #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(.. ..);

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! ]
Jul 22 '05 #3
GTO
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++.m oderated. First time posters: Do this! ]
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4117
by: RainBow | last post by:
Hi everyone, (Very Sorry, if this is the wrong group in which I am posting this query). Code snippet: //C library typedef int (*PFunc)(int* aArg); void call_c_foo(PFunc aPtrtoFunc) {
19
2519
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with the declaration for the C library function (stdlib.h in this case) then define/declare my own function with the same name, am I safe? It seems to work on my compiler, but I wonder if this is portable or even acceptable? thx
2
1923
by: adrin | last post by:
hello, im calling a function from an unmanaged dll which takes a ref to structure as an argument. one of the struct's fields is an address to a callback function/event(it depends on you what to choose). It works asynchronously, modyfies given structure and calls callback/event when finished. How can i make it work in c#? can i use events? Can i wait for the dll function to finish within c# method that called it? Do i declare address to...
1
7162
by: Adam Clauss | last post by:
I have an unmanaged C++ library that I need to use through C#. I created a Managed C++ class library and through it wrote a wrapper class to call what I need. However, one of the methods (an Initialize function in the unmanaged library) crashes everytime I call it. Initialize calls another function 'GetInstance' (a static member function of a class) which looks something like: CLibConfig& CLibConfig::GetInstance()
4
6554
by: Anita C | last post by:
Hi, Can one call a c# function from within a javascript function ? I have a c# function GetLanguageItem(title), how can I call the above function from a javascript function in an .aspx page. Thanks
6
1709
by: Amjad | last post by:
Hi, I want to make a project that calls and executes a function (VB code) made in a seperate file in the Application Folder. I know I can create the function in my project and call it internally, but I want to put the function's code in an external file, so that future updates to the function will require a replacing the function file instead of re-installing the whole project. Can you give me ideas on how I can make small updates to my...
12
1304
by: KIRAN | last post by:
hi, the grammer for any programming language says that when a function is called by another function,the callee after executing it's body should return to the point where it left in the caller.. Is there any technique to make the callee to return to some other point(within the current process) other than the callee by changing the call stack in callee... My code runs on 86 processor (If this thread is irrelevent to this group please...
3
4712
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting enhancement which asynchronly
23
5002
by: Stewart Berman | last post by:
I am trying to develop a wrapper class for the Windows API functions in Visual Studio 2008: GetOpenFileName GetSaveFileName I put together a starter class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.