Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: On the very raw basics of programming...  (Read 372 times)

noodle0117

  • Bay Watcher
  • I wonder what would happen if I pull it.
    • View Profile
On the very raw basics of programming...
« on: April 06, 2012, 05:09:36 am »

I've recently gotten into programming and I've been reading quite a few tutorials on C++. I've been able to successfully write a few simple programs which is nice and all, but there are just a few really basic things that are bothering me.

1. What exactly is a "project"?
I think its like a step needed before writing the code, but what exactly does it do? How is a "project" different from a "program"?

2. What's the difference between running a program and building a program? (I know it sounds stupid to ask, but google says nothing)

3. What exactly is a workspace? Namely, what does building/rebuilding a workspace mean? (further clarification in image below)


I'm currently using Codeblocks as an IDE, although some people say I should switch to Eclipse. Does it make that much of a difference?
Any advice appreciated.
Logged
Cortex Command, where physics meet ludicrous gibs and explosions.
My personal mod, The Bass Cannon
Blow stuff up with sound!

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: On the very raw basics of programming...
« Reply #1 on: April 06, 2012, 05:32:44 am »

  • A project is simply a means of organising the various files that go into making your program (mostly code files, but maybe some object libraries as well)
  • Building a program involves compiling the code files and linking them and the libraries used into an executable file. I would assume the the "build" command always builds the program, whereas the "run" command doesn't build if you haven't changed any of the project files
  • My experience with Visual Studio suggests that a workspace is composed of multiple projects, and the ".. workspace" commands simply perform the task on all the projects in the workspace. Codeblocks may be different in this regard though.
Hope this helps.
Logged

Sir Finkus

  • Bay Watcher
    • View Profile
Re: On the very raw basics of programming...
« Reply #2 on: April 06, 2012, 06:31:09 am »

It's just a way an IDE organizes all the source files you want to include in a project.

If you're just starting out, I'd actually recommend just using a simple text editor and compiling with the command line.  Once you have a better understanding of what's going on with the code, you can tackle the IDE.

noodle0117

  • Bay Watcher
  • I wonder what would happen if I pull it.
    • View Profile
Re: On the very raw basics of programming...
« Reply #3 on: April 06, 2012, 11:01:43 am »

Can you elaborate a bit on the command line compiling?
Like if I wrote a basic hello world script and placed it on my desktop, how would I activate it via command line?
And does it work for other languages apart from C++?
Logged
Cortex Command, where physics meet ludicrous gibs and explosions.
My personal mod, The Bass Cannon
Blow stuff up with sound!

MorleyDev

  • Bay Watcher
  • Nerds who program and so-forth
    • View Profile
    • MorleyDev
Re: On the very raw basics of programming...
« Reply #4 on: April 06, 2012, 11:35:21 am »

It's difficult for me to simplify it, but I'll try...

When you install a compiler, such as g++ (which is included with mingw), you typically place it in the system path. This is where windows searches for programs to launch from the command prompt.

So if you type g++ it'll launch the g++ compiler. Try this by typing "g++ --version".

You should see an output listing the version number of g++. If not you need to add wherever g++.exe is found on your computer to the system path (this can be done by playing around in the settings somewhere, can't remember off the top of my head). It's often and usually best installed somewhere like C:\MinGW\bin.

This executable is actually what the IDE launches in order to compile your code into something the machine can execute. There are two steps to compilation:

1) First the IDE uses g++ with specific parameters to compile each of the source files to object files.
2) These are then all linked together with external libraries, creating a final executable.

This is often simplified by using a makefile, a file that contains the information related to these steps so they can be executed automatically by another program called make. A lot of IDEs will generate this file for you and all they do is call make on this file.

The same basic structure works for a lot of other languages and compilers. Pretty much every GNU compiler (g++ for C++, gcc for C, gfortran for Fortran) uses this set-up and most other compilers have one the same or similar. Even the JDK does this, but it compiles to Java bytecode which is then ran by the JRE.

So first you'd move the command prompt to the correct folder via the CD command, which changes the directory. So if it was in "C:\Users\Me" you'd enter CD C:\Users\Me.

Now, assuming here is your file called main.cpp you'd call

g++ -c -o "main.o" "main.cpp"

This will create a "main.o" file from "main.cpp". -c tells the program to compile the code. -o tells it that main.o is an object file to create.

Next you'd call

g++ -o "main.exe" "main.o"

This will create main.exe from main.o. You can now run main.exe. So if main.cpp contains your standard:

Code: [Select]
#include <iostream>

