473,388 Members | 1,215 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,388 software developers and data experts.

Invoking c function in a c plus plus function...

Hi Everyone,

I want to invoke a c function ( object file created by cc ) from a
cpp function ( object file is created using g++ ). When i try to link,
both the object files using g++

g++ cfile.o cppfile.o

i get an error saying,

: undefined reference to `fun()'
collect2: ld returned 1 exit status

Note that the fun() is the c function defined in cfile.c. I can't use
cc to link both the object files as the master file is a cpp source
file.

Is there anyway to get this done? I know the method of using extern
"C" but that works only when a cpp function ( object file created in g+
+ ) is to be invoked from a c function ( object file created in cc ).

Thanks in advance ! ! !
Mar 24 '08 #1
4 1273
Rahul wrote:
Hi Everyone,

I want to invoke a c function ( object file created by cc ) from a
cpp function ( object file is created using g++ ). When i try to link,
both the object files using g++

g++ cfile.o cppfile.o

i get an error saying,

: undefined reference to `fun()'
collect2: ld returned 1 exit status

Note that the fun() is the c function defined in cfile.c. I can't use
cc to link both the object files as the master file is a cpp source
file.

Is there anyway to get this done? I know the method of using extern
"C" but that works only when a cpp function ( object file created in g+
+ ) is to be invoked from a c function ( object file created in cc ).
I believe that's exactly what extern "C" is for. Show us the
chunk-o-code that exhibits this problem.
Mar 24 '08 #2
On Mar 24, 4:47 pm, Gianni Mariani <gi4nos...@mariani.wswrote:
Rahul wrote:
Hi Everyone,
I want to invoke a c function ( object file created by cc ) from a
cpp function ( object file is created using g++ ). When i try to link,
both the object files using g++
g++ cfile.o cppfile.o
i get an error saying,
: undefined reference to `fun()'
collect2: ld returned 1 exit status
Note that the fun() is the c function defined in cfile.c. I can't use
cc to link both the object files as the master file is a cpp source
file.
Is there anyway to get this done? I know the method of using extern
"C" but that works only when a cpp function ( object file created in g+
+ ) is to be invoked from a c function ( object file created in cc ).

I believe that's exactly what extern "C" is for. Show us the
chunk-o-code that exhibits this problem.
cfile.c

int fun()
{
return (0);
}

cppfile.cpp

#include <cstdio>

extern int fun();

int main()
{
int a = fun();
printf("integer is %d\n",a);
}

extern "C" just avoids name mangling. Are you suggesting to have it
around the function call in main() of cppfile.cpp?
Mar 24 '08 #3
Rahul wrote:
>
By the way, this is working,

cppfile.cpp

#include <cstdio>

extern "C"
{
extern int fun();

int main()
{
int a = fun();
printf("integer is %d\n",a);
}
}
'extern "C"' is supposed to be applied to declarations. There's no much point in
wrapping your entire translation unit into 'extern "C"'

extern "C" int fun();

int main()
{
int a = fun();
printf("integer is %d\n",a);
}

--
Best regards,
Andrey Tarasevich
Mar 24 '08 #4
On Mar 24, 2:45 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
Rahul wrote:
By the way, this is working,
cppfile.cpp
#include <cstdio>
extern "C"
{
extern int fun();
int main()
{
int a = fun();
printf("integer is %d\n",a);
}
}
'extern "C"' is supposed to be applied to declarations.
There's no much point in wrapping your entire translation unit
into 'extern "C"'
It's frequent to wrap a set of declarations (or even an entire
header file) in `extern "C"'---there are even some types of
declarations (e.g. typedef's) which can't be done otherwise.

On the other hand, I'm not too sure that declaring main to be
`extern "C"' is legal. (If it is, the `extern "C"' is ignored.)
In the OP's case, applying the `extern "C"' to just the
declaration is quite appropriate.
extern "C" int fun();
And of course, this really belongs in a separate header file.

--
James Kanze (GABI Software) email:ja*********@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
Mar 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Manuel | last post by:
Can I invoke a function before main I could do it by invoking it in a Global object's constructor . Is there any other method other than this. Manuel
10
by: sjbrown8 | last post by:
I have the piece of code below, and when i try compiling with the line g++ 753075304.cpp I get the following error message: 753075304.cpp: In function 'int main()': 753075304.cpp:29: error:...
25
by: MuZZy | last post by:
Hi, I'm currently rewriting some functionality which was using multithredaing for retrieving datasets from database and updating a grid control. I found that the grids (Infragistics UltraGrid,...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.