extern "C"

This is a discussion on extern "C" within the c++ forums in Programming Languages category; Hi everyone, I need to be able to mix C and C++. The main program is written in C and needs to access C++ code. To be more specific it's a user defined function in fluent, but this should not be relevant. I've allready read that i need to use the extern "C" construction but i don't seem to be doing it right. So far i've got this compiled: <file: cpplib.cpp> extern "C" int function(); //declaration int function(){...} //definition </file: cpplib.cpp> <file: cpplib.h> int function(){} </file: cpplib.h> <file: cprogram> #include "cpplib.h" ...//call "function()" somewhere </file: cprogram> When i try to ...

Go Back   Application Development Forum > Programming Languages > c++

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-04-2008, 09:55 AM
Karel Van Laer
Guest
 
Default extern "C"

Hi everyone,

I need to be able to mix C and C++.
The main program is written in C and needs to access C++ code.
To be more specific it's a user defined function in fluent, but this
should not be relevant.

I've allready read that i need to use the extern "C" construction but i
don't seem to be doing it right.

So far i've got this compiled:

<file: cpplib.cpp>
extern "C" int function(); //declaration
int function(){...} //definition
</file: cpplib.cpp>

<file: cpplib.h>
int function(){}
</file: cpplib.h>

<file: cprogram>
#include "cpplib.h"
...//call "function()" somewhere
</file: cprogram>

When i try to run it this generates the following error:
/opt/Fluent.Inc/fluent6.3.26/lnx86/2d/fluent.6.3.26: symbol lookup
error: libudf/lnx86/2d/libudf.so: undefined symbol: init
"init" is the function i'm trying to call here.

I'm hoping somebody here can give me an idea of what i'm doing wrong here.

Karel


Reply With Quote
  #2  
Old 09-04-2008, 10:24 AM
Victor Bazarov
Guest
 
Default Re: extern "C"

Karel Van Laer wrote:
> I need to be able to mix C and C++.
> The main program is written in C and needs to access C++ code.
> To be more specific it's a user defined function in fluent, but this
> should not be relevant.
>
> I've allready read that i need to use the extern "C" construction but i
> don't seem to be doing it right.
>
> So far i've got this compiled:
>
> <file: cpplib.cpp>
> extern "C" int function(); //declaration

^^^^^^^^^^^^^^^^^^^^^^^^^
Drop that. Instead do

#include "cpplib.h"

> int function(){...} //definition
> </file: cpplib.cpp>
>
> <file: cpplib.h>
> int function(){}


That's simply wrong. You shouldn't *define* functions in headers like
that. Think about it: the empty body? What you should have do is:

#ifdef __cplusplus
extern "C"
#endif
int function();

> </file: cpplib.h>
>
> <file: cprogram>
> #include "cpplib.h"
> ...//call "function()" somewhere
> </file: cprogram>
>
> When i try to run it this generates the following error:
> /opt/Fluent.Inc/fluent6.3.26/lnx86/2d/fluent.6.3.26: symbol lookup
> error: libudf/lnx86/2d/libudf.so: undefined symbol: init
> "init" is the function i'm trying to call here.


'init'? How does the 'init' play into all that? Isn't your function
called 'function'?

>
> I'm hoping somebody here can give me an idea of what i'm doing wrong here.


For one, you're not posting your real code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Reply With Quote
  #3  
Old 09-04-2008, 10:53 AM
Karel Van Laer
Guest
 
Default Re: extern "C"

You are right about the readability of my code.
So i'll respond with a cleaner version

<file: cpplib.h>
#ifdef __cplusplus
extern "C" {
#endif
int init(); //declaration
#ifdef __cplusplus
}
#endif
</file: cpplib.h>

<file: cpplib.cpp>
#include "cpplib.h"
int init(){...} //definition
</file: cpplib.cpp>

<file: cprogram>
#include "cpplib.h"
...//call "init()" somewhere
</file: cprogram>

