473,382 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2746

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
3
by: Andy | last post by:
Hi, This is just a simple hello world program. #include <iostream> using namespace std; int main() { cout<<"Hello,world\n"; }
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
4
by: Bonj | last post by:
I can use extern "C" ... to link a C variable or runction into a C++ file, but what if I want to link a C++ variable into a C file? I get errors trying to use extern "C++" and it seems like it is...
4
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a...
5
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern"...
3
by: parag_paul | last post by:
Does the following typedef differ from a typedef inside a extern "C" #if defined(__cplusplus) extern "C" { #endif typedef int (*fun_point)(int);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.