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