LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2010, 10:26 AM   #1
nuliknol
Member
 
Registered: Jul 2009
Posts: 42

Rep: Reputation: 0
accessing C function from C++


Hi,

i have an open source application that was developed in C++ (it uses objects and namespaces all over the place). I also have another application that was developed in C. Now i am trying to insert the code made in C into C++ application, but when i do that , i get this error on compilation of the source code:

Code:
error: 'mpi_init_vars' was not declared in this scope
the function mpi_init_vars() is not part of object oriented programming and it doesn't belong to any scope, it was compiled into an object file and i am linking it with all objects of C++ application.

How can invoke the C function from C++ object oriented code?

Thanks in advance.
 
Old 07-05-2010, 10:53 AM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by nuliknol View Post
i have an open source application that was developed in C++ (it uses objects and namespaces all over the place). I also have another application that was developed in C. Now i am trying to insert the code made in C into C++ application, but when i do that , i get this error on compilation of the source code:

Code:
error: 'mpi_init_vars' was not declared in this scope
the function mpi_init_vars() is not part of object oriented programming and it doesn't belong to any scope, it was compiled into an object file and i am linking it with all objects of C++ application.

How can invoke the C function from C++ object oriented code?
C++ mangles function names, thought mpi_init_vars() will become _Zmpi_init_vars (or similar). Please check out the extern "C" {} functionality in your favourite WEB search.

Andi

Last edited by ForzaItalia2006; 07-05-2010 at 11:07 AM.
 
Old 07-05-2010, 11:01 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, nuliknol -

1. C++ (unlike "classic C") does NOT allow "implicit declarations". You MUST declare function "mpi_init_vars()" before you use it.

2. As ForzaItalia2006 said, you must also use "externC {}" syntax when you declare it.

3. The declaration should usually be in a header file. If you're using somebody else's library, you should already have a .h header. Otherwise, you can easily write your own:

Example header ("mympi.h"; change as appropriate):
Code:
#ifndef MY_MPI_H
#define MY_MPI_H

#ifdef __cplusplus
extern "C" {
#endif
void mpi_init_vars ();
#ifdef __cplusplus
}
#endif 

#endif
/* MY_MPI_H *
It looks ugly. Heck - it IS ugly But it should get you going!

'Hope that helps .. PSM
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
Accessing command line arguments from a library function? tojo2 Programming 3 05-25-2005 03:56 PM
accessing variables inside a function bahadur Programming 1 03-27-2005 04:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration