Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Invoking c function in a c plus plus function...

Reply
Thread Tools

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

 
 
Rahul
Guest
Posts: n/a
 
      03-24-2008
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 ! ! !
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-24-2008
* 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

--
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?
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      03-24-2008
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.
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      03-24-2008
On Mar 24, 4:39 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * 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
>
> --
> 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...
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      03-24-2008
On Mar 24, 4:47 pm, Gianni Mariani <(E-Mail Removed)> wrote:
> 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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-24-2008
* Rahul:
> On Mar 24, 4:47 pm, Gianni Mariani <(E-Mail Removed)> wrote:
>> 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.


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


--
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?
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      03-24-2008
On Mar 24, 5:00 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * Rahul:
>
>
>
> > On Mar 24, 4:47 pm, Gianni Mariani <(E-Mail Removed)> wrote:
> >> 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.

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

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-24-2008
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-25-2008
On Mar 24, 2:45 pm, Andrey Tarasevich <(E-Mail Removed)>
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:(E-Mail Removed)
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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-25-2008
James Kanze 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.
> ...


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.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c plus plus code comparator furqan shaikh C++ 6 11-12-2008 06:14 AM
JavaScript: conditionally invoking functions within a function Spartanicus HTML 3 09-30-2005 03:35 PM
C plus plus vs C Sharp The LoxFather C Programming 23 08-14-2003 03:51 AM
C plus plus vs C Sharp The LoxFather C++ 23 08-14-2003 03:51 AM



Advertisments