C call C++ fuction and iostream

C

Cat

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::eek:perator<<(char const *)'
collect2: ld returned 1 exit status


But, if I remove the following lines in 1.cpp
#include <iostream>
std::eek: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
 
B

Buster Copley

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::eek: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.
 
L

llewelly

Cat said:
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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top