Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C call C++ fuction and iostream

Reply
Thread Tools

C call C++ fuction and iostream

 
 
Cat
Guest
Posts: n/a
 
      08-18-2003
Hi
I got stuck for C call C++ function w/ iostream.
Here is my code

In 1.h
---------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
int Export();
#ifdef __cplusplus
}
#endif
---------------------------------------------

In 1.cpp
---------------------------------------------
#include <vector>
#include <iostream>
#include "1.h"

int Export() {
std::vector<int> v;
std::cout << "This is from Export()\n";
return v.size();
}
---------------------------------------------

In Driver.c
---------------------------------------------
#include <stdio.h>
#include "1.h"

int main() {
printf("%d\n", Export());
return 0;
}
---------------------------------------------

And I execute the following:

$ g++ -c 1.cpp
$ gcc -c Driver1.c
$ gcc -o Out1 Driver1.o 1.o

then got the error for the last one command.

1.o: In function `Export':
1.o(.text+0x4c): undefined reference to `cout'
1.o(.text+0x50): undefined reference to `cout'
1.o(.text+0x5c): undefined reference to `ostream:perator<<(char const *)'
collect2: ld returned 1 exit status


But, if I remove the following lines in 1.cpp
#include <iostream>
std:ut << "This is from Export()\n";

Everything becomes right. Anyone can give me a hint?
I tried on two machines:

Solarios 5.8, gcc/g++ 2.95.3
and
Mandrake Linux 9, gcc/g++ 3.2

They both got the same result.

Thanx adv.
JSCW


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
Reply With Quote
 
 
 
 
Buster Copley
Guest
Posts: n/a
 
      08-18-2003
Cat wrote:
....
> And I execute the following:
>
> $ g++ -c 1.cpp
> $ gcc -c Driver1.c
> $ gcc -o Out1 Driver1.o 1.o
>
> then got the error for the last one command.
>
> 1.o: In function `Export':
> 1.o(.text+0x4c): undefined reference to `cout'
> 1.o(.text+0x50): undefined reference to `cout'
> 1.o(.text+0x5c): undefined reference to `ostream:perator<<(char const *)'
> collect2: ld returned 1 exit status


Use the command 'g++' to link C++ programs. This causes the
necessary C++ libraries to be linked.

Buster.

 
Reply With Quote
 
 
 
 
llewelly
Guest
Posts: n/a
 
      08-20-2003
Cat <(E-Mail Removed)> writes:

> Hi
> I got stuck for C call C++ function w/ iostream.
> Here is my code


{ mod snip -mod/jep }

> And I execute the following:
>
> $ g++ -c 1.cpp
> $ gcc -c Driver1.c
> $ gcc -o Out1 Driver1.o 1.o


Link with g++ and not with gcc:

g++ -o Out1 Driver1.o 1.o


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
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
call asp fuction on button click in asp vinodkus@gmail.com ASP General 2 08-13-2007 10:49 AM
Semi OT: Mixing iostream and iostream.h red floyd C++ 3 03-08-2005 05:57 PM
iostream + iostream.h S. Nurbe C++ 7 01-15-2005 07:49 AM
is MS newer <iostream> is slower than older <iostream.h>? ai@work C++ 9 12-16-2004 08:21 PM
#include <iostream.h> or <iostream> John Tiger C++ 10 08-06-2003 01:30 PM



Advertisments