int main()
{
std::cout << "Hello World" << std::endl;
system("PAUSE");
return 0;
}

You'll see your standard Hello World program. To compile multiple files into one executable, it's a very similar process:

You can just compile each cpp file one at a time

g++ -c -o "func.o" "func.cpp"
g++ -c -o "main.o" "main.cpp"

And bring them all together in the final step

g++ -o "func.exe" "main.o" "func.o"

There are a lot of other things you can pass to g++ or gcc or whichever, obviously. Usually compilers simplify this by letting you select them from a list that also explains what each option does.
« Last Edit: April 06, 2012, 01:11:20 pm by MorleyDev »
Logged

zombie urist

  • Bay Watcher
  • [NOT_LIVING]
    • View Profile
Re: On the very raw basics of programming...
« Reply #5 on: April 06, 2012, 12:50:34 pm »

Can you elaborate a bit on the command line compiling?
Like if I wrote a basic hello world script and placed it on my desktop, how would I activate it via command line?
And does it work for other languages apart from C++?
Compiling means translating your source code to machine code.
It turns something like
Code: [Select]
int main ()
{
cout << "Hello World!";
return 0;
}
into something like this
Code: [Select]
000010 10010 01000 00000 10000 000000
which the computer can understand.

Some language, such as Perl and Python, are not compiled but are interpreted. This means that the source code is read by an interpreter, which performs the stuff in the source.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: On the very raw basics of programming...
« Reply #6 on: April 07, 2012, 01:36:03 am »

That is... less than technically accurate, but close enough. ;)

A compiler simply turns your written code from the language you wrote it in to some other language, presumably a lower-level one that's easier for your computer to understand. Some, like the Java compiler, do NOT compile into machine code, but some special code built to run on a virtual environment.

Zoomulator

  • Bay Watcher
    • View Profile
Re: On the very raw basics of programming...
« Reply #7 on: April 11, 2012, 05:03:20 am »

When you compile (build) a low level language, your source code is "translated" into machine code. This code is close to assembly in syntax, though the instructions aren't really humanly readable, hence people say it's just "ones and zeros" which it does boil down to in the end. I prefer thinking of it as groups of bytes forming instructions though, since an individual bit means nothing. It's beneficial to have this in mind when programming in C/C++.

The most common low level languages these days are C, C++ and perhaps Assembly (don't go there yet) that compiles down to machine code. The programmer/distributor has to compile their source to each specific system they desire to publish it on. Usually there's just one specific system (OS or machine architecture like x86) that the developer has in mind.

There's also high level languages, such as C# and Java that run on virtual machines(VM). Applications in these languages are often distributed in a tokenized (think replacing words with IDs) form of your source or simply left un-obfuscated. The VM reads the source "just-in-time" and does a simple compilation on the spot for the specific machine you're on. This lets you publish your application on any machine without having to worry about compiling specifics, because the VM sorts that out and makes a generalized machine interface for you. This interface adds an "indirection" to most parts of the language and generally makes it slower than C/C++. The VM languages does tend to cut down on production time since they have a more extensive standard library and more high level functionality.


Command line compiling isn't really necessary in my opinion. If you run on windows, get the Visual Studio express 2010 or 2008. It's well known among professionals and you tend to get good help using it. It also has the best "intellisense" and debugger money can't buy (it's free). Compiling a basic Hello World in Visual Studio requires no additional settings. Just start up a "solution" and add a "project". Solutions in VS are just groups of projects. Projects keeps track of the source files and the compiler options for building them.

If you're interested in programming for games, there's an excellent series that walks you through each step of setting up SDL and start making fancy graphics! Go to Lazy foo's SDL tutorials. He's got instructions for Visual Studio, Code blocks, and many other IDEs and compilers.
For the specifics of the language itself, http://cplusplus.com/ provides an excellent reference and tutorial which I've used for years when I need to looks something up.
« Last Edit: April 11, 2012, 05:04:51 am by Zoomulator »
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: On the very raw basics of programming...
« Reply #8 on: April 11, 2012, 08:55:02 am »

That is... less than technically accurate, but close enough. ;)

A compiler simply turns your written code from the language you wrote it in to some other language, presumably a lower-level one that's easier for your computer to understand. Some, like the Java compiler, do NOT compile into machine code, but some special code built to run on a virtual environment.
Java bytecode is a kind of machine code though? You can build an architecture to run (a particular generation of) Java bytecode if you're so inclined, but usually the system you're running the code on simulates such a machine because that makes it easier to ensure portability and upgrade things.
Logged