By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,189 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,189 IT Pros & Developers. It's quick & easy.

C call C++ fuction and iostream

P: n/a
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::operator<<(char const *)'
collect2: ld returned 1 exit status
But, if I remove the following lines in 1.cpp
#include <iostream>
std::out << "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! ]
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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::operator<<(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.

Jul 19 '05 #2

P: n/a
Cat <me**@cs.umd.edu> 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! ]
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.