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

R

Rahul

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 ! ! !
 
A

Alf P. Steinbach

* Rahul:
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 ).

Well that is incorrect.

Thanks in advance ! ! !

This is a FAQ.

See the FAQ.


Cheers, & hth.,

- Alf
 
G

Gianni Mariani

Rahul said:
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.
 
R

Rahul

* Rahul:











Well that is incorrect.


This is a FAQ.

See the FAQ.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

I know that it is incorrect, i was looking for a work around...
 
R

Rahul

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?
 
A

Alf P. Steinbach

* Rahul:
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.

That is, again, incorrect.

Last time you responded that you knew your statement was incorrect.

Please stop making statements that you "know" are incorrect.

Are you suggesting to have it
around the function call in main() of cppfile.cpp?

This is covered by the FAQ.

Please see that FAQ, instead of having people here use time to spoon-feed you.


Cheers, & hth.,

- Alf
 
R

Rahul

* Rahul:







That is, again, incorrect.

Last time you responded that you knew your statement was incorrect.

Please stop making statements that you "know" are incorrect.


This is covered by the FAQ.

Please see that FAQ, instead of having people here use time to spoon-feed you.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

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);
}
}

cc -c cfile.c
g++ -c cppfile.cpp
g++ file.o cppfile.o
 
A

Andrey Tarasevich

Rahul said:
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);
}
 
J

James Kanze

Rahul wrote:
'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.
 
A

Andrey Tarasevich

James said:
...

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.
...

Well, the reason I wanted to emphasize that 'extern "C"' is supposed to
be used with declarations is because in one of the previous messages OP
appeared to come up with an idea of applying 'extern "C"' to the
"function call in main".

I assumed that wrapping the entire body of 'main' into the 'extern "C"'
in his previous message was an attempt to do exactly that, and that the
success ("this is working") was incorrectly attributed specifically to
the actual call being located within 'extern "C"' region.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top