Chapter 2: Compiling Magick++ program In this chapter, we will look at a demonstration program to understand the method for compiling and linking a Magick++ program. In all the subsequent chapters, programs will be displayed inside a box and will also be italicized to differentiate from the surrounding text. The line numbers would also be provided for easy reference. Parts of the code will also be highlited depending on the discussion pertinent to that chapter. Parts of a Magick++ program Here is an example of a simple program in Magick ++ to read and write an image file.
1. #include 2. #include 3. using namespace std; 4. using namespace Magick; 5. 6. int main(int argc,char **argv){ 7. 8. //Create an instance of the class 9. Image master; 10. 11. //Read the inputfile 12. master.read(argv[1]); 13. 14. //Display image 15. master.display(); 16. 17. //Write the file to a different format 18. master.write(argv[2]); 19. 20. return 0; 21. 22. }
Prog 2.1: Demonstrate the parts of a Magick++ program The Magick++.h file has all the class definition and is included in line #1. Line #4 should be added to differentiate the classes in other namespace from that in Magick namespace. Instead, the programmer can also add Magick:: to each of the Magick++ classes.
Compiling a Magick++ program In this chapter and all subsequent chapters, we assume that the program will be compiled using a *nix machine and using a c++ compiler, although the code was also tested using g++ compiler. To compile your code, ensure that the path to libraries and include files of ImageMagick are included in the *nix PATH. The methods for setting the PATH may vary across different
systems and will not be discussed here. Magick++-config is a shelll script that assists programmers by bringing together the various options needed for compiling. To compile a Magick++ program called first.cpp (given above) and to produce an executable first, use the following command. c++ -o first first.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs` In the above command, Magick++-config script is requested to run with cxxflags – to provide code optimization cppflags – to include header files ldflags – to include library files libs – to include other libraries like jpeg, tiff etc
1. 2. 3. 4.
In this chapter, we looked at the different parts of a typical Magick++ program and also the methods to compile the same. In the next two chapters, we will concentrate on understanding the various classes that form the basis of Magick++.