C++ ofstream multifile in Linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • everlast
    New Member
    • Nov 2007
    • 18

    C++ ofstream multifile in Linux

    Must i use the MAKE utility in linux bash to build multifile projects?

    I tried to write the usual .h and .cpp files that hold the definition and recspectively the declaration of a function, I included the .h file in my main.cpp and called the function within main. when I used g++ to compile main.cpp there was an error: "undefined reference to fun(int)"

    Can one use g++ to build multifile projects? Can anyone tell me how to use make, or where to find the best tutorial for it?

    Thank You!
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by everlast
    Must i use the MAKE utility in linux bash to build multifile projects?

    I tried to write the usual .h and .cpp files that hold the definition and recspectively the declaration of a function, I included the .h file in my main.cpp and called the function within main. when I used g++ to compile main.cpp there was an error: "undefined reference to fun(int)"

    Can one use g++ to build multifile projects? Can anyone tell me how to use make, or where to find the best tutorial for it?

    Thank You!
    You dont have to use make, but people usually like to when they start getting lots of files.

    Do you have more than 1 .cpp file? If so you have to compile all of them, not just main.cpp (i.e. g++ file1.cpp file2.cpp main.cpp -o myprogram)

    I like this site for make reference
    http://www.delorie.com/gnu/docs/make/make_toc.html

    Comment

    • everlast
      New Member
      • Nov 2007
      • 18

      #3
      Originally posted by mac11
      You dont have to use make, but people usually like to when they start getting lots of files.

      Do you have more than 1 .cpp file? If so you have to compile all of them, not just main.cpp (i.e. g++ file1.cpp file2.cpp main.cpp -o myprogram)

      I like this site for make reference
      http://www.delorie.com/gnu/docs/make/make_toc.html
      first: THANK YOU

      I've tried g++, and it worked. BUT, the problem was in the code. Why can't I use ofstream class objects (or pointers) as arguments in functions in header files? i.e:

      #ifndef MYHEAD_H
      #define MYHEAD_H

      void myFun (ofstream* );

      #endif

      I'll look up the link for make anyway, that seems usefull : alot of people seem to be using it (my Bsc mentor as well :) )

      Comment

      • mac11
        Contributor
        • Apr 2007
        • 256

        #4
        Originally posted by everlast
        Why can't I use ofstream class objects (or pointers) as arguments in functions in header files? i.e:

        #ifndef MYHEAD_H
        #define MYHEAD_H

        void myFun (ofstream* );

        #endif
        Maybe you figured out your problem already, it's been a while. In case you haven't... you can pass streams around - they're objects. Here is a few samples to show you how to make it go:


        main.cpp:
        [CODE=cpp]
        #include <iostream>
        #include "testofstream.h "

        using namespace std;

        int main ()
        {

        ofstream outfile;
        outfile.open ("test.txt") ;
        if (outfile.is_ope n())
        {
        go( outfile );
        outfile.close() ;
        }
        else
        {
        cout << "Error opening file";
        }
        return 0;
        }
        [/CODE]

        testofstream.h:
        [CODE=cpp]
        #ifndef TESTOFSTREAM_H
        #define TESTOFSTREAM_H

        #include <fstream>

        using namespace std;

        void go( ofstream &outfile );

        #endif
        [/CODE]

        testofstream.cp p:
        [CODE=cpp]
        #include "testofstream.h "

        void go( ofstream &outfile )
        {
        outfile << "Did this work?" << endl;
        }
        [/CODE]


        compile with the command
        Code:
        g++ main.cpp testofstream.cpp -o testofstream
        and when you run it you should find a file "test.txt" has been created.

        Comment

        • everlast
          New Member
          • Nov 2007
          • 18

          #5
          Originally posted by mac11
          Maybe you figured out your problem already, it's been a while. In case you haven't... you can pass streams around - they're objects. Here is a few samples to show you how to make it go:
          thanks! i've figured it out (with help of course). I didn't declare the std namespace in my header, so compiler had no idea what is ofstream. : )

          Comment

          Working...