C call C++ fuction and iostream

Discussion in 'C++' started by Cat, Aug 18, 2003.

  1. Cat

    Cat Guest

    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


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Cat, Aug 18, 2003
    #1
    1. Advertising

  2. 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.
     
    Buster Copley, Aug 18, 2003
    #2
    1. Advertising

  3. Cat

    llewelly Guest

    Cat <> 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! ]
     
    llewelly, Aug 20, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Tiger
    Replies:
    10
    Views:
    5,630
  2. ai@work
    Replies:
    9
    Views:
    556
    Ron Natalie
    Dec 16, 2004
  3. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    786
    red floyd
    Jan 15, 2005
  4. red floyd
    Replies:
    3
    Views:
    544
    Dietmar Kuehl
    Mar 8, 2005
  5. Replies:
    2
    Views:
    177
    Bob Barrows [MVP]
    Aug 13, 2007
Loading...

Share This Page