codeblocks compiler c++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

codeblocks compiler c++

Post by jtst1 »

I'm using codeblocks compiler, and I was wondering if anyone knows how to run and compile a .cpp besides the main one. I have a source called main.cpp which runs perfectly, and another named test.cpp but i can't figure out how to run it.
When One learns to Love, One must bear the risk of Hatred.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: codeblocks compiler c++

Post by Ginto8 »

#1: Codeblocks is an IDE, not a compiler. It comes with the minGW GCC compiler I believe. ;)

#2: Check out this tutorial for how to do multiple files.

Hope I helped. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: codeblocks compiler c++

Post by jtst1 »

Thanks sorta what I was looking for, but what if i had one file that was

Code: Select all

#include <iostream>
{
int main
cout << "hello"
}
and then i have another file that says
#include <iostream>
{
int main
cout << "bye"
}[/code]
how would i make it so that when i compile it would first do "hello" and then do the linked file and say "bye"
When One learns to Love, One must bear the risk of Hatred.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: codeblocks compiler c++

Post by eatcomics »

well just to something like this

Code: Select all

//Hello.cpp
#include <iostream>
#include "GoodBye.h"

using namespace std;

int main(void) //I'm lazy shut up about the void
{
    //code for hello 
    bye();
    getchar();
}

Code: Select all

//GoodBye.h
#include <iostream>

using namespace std;

void bye()
{
      //print bye to the screen
}
Image
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: codeblocks compiler c++

Post by CC Ricers »

You can't have two functions named "main" in your program. Mostly for two reasons: 1. you can't re-declare a function with the same name (unless you use overloading) and 2. "main" is reserved and required as the primary function- it's like the "body" of a document.

What you would do is have two functions, name "hello" and "bye", put each in its own .h file and have each function print out the text you want. You could also put all your functions in one file, but for larger programs it's good practice to group them in separate files.

Code: Select all

#include <iostream>
#include hello.h
#include bye.h

int main() {

  hello();
  bye();
  return 0;
}

User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: codeblocks compiler c++

Post by eatcomics »

CC Ricers wrote:You can't have two functions named "main" in your program. Mostly for two reasons: 1. you can't re-declare a function with the same name (unless you use overloading) and 2. "main" is reserved and required as the primary function- it's like the "body" of a document.

What you would do is have two functions, name "hello" and "bye", put each in its own .h file and have each function print out the text you want. You could also put all your functions in one file, but for larger programs it's good practice to group them in separate files.

Code: Select all

#include <iostream>
#include hello.h
#include bye.h

int main() {

  hello();
  bye();
  return 0;
}

There you go, that would work well too... I should have put that in my post, it doesn't make much since to have hello be printed out in main, then have a function for bye, which is shorter than hello.... Whodda thunk it...
Image
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: codeblocks compiler c++

Post by jtst1 »

CC Ricers wrote:You can't have two functions named "main" in your program. Mostly for two reasons: 1. you can't re-declare a function with the same name (unless you use overloading) and 2. "main" is reserved and required as the primary function- it's like the "body" of a document.

What you would do is have two functions, name "hello" and "bye", put each in its own .h file and have each function print out the text you want. You could also put all your functions in one file, but for larger programs it's good practice to group them in separate files.

Code: Select all

#include <iostream>
#include hello.h
#include bye.h

int main() {

  hello();
  bye();
  return 0;
}

Thank you so much, this is what I was wondering. So when you put
hello();
bye();

that basically goes to the header file and runs what is inside right?
When One learns to Love, One must bear the risk of Hatred.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: codeblocks compiler c++

Post by MarauderIIC »

I'm guilty of skimming the thread here.
Sort of. Really what they gave you was a horrible implementation pattern. If you want to do it the "right" way (read: it scales well for larger projects, doesn't conflict with other common design patterns) you would do something like (don't get sticker shock now, keep reading) :

hello.h

Code: Select all

#ifndef HELLO_H
#define HELLO_H

void hello();

#endif
bye.h

Code: Select all

#ifndef BYE_H
#define BYE_H

void bye();

#endif
hello.cpp

