codeblocks compiler c++
Moderator: Coders of Rage
codeblocks compiler c++
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.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: codeblocks compiler c++
#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.
#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.
Re: codeblocks compiler c++
Thanks sorta what I was looking for, but what if i had one file that was
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"
Code: Select all
#include <iostream>
{
int main
cout << "hello"
}
#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.
Re: codeblocks compiler c++
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
}
Re: codeblocks compiler c++
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.
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;
}
Re: codeblocks compiler c++
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...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; }
Re: codeblocks compiler c++
Thank you so much, this is what I was wondering. So when you putCC 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; }
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: codeblocks compiler c++
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
bye.h
hello.cpp
bye.cpp
main.cpp
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
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.
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
Code: Select all
#ifndef BYE_H
#define BYE_H
void bye();
#endif
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
}
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
}
Code: Select all
#include "hello.h"
#include "bye.h"
int main() {
hello();
bye();
return 0;
}
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
}
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.
Re: codeblocks compiler c++
Good job marauder, I think I learned something from that
Re: codeblocks compiler c++
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.
Re: codeblocks compiler c++
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 .
When One learns to Love, One must bear the risk of Hatred.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: codeblocks compiler c++
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.
Re: codeblocks compiler c++
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.
When One learns to Love, One must bear the risk of Hatred.
Re: codeblocks compiler c++
I love c++ as well, I'm tired of blitz right now...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.
Re: codeblocks compiler c++
I tried blitz but never really got into.eatcomics wrote:I love c++ as well, I'm tired of blitz right now...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.
When One learns to Love, One must bear the risk of Hatred.