469,269 Members | 1,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,269 developers. It's quick & easy.

PLEASE HELP !! Why does extern "C" not work for gcc 3.2.3

Could some C++ guru help me please? I am trying to build an application
using gcc 3.2.3, that has a some classes using functions defined in
some C files in the same directory. If inside the C++ source file I
use:
extern "C"{
// C function declarations
}

//C++ code

Then I get all linker problems that such and such C function cannot be
resolved. Is
there a compiler flag(in addition to the directive above) that I need
to activate?
Any help would be greatly appreciated. Thanks in advance for your help.

Jul 23 '05 #1
4 2565

<cp**********@yahoo.com> wrote in message
Could some C++ guru help me please? I am trying to build an application
using gcc 3.2.3, that has a some classes using functions defined in
some C files in the same directory. If inside the C++ source file I
use:
extern "C"{
// C function declarations
}

//C++ code

Then I get all linker problems that such and such C function cannot be
resolved. Is
there a compiler flag(in addition to the directive above) that I need
to activate?
Any help would be greatly appreciated. Thanks in advance for your help.


What are the errors ? Use g++ for C++ code.

Sharad

Jul 23 '05 #2
Do you compile your C code with g++? if so you will get C++ symbols...
compile with gcc.

Jul 23 '05 #3
There is a Makefile and I do have seperate ways of compiling C and C++
code. The errors are linker errors, and is due to the name mangling by
C++ compiler. I am not sure how to get around them though.

Jul 23 '05 #4
cp**********@yahoo.com wrote:
Could some C++ guru help me please? I am trying to build an application
using gcc 3.2.3, that has a some classes using functions defined in
some C files in the same directory. If inside the C++ source file I
use:
extern "C"{
// C function declarations
}

//C++ code

Then I get all linker problems that such and such C function cannot be
resolved. Is
there a compiler flag(in addition to the directive above) that I need
to activate?
Any help would be greatly appreciated. Thanks in advance for your help.

Well, you didn't give any specifics, so here are some
generic tips for the GCC suite:

1) use 'gcc' to compile AND LINK 'C' programs (even
those with multiple object files and libs)

2) use 'g++' to compile AND LINK 'C++' programs (even
those with multiple object files and libs)

3) use 'g++' to link programs which mix 'C' and 'C++'
object files and libs.

Both 'gcc' and 'g++' provide default lib info and additional
link info to the linker depending on 'C', 'C++', or mixed
code.

In other words, if your Makefile is invoking the linker
directly, you should change it to invoke either 'gcc' or
'g++' based on the 3 tips above.

Normally, the function prototypes for the 'C' modules
would be in a header file (e.g. 'myCstuff.h") that
your 'C++' modules would '#include'. That header file
would include the 'extern "C"' block, e.g.

/* myCstuff.h */

#ifndef MYCSTUFF_H
# define MYSCTUFF_H

# include <some_header>
# include <another_header>
# include "local_header"
# include "another_local_header"

# ifdef __cplusplus
extern "C" {
# endif

/* defines, typedefs, macros, prototypes for C functions
* go here
*/

# ifdef __cplusplus
}
# endif

#endif

--------

// in somefile.cpp
#include "myCstuff.h"
Regards,
Larry
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andy | last post: by
19 posts views Thread by ccwork | last post: by
4 posts views Thread by Bonj | last post: by
4 posts views Thread by kk_oop | last post: by
5 posts views Thread by jchludzinski | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.