Code: Select all

//any includes you need for this function to do its business, such as iostream
//using namespace std; if you need it
void hello() {
//code for hello
}
bye.cpp

Code: Select all

//any includes you need for this function to do its business, such as iostream
//using namespace std; if you need it
void bye() {
//code for bye
}
main.cpp

Code: Select all

#include "hello.h"
#include "bye.h"

int main() {
    hello();
    bye();
    return 0;
}
ALTHOUGH that pattern loses its grace in very small projects, such as this one. In which case, especially for this project, I would recommend THIS design pattern:

main.cpp

Code: Select all

//any includes you need, such as iostream
//using namespace std; if you need it

void hello(); //declare some function prototypes, tells compiler to expect this function later
void bye();

int main() {
    hello(); //since you have the prototypes you can go ahead and use them, the compiler will put it off
    bye();
    return 0;
}

void hello() {
//code for hello
}

void bye() {
//code for bye
}
In short you pretty much never want function bodies in your header files (with the exception of inline functions but that's a story for another day), otherwise you'll find yourself later going WHAT IS WRONG WHY WONT IT COMPILE and that'll be why.

What function calls do is they tell the computer that the next instruction isn't right next to the previous one, rather it's waaayyy over here. But, it goes to the same "waaayyyy over here" every time you call the function :)

EDIT: If you have multiple files, you have to add every file to your project, so that Code::Blocks knows to compile it as part of that .exe. Throughout your project, functions that take the same parameters have to be uniquely named. If you don't understand what I'm talking about, just uniquely name all your functions. :)
More edit: When something is compiled, it becomes a single file, hence the "waaayyyy over here" -- the "way over here" is inside the single .exe file. The separate .cpp files are just for us humans to keep things straight. The computer finds "hello() {" in the .exe and executes the code in the curly braces, then goes back to where it used to be.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: codeblocks compiler c++

Post by eatcomics »

Good job marauder, I think I learned something from that ;)
Image
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: codeblocks compiler c++

Post by CC Ricers »

Whoops, I guess I did forget that important rule. Don't get me wrong, I only use function prototypes in header files and put the whole definitions in cpp files, but for this simple example I didn't consider switching to a different coding pattern. I got used to separating my files when my work got more complex.
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: codeblocks compiler c++

Post by jtst1 »

Wow, thank you so much Marauder. I didn't quite understand everything you said, but I will keep this thread favorited so that I can use it for future refernce, and thanks to everyone else for your input. Great community to come to for help :mrgreen: .
When One learns to Love, One must bear the risk of Hatred.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: codeblocks compiler c++

Post by MarauderIIC »

You're welcome. If you use the debugger and set a breakpoint on the start of your program (right on top of "int main() {") and push "step into" (F11, IIRC) on all function calls, as opposed to step over (F10) which you'd want to push for stuff like cout and printf and other things, then you can see what I mean by "next instruction".
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: codeblocks compiler c++

Post by jtst1 »

Honestly I have no clue what you just said, but i'm sure i'll come to undestand it. I'm very new to c++, all my first program does is some basic math, and uses shorts/longs. In any case I love this language, and my book is getting here, well today i think. Again Thank you. :bow:
When One learns to Love, One must bear the risk of Hatred.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: codeblocks compiler c++

Post by eatcomics »

jtst1 wrote:Honestly I have no clue what you just said, but i'm sure i'll come to undestand it. I'm very new to c++, all my first program does is some basic math, and uses shorts/longs. In any case I love this language, and my book is getting here, well today i think. Again Thank you. :bow:
I love c++ as well, I'm tired of blitz right now...
Image
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: codeblocks compiler c++

Post by jtst1 »

eatcomics wrote:
jtst1 wrote:Honestly I have no clue what you just said, but i'm sure i'll come to undestand it. I'm very new to c++, all my first program does is some basic math, and uses shorts/longs. In any case I love this language, and my book is getting here, well today i think. Again Thank you. :bow:
I love c++ as well, I'm tired of blitz right now...
I tried blitz but never really got into.
When One learns to Love, One must bear the risk of Hatred.
Post Reply