| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| 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 |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| 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. |
|
#6
| |||
| |||
| 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 |
|
#7
| |||
| |||
| <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 |
|
#8
| |||
| |||
| 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 |
|
#9
| |||
| |||
| 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 |
|
#10
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.