This tutorial gives you easy-to-follow instructions, with screenshots, for setting up a compiler (the
Code::Blocks, Free Download by The Code::Blocks Team. The answers here failed to solve my problem! My friend got it fixed for me. The solution was pretty simple and straight forward. The problem that occurred was that compiler couldn't chain itself with the MinGW (Minimalist GNU for Windows) which could have been modified by some other make files for makefiles softwares like CMake etc.
MINGW compiler), a tool that will let youturn the code that youwrite into programs, and Code::Blocks, a free development environment for C and C++. This tutorial explains how to install Code::Blocks on Windows 2000, XP, Vista or Windows 7. Note: the easiest way to get a compiler is to use Cee Studio, from our sponsor. It will let you run your code in your browser, without installing anything. If you're running Linux, go here to learn how to use GCC; if you're on OS X, go here to get set up using Apple XCode.Step 1: Download Code::Blocks
- Go to this website: http://www.codeblocks.org/downloads
- Follow the link to 'Download the binary release' (direct link)
- Go to the Windows 2000 / XP / Vista / 7 section
- Look for the file that includes mingw in the name. (The name as of this writing was codeblocks-10.05mingw-setup.exe; the 10.05 may be different).
- Save the file to your desktop. It is roughly 74 megabytes.
Step 2: Install Code::Blocks
- Double click the installer.
- Hit next several times. Other setup tutorials will assume you have installed in C:Program FilesCodeBlocks (the default install location), but you may install elsewhere if you like
- Do a Full Installation
- Launch Code::Blocks
Step 3: Running in Code::Blocks
You will be prompted with a Compilers auto-detection window:When you get the compiler auto-detection window, just hit OK.Code::Blocks may ask if you want to associate it as the default viewer forC/C++ files--I'd suggest you do.Click on the File menu, and under 'New', select 'Project...'
The following window will come up:
Click on 'Console Application' and hit the 'Go' button.
Click next until you get to the Language Selection Dialog:
You'll be asked to choose whether you want to use C or C++. If you'renot sure, use C++. Otherwise, choose based on the language you are learning. (You can find tutorials here on both C and C++.)
After clicking 'Next', Code::Blocks will then prompt you with where you'd like to save the consoleapplication:
I'd recommend you put it in its own folder, as it may createseveral files (this is especially true if you create other types of projects). You will need to give your project a name, anything willbe fine.
Clicking 'Next' again will prompt you to set up your compiler:
You don't need to do anything here. Just accept the defaults by hitting'Finish'.
You can now open the main.cpp file on the left:
(You may need to expand the contents of the 'Sources' folder if you don't see main.cpp.)
At this point, you will have your main.cpp file, which you can modify if you like. For now, it just says 'Hello World!', so we can run it as is. Hit F9, which will first compile it and then run it.
You now have a running program! You can simply edit main.cpp and then hit F9 to compile it and run it again.
Now that you've finished setting your compiler up, it's time to learn toprogram: Intro toC++ (or if you're learning C, Intro to C).
Troubleshooting
The most common error people see if things don't work is a message like
'CB01 - Debug' uses an invalid compiler. Probably the toolchain path within the compiler options is not setup correctly?! Skipping...'
First, make sure that you downloaded the right version of Code::Blocks, theone that included MinGW. If that doesn't solve the problem, it is likely aproblem with compiler auto-detection. Here's how you can check your current'auto-detected' state. Go to 'Settings|Compiler and Debugger...'. Then on theleft, choose 'Global Compiler Settings' (it has a gear icon) and on the right,select the 'Toolchain executables' tab. This tab has a 'Auto-detect' buttonthat you can use. That might fix the problem--if it doesn't, you can manuallyfill out the form. Here's a screenshot demonstrating what things look like onmy system. Change the path marked 'Compiler's installation directory' if youinstalled to a different location, and make sure everything else is filled in as shown.
Once you've done that, try pressing F9 again to see if you get a running program.
Continue to Intro toC++ (or Intro to C).
** Disclaimer **Much to my dismay this tutorial is by far the most visited page on my website, why does this upset me? It upsets me because as glad as I am to see people using open source tools like OpenCV and MinGW rather than proprietary or commercial alternatives I feel strongly that developers should be using Linux not Windows for coding, especially for C++.
Why should you use Linux? There’s a lot of reasons in my opinion but right now I am going to keep it simple. It will make you a better coder. period. Most people I know barely understand setting up their own C++ projects and linking to 3rd party libraries etc. and using Linux is the best way to see and learn how this works. I also personally recommend staying away from IDEs.
Linux is quite often the first priority for developers of open source tools and windows support is sometimes an after thought. You’re obviously interested in open source or you wouldn’t be here - so I’m telling you to take the plunge, go all in, close this tab and grab an image of Ubuntu or Mint if you want to be just like me and become enlightened!
Step 1: Install minGW
MinGW is a c/c++
compiler for windows, head to their website and download the latest version. http://sourceforge.net/projects/mingw/files/
Install to the default location C:MinGW
From the options install mingw32-base
and mingw32-gcc-g++
`, you can also install the other components if you wish, but all you need is the c++ compiler (g++).
Step 2: Add minGW to system path
Navigate to Control Panel -> System -> Advanced System Settings
and then:
Type a semi colon after the last entry in path
and then paste your MinGW path (it should be C:MinGWbin
if you chose the default location).
Afterwords open up a command prompt and type path
to make sure it worked (you should see minGW
somewhere in the print out, probably near or at the end).Programs will need to be restarted for this change to take effect.
Step 3: Install Code::Blocks
Code::Blocks is an IDE (integrated development environment). Head to their website and download the latest version (codeblocks-10.05-setup.exe
at the time of writing)
Install it to the default location
When the installer is finished click yes to run Code::Blocks
then go to Settings -> Compiler and Debugger
Under the Toolchain Executables select GNU GCC Compiler from the drop down and then press AutoDetect
verify that Code::Blocks has found MinGW
If you like now might be a good time to test your Code::Blocks and MinGW setup with a simple Hello World C++ program.
Step 4: Install OpenCV
OpenCV is a library of Computer Vision functions. Head to their website and download the latest version (2.4.2 for Windows)
Click on the OpenCV-2.4.2.exe
and choose C:
as the extract directory
OpenCV is now installed – but not configured with Code::Blocks
** Update **If this is your first time through the tutorial check if the supplied pre-built binaries will work for you and skip the next section. If you’ve already tried and had issues or if you need to build OpenCV with custom configuration then continue with the next section.
I’ve been talking to the OpenCV devs about some of the issues people (and me) have been having with the latest pre-built binaries, what you need to know is they are discontinuing pre-built binaries for MinGW. From now on you will have to build your own, I have included instructions for how to make your own binaries and it’s pretty straight forward. I still prefer MinGW to other compilers on windows (well actually I prefer Linux, see above) and I hope this tutorial will continue to be useful.
Code Blocks 10.05 Mingw Free Download 64-bit
Step 5: Compiling OpenCV
First you’ll need to download and install cmake
Open cmake and select C:opencv
as the source directory and C:opencvbuildx86mingw
as the directory to build the binaries (you could select any directory but choosing this one will overwrite the pre-built OpenCV binaries and then therest of the tutorial is the same.
Click configure, choose minGW makefiles wait and then click generate.
When cmake is done we need to open a command prompt in the build directory, so navigate to C:opencvbuildx86mingw
then shift right click and choose open command window here then type mingw32-make
.
Mingw will now start compiling OpenCV, this will take a bit so feel free to do something else, when you come back type mingw32-make install
and continue with the rest of the tutorial.
Step 6: Add OpenCV to the system path
C:opencvbuildx86mingwbin
(use the same process as above)
Note: Add x86 binaries regardless of your system type (32bit or 64bit) because minGW is 32bit.
Verify that both MinGW and OpenCV are in your system path Make sure you restart Code::Blocks before continuing if you have it open.
Step 7: Configuring Code::Blocks with OpenCV
Make a new Code::Blocks Project:
right click on your project and choose build options:
You can also change the global compiler settings from the menu bar at the top right.
Again Note – we are using 32-bit binaries even though the system is 64-bit because the compiler is 32-bit.
Now run this simple OpenCV “Hello World” program to test that the install has worked.
Download any image you want, rename it test.png
or hard code its name and place it in the top of the project directory.
Note – if you run your .exe
from outside of code blocks the image needs to be in the same directory as the .exe
.
Code Blocks 10.05 Mingw Free Download 32-bit
As I mentioned earlier you can also configure OpenCV using the global compiler and debugger settings and the steps are the same, this means that every new project is ready to go with OpenCV. You can also choose file -> save project as template
, This allows you to choose the option new from template and avoid the configuration each time.