Using ImageMagick Library in C++ Linux Projects

This tutorial demonstrates the use of external Linux libraries in VisualGDB projects. We will create a Linux project that rotates images using the Magick++ library.

  1. Open Visual Studio. Select “File->New->Project”, then select “VisualGDB->Linux Project Wizard’. Specify project name and location and press the OK button:01-newproj
  2. On the first page select “Application” and press “Next”: 02-application
  3. On the next page select the Linux computer you want to use. We will now need to install the Magick++ library on it, so press the console button to the right from the computer selection to open a new SSH console:03-selectmachine
  4. Install the libmagick++-dev package. If you are running a Debian-based Linux distro, run the following command in the console window:
    sudo apt-get install libmagick++-dev

    04-libinstallWhen the package is installed go back to the VisualGDB wizard and press “Next”.

  5. To let the Linux machine easily access files from the Windows project folder we will use a shared folder instead of transferring the files. Select “Mount a shared Windows folder” and click “Setup”:05-mountfolder
  6. VisualGDB will suggest sharing one of the folders containing the project folder. In this example the project resides in c:\projects\RotateDemo and we will share c:\projects. To let the Linux computer access your shared folder you will need to provide valid credentials:06-newshare
  7. When you press OK VisualGDB will let you select the mount point where you want to map your folder. In this example we use /opt/VisualGDB-projects:07-mount
  8. Once the folder is shared and mounted use the diagram at the bottom if the wizard page to recheck the relation between the Windows and Linux paths:08-mountsetup
  9. Press “Finish” when done. VisualGDB will create a “Hello, World” project. Build it by pressing Ctrl-Shift-B:09-helloproj
  10. As we have configured shared folders, the Linux computer will be able to automatically access all files from the Windows project directory and write files to it as well. Create a file called ArrowRight.png in the project directory and draw an arrow in it:10-arrow
  11. Now we will need to configure IntelliSense to support the ImageMagick headers. Right-click on the project, select VisualGDB Project Properties, go to the IntelliSense page:11-includedirs
  12. Click “Add Remote”:12-addremote
  13. Click the browse button and select /usr/include/ImageMagick:13-browse
  14. Press “OK” in all the dialogs to save your settings. VisualGDB will add the directory to the IntelliSense search path. If you have installed other libraries recently, click “Edit Remote Cache”, select all directories and press “Reload”:14-reload
  15. Replace the contents of the main source file with the following:
    #include<Magick++.h>
    #include<stdio.h>
    
    int main(int argc, char *argv[])
    {
        if (argc < 3)
       {
           printf("Usage: %s <Input file> <Output file>", argv[0]);
            return 1;
       }
    
       printf("Opening...\n");
       Magick::Image img(argv[1]);
    
       printf("Rotating...\n");
       img.rotate(45);
    
       printf("Saving...\n");
       img.write(argv[2]);
    
        return 0;
    }

    If you try to build your project now you will get an error message:15-noinclude

  16. The error is shown because GCC include directories are managed separately from IntelliSense include directories. To fix the GCC includes open the flags.mak file and add /usr/include/ImageMagick to the INCLUDE_DIRS statement. If you build your project now the compilation will succeed but you will get several linker errors instead:16-linkerror
  17. To fix the linker errors you will need to add the Magick++ library to the LIBRARY_NAMES:17-linkdoneNote that the actual library file is called libMagick++.a and is located in one of the standard library directories. We omit the lib prefix in the flags.mak file as GCC traditionally requires it.
  18. Now we need to provide the command-line arguments to our program and run it. The easiest way to do it is to use the VisualGDB toolbar. Enable the toolbar by right-clicking in the toolbar area and selecting VisualGDB. Then enter “ArrowRight.png ArrowRotated.png” in the text field:18-args
  19. Press F5 to run the program to the end or F10 to step into it:19-completed
  20. Open the project folder in Explorer and check out the ArrowRotated.png file created by the program:20-rotated
  21. You can always see the program output from the last debugging session using the VisualGDB Program Output view in the Output pane in Visual Studio:21-log