473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

C call C++ fuction and iostream

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
2 3874
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Szaki | last post by:
I use a BulkLoad to import file.xml to my base MS Server 2000. To import this xml file I need schema file. Mayby you know how to do this file mechanicy f.g. mayby somebody have some script in .net...
3
by: mike | last post by:
i've already used static fuction to make website i wanna common module that's why i used static fuctions. may be it's more than 100 fuction ...is it ok?
18
by: cmk128 | last post by:
hi 1 #include <stdio.h> 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x;
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
3
by: xtrigger303 | last post by:
Hi to all, a newbie question. Is it possible to avoid the virtual mechanism with pointers to members syntax the same way it's done with an explicit call ( obj.Base::Func() )? It would be...
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
5
by: Arquitecto | last post by:
Hi ,i would like to give u a piece of code and discuss if it is possible to call the hidden_function without modify code to call it . I have read on internet about buffer overflows and i can...
2
by: vinodkus | last post by:
dear sir/madam 1 I want to call an asp function through onclick method of button. 2I want to call an asp function through javaScript Thanks in advance
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.