473,569 Members | 2,598 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 2430
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.cp p:
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
1581
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 over the internet (I want to keep the files small and to download only the things that are needed). Is there a way to define inside visual studio...
0
2007
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 the proper way to do this, eg, without the pointers and such). As is probably obvious, I am not terribly knowledgable here. The *fs.of<< ......
1
3417
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 mentions that ofstream default constructor can open a file. But when i tried to open a file it failed to open. Then i tried it by open method which...
1
1193
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 next_section would be better.)Is this just historical accident or am I missing the point?Thanks,Alan Isaac
12
2763
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 have 2 files that contain each one a very simple class. I compiled them as .netmodules. Both classes are under the same namespace. I then bundled...
3
1518
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 can create, (but no info, or steps how to create?) it in VSIDE, but no STEPS are given. I want this solution Only and Only in Managed VC++, with the...
2
1903
by: avenpace | last post by:
Hi, I had error in my script like "sudden EOF in MultiFile readline()" Why such error occur
1
2877
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 creating of log files, formatting the incoming log messages with proper time stamp and writing the logs in the respective log files. I am using C++...
15
5982
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() .... std::ofstream fout; fout.open("hello.out"); fout<<setfill('-')<<setw(20)<<"-"<<setfill(' ')<<endl; fout<<"hello "; // this is line 139
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.