On 2 Oct 2011, at 14:54, Mariwan wrote:

> HI,
> I have a problem in linking some .o object to the executable file in linux 
> (fedora).
> com.o uiciname.o uici.o restart.o are written in c and I use gcc to compile 
> them.
> but client.o is a fltk program.  and I use g++ to compile it.
> here is the out put of the make:
> 
> g++ -o Client  client.o   com.o uiciname.o uici.o restart.o   
> -L/usr/X11R6/lib -L/usr/local/lib   -lX11 -lfltk -lfltk_images -lm -lXpm 
> -lpng -lz -ljpeg
> client.o: In function `main':
> client.cxx:(.text.startup+0x3bb): undefined reference to 
> `connectToServer(char const*, unsigned short, char const*, char const*)'
> client.cxx:(.text.startup+0x3e8): undefined reference to 
> `stratChatReceiving(int*, char*)'
> client.cxx:(.text.startup+0x49f): undefined reference to 
> `startChatSending(int*, char const*)'
> collect2: ld returned 1 exit status
> make: *** [Client] Error 1
> 
> These functions are defined with extern .. still it is not helping to solve 
> the problem..
> It seems that gcc and g++ have different format in the object files .. that 
> is why the linker (g++) will not find the reference in the  objects that were 
> compiled by gcc. Dose that mean that I should make a library for the gcc 
> outputs?
> Any idea is appreciated.

The calling conventions used by C and C++ are not the same, so you can not 
directly call C++ functions from C programs, or vice versa.

However, it is very easy to call C functions from C++, you just need to ensure 
that you put an extern "C" {...} around the definitions of the C functions you 
want to call from your C++ code.

So, for example, in your C++ code, you might have...

extern "C" {
int my_C_function(void);
int my_other_C_function(int param);
// etc...
}

And then the C++ code will be able to call (and link correctly to) the C 
functions.

Calling C++ code is trickier, so your best bet is to make a C++ function that 
is explicitly compiled with C linkage.
That will be callable from your C code, and will be able to call other C++ 
methods. But calling C++ methods directly from C code is not going to work...

This might help...

http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.6 







_______________________________________________
fltk mailing list
fltk@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to