473,503 Members | 13,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ ofstream multifile in Linux

18 New Member
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!
Feb 13 '08 #1
4 2420
mac11
256 Contributor
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
Feb 13 '08 #2
everlast
18 New Member
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 :) )
Feb 15 '08 #3
mac11
256 Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "testofstream.h"
  3.  
  4. using namespace std;
  5.  
  6. int main () 
  7. {
  8.  
  9.   ofstream outfile;
  10.   outfile.open ("test.txt");
  11.   if (outfile.is_open())
  12.   {
  13.       go( outfile );
  14.       outfile.close();
  15.   }
  16.   else
  17.   {
  18.       cout << "Error opening file";
  19.   }
  20.   return 0;
  21. }
  22.  
testofstream.h:
Expand|Select|Wrap|Line Numbers
  1. #ifndef TESTOFSTREAM_H
  2. #define TESTOFSTREAM_H
  3.  
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. void go( ofstream &outfile );
  9.  
  10. #endif
  11.  
testofstream.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "testofstream.h"
  2.  
  3. void go( ofstream &outfile )
  4. {
  5.     outfile << "Did this work?" << endl;
  6. }
  7.  

compile with the command
Expand|Select|Wrap|Line Numbers
  1. g++ main.cpp testofstream.cpp -o testofstream
and when you run it you should find a file "test.txt" has been created.
Feb 22 '08 #4
everlast
18 New Member
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. : )
Feb 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1576
by: Sven Erik Matzen | last post by:
Hi there, I'm searching for a "convinient" way to compile my source into a multifile assembly (multiple files for one assembly). I need this to optimize performance while loading the assembly...
0
1997
by: m vaughn | last post by:
I wanted to hold information about open files in a struct and be able pass it around, but the behavior is not what I would expect. Can someone tell me why this doesn't work? (Even better, what is...
1
3408
by: tornado | last post by:
hi all, This is quite puzzling me why is that ofstream constructor is unable to open a file for writing while, a ofstream "open" method can ! The c++ book by Herbert Schildt which i m readin...
1
1184
by: David Isaac | last post by:
Why is a MultiFile object not an iterator? For example if mfp = multifile.MultiFile(fp)I cannot dofor line in mfp: do_somethingRelated:MultiFile.next seems badly named.(Something like...
12
2761
by: Pollux | last post by:
I'm having trouble doing someting apparently simple. I'm still not very familiar with Visual Studio 2003, so apologies if I've missed something obvious. Basically my problem is the following. I...
3
1514
by: Lord2702 | last post by:
Sun. Aug. 22, 2004 2:20 PM PT How to create multifile assembly in Managed Visual C++ ? Using VSIDE. Please do not point me to MSDN pages, I already read those pages, and it only says, that you...
2
1896
by: avenpace | last post by:
Hi, I had error in my script like "sudden EOF in MultiFile readline()" Why such error occur
1
2872
by: shyam | last post by:
Hi All I have a application which is basically a log manager and accepts log messages from other processes and logs them in files ( one file per process per day ). The application manages the...
15
5972
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7366
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
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.