the result remains the same:
/opt/Fluent.Inc/fluent6.3.26/lnx86/2d/fluent.6.3.26: symbol lookup
error: libudf/lnx86/2d/libudf.so: undefined symbol: init

Karel

Victor Bazarov wrote:
> Karel Van Laer wrote:
>> I need to be able to mix C and C++.
>> The main program is written in C and needs to access C++ code.
>> To be more specific it's a user defined function in fluent, but this
>> should not be relevant.
>>
>> I've allready read that i need to use the extern "C" construction but i
>> don't seem to be doing it right.
>>
>> So far i've got this compiled:
>>
>> <file: cpplib.cpp>
>> extern "C" int function(); //declaration

> ^^^^^^^^^^^^^^^^^^^^^^^^^
> Drop that. Instead do
>
> #include "cpplib.h"
>
>> int function(){...} //definition
>> </file: cpplib.cpp>
>>
>> <file: cpplib.h>
>> int function(){}

>
> That's simply wrong. You shouldn't *define* functions in headers like
> that. Think about it: the empty body? What you should have do is:
>
> #ifdef __cplusplus
> extern "C"
> #endif
> int function();
>
>> </file: cpplib.h>
>>
>> <file: cprogram>
>> #include "cpplib.h"
>> ...//call "function()" somewhere
>> </file: cprogram>
>>
>> When i try to run it this generates the following error:
>> /opt/Fluent.Inc/fluent6.3.26/lnx86/2d/fluent.6.3.26: symbol lookup
>> error: libudf/lnx86/2d/libudf.so: undefined symbol: init
>> "init" is the function i'm trying to call here.

>
> 'init'? How does the 'init' play into all that? Isn't your function
> called 'function'?
>
>>
>> I'm hoping somebody here can give me an idea of what i'm doing wrong
>> here.

>
> For one, you're not posting your real code.
>
> V

Reply With Quote
  #4  
Old 09-04-2008, 11:15 AM
Uwe Schmitt
Guest
 
Default Re: extern "C"

On 4 Sep., 16:53, Karel Van Laer <kvl...@biomath.ugent.be> wrote:
> You are right about the readability of my code.
> So i'll respond with a cleaner version
>
> <file: cpplib.h>
> * * * #ifdef __cplusplus
> * * * extern "C" {
> * * * #endif
> * * * int init(); //declaration
> * * * #ifdef __cplusplus
> * * * }
> * * * #endif
> </file: cpplib.h>
>
> <file: cpplib.cpp>
> * * *#include "cpplib.h"
> * * *int init(){...} //definition
> </file: cpplib.cpp>
>
> <file: cprogram>
> * * *#include "cpplib.h"
> * * *...//call "init()" somewhere
> </file: cprogram>
>
> the result remains the same:
> /opt/Fluent.Inc/fluent6.3.26/lnx86/2d/fluent.6.3.26: symbol lookup
> error: libudf/lnx86/2d/libudf.so: undefined symbol: init
>


Which compilers do you use and how do you use them ?

If you use the gnu compiliers, you should use

g++ -c cpplib.cpp

and then

gcc cprogram.c cpplib.o

Greetings, Uwe
Reply With Quote
  #5  
Old 09-04-2008, 03:10 PM
Ian Collins
Guest
 
Default Re: extern "C"

Uwe Schmitt wrote:
>
> Which compilers do you use and how do you use them ?
>
> If you use the gnu compiliers, you should use
>
> g++ -c cpplib.cpp
>
> and then
>
> gcc cprogram.c cpplib.o
>
> Greetings, Uwe


That's very unlikely to work. The main program must be compiled and
linked as C++.

--
Ian Collins.
Reply With Quote
  #6  
Old 09-04-2008, 03:46 PM
Victor Bazarov
Guest
 
Default Re: extern "C"

