Code::Blocks Forums
User forums => Using Code::Blocks => Topic started by: Rapster on December 02, 2009, 10:05:23 am
-
Hi all!
I've just started learning c++ (so you could call me a novice!). Anyway, I had an issue that I have now resolved. But what I'd like to know is why the error happened in the first place.
Let me explain:
I was following a tutorial for a console application. There was only one page of code named main.cpp. I hadn't created a project in Code::Blocks, I just created a new empty file, and coded away. When I compiled and ran the code, it worked just fine.
Then, as the tutorial instructed, I split the code into main.cpp and GradeBook.h. Again, after compiling and running, it worked just fine.
Then the tutorial dictated that the GradeBook.h file should be itself split into 2 parts: GradeBook.h containing the class's interface (prototypes), and GradeBook.cpp, which contained the member function definitions for the GradeBook class in the header file.
This time, however, when I compiled and ran the files it didn't work. Instead, I got the following error message: undefined reference to '_WinMain@16'.
Of course, at first I thought I had miscoded something somewhere, but after 30 minutes of thorough checking the code was fine. The next morning, I decided to create a console project and I added the same files that I had already written into the new project (without changing a single line of code!).
This time, when compliled and run, it worked just fine.
So, I haven't really understood what or why the error is/happened. Can someone explain any underlying issues, please?
Many thanks in advance,
Tony
-
If you compile a single file to an executable with CB, that file must contain a main() function. WinMain is the windows version of main() that MinGW supplies if it sees a main() function in your code. If it does not, you will get the linker error.
If you create a project, then all files (including the one that actually contains main()) are linked, so there is no problem. CB is really not intended for compiling single files - you should always prefer to use a project.
-
Hi Zabzonk!
I don't want to argue with someone who obviously knows better than me, but like I said in my original post, it compiled fine when there was just the one file (main.cpp) that contained the function int main() and a class.
It even compiled fine when I split the file into main.cpp (int main()) and GradeBook.h (class).
It was only when I split GradeBook.h into 2 files (GradeBook.h for the interface (prototypes), and GradeBook.cpp for the member function definitions) that Code::Blocks didn't like it. So, my question remains: why did Code::Blocks 'suddenly' decide that it wouldn't compile the files, when all along my files weren't contained within a 'project'?
Still puzzled,
Tony
-
Hi Zabzonk!
I don't want to argue with someone who obviously knows better than me, but like I said in my original post, it compiled fine when there was just the one file (main.cpp) that contained the function int main() and a class.
It even compiled fine when I split the file into main.cpp (int main()) and GradeBook.h (class).
It was only when I split GradeBook.h into 2 files (GradeBook.h for the interface (prototypes), and GradeBook.cpp for the member function definitions) that Code::Blocks didn't like it. So, my question remains: why did Code::Blocks 'suddenly' decide that it wouldn't compile the files, when all along my files weren't contained within a 'project'?
Still puzzled,
Tony
Your error is not a compiler, but a linker error.
That means C::B still compiles fine, but fails to link.
A header file gets included (if it can be found in the includepath the compiler uses to find headers), so this works fine.
But for linking two (object-)files together C::B has to know which files should be linked. And how should it decide which files to use ?
That's the cause to create projects in C::B (and other IDE's) or to create makefiles.
If you turn on full commandline-logging in "Settings -> Compiler and debugger... -> Global compiler settings -> [the_compiler_you_use] -> Other settings (rightmost tab) -> Compiler loggin" you can see which commands are send to the compiler/linker and where the difference is between a single file and a project with multiple files.
By the way: your book/tutorial should explain the steps needed to compile and link files, what happens if headers are included and how to use prebuild libraries/dlls.
-
Hi Jens!
I think that all makes sense now. I'll check out your suggestion and take a close look.
Cheers,
Tony
PS: how do I mark your post as 'answer'?
-
Hi Jens!
I think that all makes sense now. I'll check out your suggestion and take a close look.
Cheers,
Tony
PS: how do I mark your post as 'answer'?
Most People just edit the title of the first and last post and add the prefixed [SOLVED] to it.
Tim S.
-
PS: how do I mark your post as 'answer'?
I think you may be mistaking this site for Stack Overflow :-)