Microsoft Visual C++ Express - A Free Compiler/Debugger for DLLs to Run in GoldSim
How do I get started with a free C/C++ compiler for GoldSim DLLs and how do I debug GoldSim DLLs?
Microsoft Visual C++ Express is free C/C++ compiler that can be used to create DLL’s for GoldSim. This article will discuss installing Microsoft Visual C++ Express and using it as a debugger when developing DLLs for execution in GoldSim.
Installing Visual Studio Express for C++
The free version of Visual Studio, Visual Studio Express for C++, is available at this link:
Registration is required within 30 days but the product is free:
After you install, go to Start: Windows Update to detect any new service packs that may be available.
Getting Started – A New GoldSim DLL project in Visual Studio Express for C++
From the start page in Visual Studio Express for C++, select New Project:
In the dialog, select Win32 Console Application and give the project a name:
From the resulting dialog, click Next to view more options:
From the Next dialog, select DLL and empty project, and then click Finish:
Now you are ready to start adding code and header files. If you plan on sharing your project with someone else, you may want to move your source code files to the directory that Visual Studio Express has created for your new project. That way the file references will be relative rather than absolute. If you do this, you can share your project with someone else by copying the source code and header files to a folder along with the “project.vcxproj” file created by Visual Studio Express, where project is the name of your project.
Debugging Your GoldSim DLLs with Visual Studio Express
When writing a GoldSim DLL, it is convenient to use GoldSim as the debugger. The rest of this article describes how to set breakpoints for code execution in a DLL and have the execution pause for debugging during a GoldSim model run. (This tutorial assumes you are starting with a DLL project that is already compiling correctly).
You can start by inserting a breakpoint where you would like code execution to be paused. In the figure below, I am setting a breakpoint right after I have assigned a matrix with values from GoldSim. I am doing this to check whether the matrix Snow.adc was assigned correctly. Note that when you first insert a breakpoint, it will be hollow with a small warning sign. This is because it is not currently attached to a debugger.
Next, go to the solution explorer and right click on your solution name to get its properties, as shown below.
In the properties dialog, navigate to configuration properties and then to Debugging. The important thing to change is the Command associated with the debugging. You will want this field to be the path to the GoldSim.exe executable that will be running the DLL, as shown below
Next make sure you are currently set to compile in Debug mode and not Release mode. That can be changed on the main menu at the top (if you are not currently in the middle of debugging), as shown below. Press F7 to compile the debug version if you haven’t done so already.
Press F5 to start debugging. You will probably see the warning message below, but this can be ignored. This occurs because GoldSim at this point has not yet referenced the DLL, so Visual Studio Express finds no debugging information about GoldSim. It is okay to click yes and continue, as the problem will be rectified when GoldSim calls the DLL.
At this point, Visual Studio Express will open GoldSim, perhaps after some lag time. Note everything will be a little slow to respond since it is going through Visual Studio Express. Now you are ready to start debugging with GoldSim. Open your GoldSim model that calls the DLL of interest, and make sure the external DLL element in the GoldSim model is referencing the DLL you have created in your solution’s debug folder. Note you cannot simply copy the debug DLL to another location and expect the debugger to somehow work.
In GoldSim, press F5 to get the run controller. You will probably want to just simulate one timestep using the run controller’s Simulate One Timestep button. When you do this, if you have set a breakpoint somewhere in the DLL function’s section that is called when XFmethod is equal to XF_CALCULATION, the DLL will run up to the breakpoint, and then you will notice control switch from GoldSim back to Visual Studio Express.
The figure below shows the Visual Studio Express window after execution has reached the breakpoint. At this point you can check the values of variables that have been calculated. In the screenshot I am checking the values of a matrix that is part of a structural variable.
At this point you can right click to see a few more debugging options, such as running to cursor (Hotkey=Ctrl+F10) or adding a variable watch. If you are done, in Visual Studio press Shift-F5 to exit debugging. GoldSim will close at this point.
In some cases you may want to do this kind of debugging in your DLL somewhere in the middle of a GoldSim run after many timesteps. In these cases, you can disable your breakpoints while you advance the GoldSim run up to your timestep of interest. With GoldSim paused in this state, go to your solution in Visual Studio and add your breakpoints. Then use the run controller to simulate one time step.