Ian Collins wrote:
> Uwe Schmitt wrote:
>> Which compilers do you use and how do you use them ?
>>
>> If you use the gnu compiliers, you should use
>>
>> g++ -c cpplib.cpp
>>
>> and then
>>
>> gcc cprogram.c cpplib.o
>>
>> Greetings, Uwe

>
> That's very unlikely to work. The main program must be compiled and
> linked as C++.
>


So, more likely it should be

g++ -c cpplib.cpp
gcc -c cprogram.c
g++ cprogram.o cpplib.o -o cprogram

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Reply With Quote
  #7  
Old 09-04-2008, 10:52 PM
northern_RATT
Guest
 
Default Re: extern "C"

<snip>
> I need to be able to mix C and C++.
> The main program is written in C and needs to access C++ code.
> To be more specific it's a user defined function in fluent, but this
> should not be relevant.
>
> I've allready read that i need to use the extern "C" construction but i
> don't seem to be doing it right.

</snip>

OK. Pardon a rank amature who only lurks here, BUT....
This looks like backwards logic to me. I thought that
'extern C' is a way for C++ code to call undecorated C code(?)
Not the otherway around!
Not that I have ever needed to face this, but I thought that C calling
C++ code needs some type of wrapper?

Confused?
Drew


Reply With Quote
  #8  
Old 09-04-2008, 11:22 PM
Alexander Dong Back Kim
Guest
 
Default Re: extern "C"

On Sep 5, 12:52 pm, "northern_RATT" <n...@none.net> wrote:
> <snip>> I need to be able to mix C and C++.
> > The main program is written in C and needs to access C++ code.
> > To be more specific it's a user defined function in fluent, but this
> > should not be relevant.

>
> > I've allready read that i need to use the extern "C" construction but i
> > don't seem to be doing it right.

>
> </snip>
>
> OK. Pardon a rank amature who only lurks here, BUT....
> This looks like backwards logic to me. I thought that
> 'extern C' is a way for C++ code to call undecorated C code(?)
> Not the otherway around!
> Not that I have ever needed to face this, but I thought that C calling
> C++ code needs some type of wrapper?
>
> Confused?
> Drew


These kinds of problems are really annoying sometimes however
unfortunately using both C and C++ are very common in real world (at
least my world =P). I'm certainly wondering what would be the "best"
practice for avoiding or even relaxing this kinds of issues.

cheers,
Alex Kim
Reply With Quote
  #9  
Old 09-05-2008, 03:18 AM
Michael DOUBEZ
Guest
 
Default Re: extern "C"

Alexander Dong Back Kim a écrit :
> On Sep 5, 12:52 pm, "northern_RATT" <n...@none.net> wrote:
>> <snip>> I need to be able to mix C and C++.

> These kinds of problems are really annoying sometimes however
> unfortunately using both C and C++ are very common in real world (at
> least my world =P). I'm certainly wondering what would be the "best"
> practice for avoiding or even relaxing this kinds of issues.


Compile your C code with a c++ compiler
Most c++ compiler support C99 unless you disable it in the command line.

--
Michael
Reply With Quote
  #10  
Old 09-05-2008, 05:56 AM
James Kanze
Guest
 
Default Re: extern "C"

On Sep 4, 9:46 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Ian Collins wrote:
> > Uwe Schmitt wrote:
> >> Which compilers do you use and how do you use them ?


> >> If you use the gnu compiliers, you should use


> >> g++ -c cpplib.cpp


> >> and then


> >> gcc cprogram.c cpplib.o


> > That's very unlikely to work. The main program must be
> > compiled and linked as C++.


> So, more likely it should be


> g++ -c cpplib.cpp
> gcc -c cprogram.c
> g++ cprogram.o cpplib.o -o cprogram


If the main() is in cprogram.c, that still isn't guaranteed to
work. main() must be compiled as C++. (But since not doing so
is undefined behavior, it's possible that it works anyway, with
some implementations.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:28 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.