Author Topic: Single source, without a project file?  (Read 7934 times)

DarkStar

  • Guest
Single source, without a project file?
« on: January 14, 2006, 07:46:22 pm »
I am learning C++ and, for now, I am only making programs with one source file.  Is there any why to use Code::Blocks with a single source files without creating project files as well?

Or are projects so important in larger programs that I should learn how to use them now?

Offline 280Z28

  • Regular
  • ***
  • Posts: 397
  • *insert unicode here*
Re: Single source, without a project file?
« Reply #1 on: January 14, 2006, 07:55:39 pm »
You can create an empty project from the Project menu, and it's very easy to add just that one source file to it.

I suggest you learn how to do that since it's not very hard at all, and you'll get the full features of the editor (debugger, compile options, etc) if you do.
78 280Z, "a few bolt-ons" - 12.71@109.04
99 Trans Am, "Daily Driver" - 525rwhp/475rwtq
 Check out The Sam Zone :cool:

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Single source, without a project file?
« Reply #2 on: January 14, 2006, 08:08:15 pm »
I agree with 280Z28. It would be better to learn since the beginning to use projects, even if they have just 1 file. When I have begun to study programming, we just used a text editor and command-line compiler (under UNIX). This was not very comfortable (e.g., debug). Later, I had to use an IDE for a larger project with several projects and a workspace :D. And I have lost precious time to learn about projects and workspaces stuff and make me accustomed to use them.

Michael
 

RKCole

  • Guest
Re: Single source, without a project file?
« Reply #3 on: January 14, 2006, 08:13:46 pm »
Hello, DarkStar.

I concur with what 280Z28 said.

When I was in college, my C++ professor only had the class create single source files for each assignment.  I've tested plenty of compilers, and it seems that Visual C++ would completely comply with doing this.  Later I learned the importance of projects.  Things seem to work a lot better through the creation of projects as well.

You can create a project via New->Project and then just make sure you are creating a Console Application (unless you are working on GUI Application projects) and that the file(s) options has C++ source as the selected option.  This should already create a "main.cpp" file in the project for you to begin working with.

From what I've learned through working with compilers is to make sure that "using namespace std;" is included before your "main" function so you can use "cout", "cin", and others without adding "std::".

The good thing about a project is that if you need to add extra header files and such, you can add them to the project so that everything for your application is bundled together in one place.

Although this post has become rather lengthy (I apologize for this), I hope that I've been of some help.

Please take care.

Offline 280Z28

  • Regular
  • ***
  • Posts: 397
  • *insert unicode here*
Re: Single source, without a project file?
« Reply #4 on: January 14, 2006, 09:02:52 pm »
I also suggest learning how to program without the using directive. It is bad practice to use it, and stops the whole reason namespaces were added to the language in the first place. The core code::blocks code is written without ever using it. :)
78 280Z, "a few bolt-ons" - 12.71@109.04
99 Trans Am, "Daily Driver" - 525rwhp/475rwtq
 Check out The Sam Zone :cool:

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Single source, without a project file?
« Reply #5 on: January 14, 2006, 09:06:52 pm »
I also suggest learning how to program without the using directive. It is bad practice to use it, and stops the whole reason namespaces were added to the language in the first place. The core code::blocks code is written without ever using it. :)

Yes, right. This topic was discussed (or partially discussed) sometimes ago already :D.

Michael

Offline yop

  • Regular
  • ***
  • Posts: 387
Re: Single source, without a project file?
« Reply #6 on: January 14, 2006, 09:08:08 pm »
From what I've learned through working with compilers is to make sure that "using namespace std;" is included before your "main" function so you can use "cout", "cin", and others without adding "std::".
You can always do something like:
using std::cout;
using std::cin;
There is a lot of discussion about the usefullnes of "using" a whole namespace against "using" some symbols only.

EDIT: Wow that using namespace thingy triggered immediate responces, I didn't even have the time to press the post button and two replies allready :shock:
« Last Edit: January 14, 2006, 09:10:11 pm by yop »
Life would be so much easier if we could just look at the source code.

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Single source, without a project file?
« Reply #7 on: January 14, 2006, 09:24:01 pm »
And nobodoy has told him how to use it with single source files.

Projects are really important in programming and have many advantages, but for small test programs I think it's too much. When you start dividing your code in many source files (at least two), it's time to use projects and see why they are so useful, IMHO (and please, don't start a war here).

A drawback of single files compiling with Code::Blocks is the debugger won't work with them (it still needs a project, but it's just an implementation issue).

I'd suggest you getting one of the nightly builds 'cause there have been some improvements in the single file compiling since RC2.

Now, it's just a matter of creating a new file, writing your code, clicking on build and clicking on run (there's also a build & run button :))

RKCole

  • Guest
Re: Single source, without a project file?
« Reply #8 on: January 14, 2006, 11:00:41 pm »
So it is better to use std::coud, std::cin, and so forth?

I didn't have much of an advantage while in my C++ Programming I and C++ Programming II (advanced) courses at my college due to the fact that our professor wrote his own books for the courses, and he did not know a lot about C++. (Makes no sense to me on that one...)  So to some point, I am going to have to re-learn a lot of things.

Thanks for the input about that, though, because I was always curious about that particular topic, and everyone always adised me to place that "using namespace std;" line in my code.  Sorry if I caused any problems or anything. :)

Take care.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: Single source, without a project file?
« Reply #9 on: January 14, 2006, 11:22:14 pm »
using namesace std; 
that's the easy way, say one thing and get all, and this makes evrything visible again, which is not that good. namespaces divide, hide, and prevent collisions.

it is indeed better to put using std::cout; for example if you only want cout.

Meyers and Sutter advice it like that also.

RKCole

  • Guest
Re: Single source, without a project file?
« Reply #10 on: January 15, 2006, 12:46:10 am »
So basically if I wanted to use cin, cout, and endl in a program I could use:

using std::cin;
using std::cout;
using std::endl; (I believe that's in the standard library)

Wow...those courses left me a bit more behind than I thought.  But thanks for the insight, there.  I'll do it that way from here on out.

Take care.

takeshimiya

  • Guest
Re: Single source, without a project file?
« Reply #11 on: January 15, 2006, 01:14:57 am »
I think it's better to not use a using statement never. It more or less defeats the purpose of the namespaces.

DarkStar

  • Guest
Re: Single source, without a project file?
« Reply #12 on: January 15, 2006, 02:12:41 am »
Wow!  Just a couple of hours and 11 replys.  Thanks!

After reading all that, I will keep using projects, overkill or not for a 20 line program.

Also, I have noticed that if you ask any two c++ programers about using namespace std;, you almost always get an arguement out of it. :lol:

For myself, I am trying to learn C++ from a book (this one) so for now I will just go along with what the book shows.  That happens to be using namespace std; inside any function that uses it, insted of being global (as of chpter 4).

I am personaly not going to type std:: in front of every cout and cin I use.  Not being a very fast tiper, I need the time savings. :)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Single source, without a project file?
« Reply #13 on: January 15, 2006, 02:37:50 am »
That happens to be using namespace std; inside any function that uses it, insted of being global (as of chpter 4).

I am personaly not going to type std:: in front of every cout and cin I use.  Not being a very fast tiper, I need the time savings. :)
And there is nothing wrong with it! Do what you feel comfortable with.

Personally, I don't use the using keyword most of the time simply because I like the scope resolution operator (and I type fast enough, so it doesn't matter). To me, explicit scope resolution looks nice, and it helps me to remember where I pulled something from.
But that does not mean that using the using keyword is generally bad practice. Wheter or not to use that keyword is a personal preference, nothing more.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."