NAG C Library with Visual Studio 2012

Tutorial on using Visual Studio 2012 with the NAG C Library

Start
64 Bit Applications
Calling NAG Example Programs
Creating Property Sheets
Applying Property Sheets

 

Create a New Project by selecting New Project from the FILE menu.

1.png

 

 

From the New Project Dialog select Templates -> Visual C++ -> General -> Empty Project and give the project a name.

2.png

 

 

Right click on Source Files under the Solution Explorer and Add a New Item.

3.png

 

 

Choose a C++ File (.cpp) and name it main.cpp.

4.png

 

 

Write a simple helloworld program in your main.cpp file.

5.png

 

 

Pressing F5 will run the program, but the output window will close instantly, so you will be unable to see the output. We can change this in the Project Properties.

6.png

 

 

Right click on Project Name under Solution Explorer and select Properties. Note you want the Project Properties here not the Solution Properties.

7.png

 

 

You will now see the Project Properties Page.

8.png

 

 

Under Configuration Properties -> Linker -> System, change the value of Subsystem to Console (/SUBSYSTEM:CONSOLE). This will allow us to run the program and see output. Note - You must press Ctrl + F5 to run, rather than just F5.

9.png

 

 

Save the changes by clicking OK and run the program by pressing Ctrl + F5. The program should now stay open until a key is pressed.

10.png

 

 

Now we will adjust the properties to be able to use the NAG C Library. Open the Project Properties once more.

11.png

 

 

First we need to add the location of NAG C Library header files under Configuration Properties -> VC++ Directories -> Include Directories. In this example we will be using the NAG C Library clw3223dal in which the default location of the headers will be found in C:\Program Files (x86)\NAG\CL23\clw3223dal\include on a 64 Bit Windows machine.

12.png

 

 

Second we need to add the location of the NAG C Library under Configuration Properties -> Linker -> Input -> Additional Dependencies. Again on a 64 Bit Windows machine the default location is C:\Program Files (x86)\NAG\CL23\clw3223dal\lib\CLW3223DA_nag.lib. Click OK 13.png

 

 

Include nag.h and naga00.h to your helloworld program and add a call to a00aac(). Run using the Local Windows Debug button again. This time you should see the About NAG C Library dialog.

11.png

That's it! You will need to add the include directory and the library path using the Project Properties Page for every project that you want to use the NAG C Library with. Alternatively this can be done using a Property Sheet. Instructions for doing can be found in the Creating A Property Sheet section. 

 

 

 

Using the 64 Bit NAG C Library

 

First we need to change the platform to 64 Bit. Select the right hand drop-down on the upper Task Bar (currently displaying Win32) and select Configuration Manager.

64-1.png

 

 

Select the drop-down under Platform that corresponds to your project, and click New.

64-2.png

 

 

Under New Platform, x64 should be selected. Under Copy settings from: choose Empty and click OK. Close the Configuration Manager.

64-3.png

 

 

We now need to edit the Project Properties as we did previously to use the 64 Bit Library. Right click on the Project Name under Solution Explorer.

64-4.png

 

 

Add the location of NAG C Library header files under Configuration Properties -> VC++ Directories -> Include Directories. The default location of the headers for the cl6i23dal library will be found in C:\Program Files\NAG\CL23\clw6i23dal\include on a 64 Bit Windows machine.

64-5.png

 

 

Next add the location of the NAG C Library under Configuration Properties -> Linker -> Input -> Additional Dependencies. Again on a 64 Bit Windows machine the default location is C:\Program Files\NAG\CL23\clw6i23dal\lib\CLW6I23DA_nag.lib. Apply the changes and click OK.

64-6.png

 

 

We can now run the program with Ctrl + F5 and we will be shown the NAG C Library Dialog.

64-7.png

 

 

Attempting to run the program with debugging (By pressing F5) will generate the information message shown below telling us debugging information cannot be found. To enable debugging for 64 Bit applications, we need to return to the Project Properties.

64-8.png

 

 

Under Configuration Properties -> C/C++ -> General, change the value of Debug Information Format on the right hand side to Program Database For Edit And Continue (/ZI).

64-9.png

 

 

Now select Optimization on the left, and change Optimization on the right to Disabled (/Od)

64-10.png

 

 

Under Configuration Properties -> Linker -> Debugging, change the value of Generate Debug Info to Yes. Apply the changes and click OK.

64-11.png

 

 

You will now be able to debug programs in 64 Bit mode.

64-12.png

 

 

 

Calling NAG Example Programs

 

Each routine in the NAG C library has an associated example program. These examples are distributed along with the NAG C Library. For the purpose of this tutorial, we will be calling the example program for the function g01eac. In Windows Explorer, navigate to the default installation location for clw3223dal. On a 64 Bit Windows machine this is "C:\Program Files (x86)\NAG\CL23\clw3223dal". Select the folder named examples.

e-1.png

 

 

Inside the examples folder you will see the source, data and results folder. The source folder contains the C examples code, the data folder contains the input data files for each example and the results folders contains the expected output or each routine. Select the source folder, and navigate to g01eace.c. Open this file in any text editor (such as Notepad) and copy the code.

e-2.png

 

 

Paste the code into the main.cpp file of a new project in Visual Studio.

e-3.png

 

 

We must now get the program to read from the associated data file. Return to Visual Studio and open the Project Properties on the right hand side.

e-5.png

 

 

Under Configuration Properties -> Debugging, edit Command Arguments.

e-6.png

 

 

You will need to use the redirection operator <  followed by the path to the file enclosed in double quotes ( " " ). The data file you need is in the main NAG folder, in examples\data. An example of the path is shown below.

e-7.png

 

 

Apply the changes and click OKThe program will now run when we press Local Windows Debugger, taking its data from the file we specified. The output should match that given in the results file, which can be found in the examples\results folder in the main NAG directory

e-9.png

 

 

 

Creating a Property Sheet

Property Sheets are useful when using the same properties over multiple projects

 

From View -> Other Windows bring up the Property Manager explorer.

p1.png

 

 

You should now see the Property Manager window.

p2.png

 

 

From the Property Manager click Add New Project Property Sheet.

p3.png

 

 

Give the sheet and name and click Add. You will now see a Property Page.

p4.png

 

 

Set up the Include directory and Library Path as you did before and click okay.

p5.png

 

 

Save the changes that you just made to the Property Sheet.

pSave.png

The Property Sheet will either be saved to C:\Users\{USER}\AppData\Local\Microsoft\MSBuild\v4.0 (AppData is a hidden folder) or to C:\Users\{USER}\My Documents\Visual Studio 2012\Projects\{PROJECT NAME} by default. You might want to store the Property Sheet somewhere more accessible.

 

 

 

Applying the Property Sheet to a Project

 

Create a New Project and make sure the Property Manager window is visable. From the Property Manager click Add Existing Property Sheet and navigate to the Property Sheet created earlier.

p6.png

 

 

You will now see the Property Sheet applied.

p7.png

 

 

Add a main.cpp with a call to a NAG C Library function i.e. a00aac. Compile and run the project with Local Windows Debug and you should see the implementations dialogs if successful.

p8.png