Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Invoking c function in a c plus plus function... (http://www.velocityreviews.com/forums/t601276-invoking-c-function-in-a-c-plus-plus-function.html)

Rahul 03-24-2008 11:31 AM

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

Alf P. Steinbach 03-24-2008 11:39 AM

Re: Invoking c function in a c plus plus function...
 
* 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?

Gianni Mariani 03-24-2008 11:47 AM

Re: Invoking c function in a c plus plus function...
 
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.

Rahul 03-24-2008 11:47 AM

Re: Invoking c function in a c plus plus function...
 
On Mar 24, 4:39 pm, "Alf P. Steinbach" <al...@start.no> 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...

Rahul 03-24-2008 11:50 AM

Re: Invoking c function in a c plus plus function...
 
On Mar 24, 4:47 pm, Gianni Mariani <gi4nos...@mariani.ws> 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?

Alf P. Steinbach 03-24-2008 12:00 PM

Re: Invoking c function in a c plus plus function...
 
* Rahul:
> On Mar 24, 4:47 pm, Gianni Mariani <gi4nos...@mariani.ws> 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?

Rahul 03-24-2008 12:20 PM

Re: Invoking c function in a c plus plus function...
 
On Mar 24, 5:00 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * Rahul:
>
>
>
> > On Mar 24, 4:47 pm, Gianni Mariani <gi4nos...@mariani.ws> 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


Andrey Tarasevich 03-24-2008 01:45 PM

Re: Invoking c function in a c plus plus function...
 
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

James Kanze 03-25-2008 08:25 AM

Re: Invoking c function in a c plus plus function...
 
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:james.kanze@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

Andrey Tarasevich 03-25-2008 05:24 PM

Re: Invoking c function in a c plus plus function...
 
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


All times are GMT. The time now is 07:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.