Cg is cool, but how do you set it up?
If you’re getting errors like
fatal error C1083: Cannot open include file: ‘Cg/cg.h’: No such file or directory
error LNK2019: unresolved external symbol __imp__cgGLLoadProgram referenced in function _main myCgProgram.obj
you probably don’t have your Visual Studio environment variables set up properly.
Below is a quick explanation of why the error occurs, then simple steps to follow to fix it.
WHY does fatal error C1083: Cannot open include file: ‘Cg/cg.h’: No such file or directory happen, even though I’ve installed the Cg Toolkit?
- COMPILER ERROR: (“FATAL ERROR”) Cannot include ‘Cg/cg.h’. Happens because Visual Studio CAN’T FIND “Cg.h” because you didn’t tell Visual Studio WHERE to find it. I explain how to tell Visual Studio where to find “Cg.h” later on, in the “DO THIS” section near the bottom of this page.
WHY do I get a linker error?
- LINKER ERROR: The list of about 50,000 errors that come up saying something about “unresolved external symbol”. This happens because Visual Studio doesn’t know where to find the Cg library files (cg.lib and cgGL.lib)
Now if you’re impatient for the answers to fix this ASAP, just skip down to the “DO THIS” section.
But why do the NVIDIA examples work while the new projects I create don’t?
We’re going to first examine what Nvidia did to get theirs to work, then we’re going to show how to make yours work easily.
Open one of the projects that generous Nvidia gives you when you install the Cg toolkit (in the “Cg Toolkit” -> “Explore Cg Toolkit” directory from your start menu)
Once you have that open, do this:
You get this dialog:
Now click “C/C++” -> General:
NOTICE under “Additional Include Directories” they’ve ADDED
$(CG_INC_PATH);c:\Program Files\NVIDIA Corporation\Cg\include
Ok? NOTICED that? THAT is how NVIDIA is telling Visual Studio where to find “Cg.h” and that will get rid of the FATAL ERROR. However, there’s an easier way that I’ll explain in the “DO THIS” section below.
NEXT, click “Linker->General”.
NOTICE there that they have ADDED under “Additional Library Directories”
$(CG_LIB_PATH);c:\Program Files\NVIDIA Corporation\Cg\lib
THAT is how NVIDIA tells Cg how to find the library files, which is the first step to fixing the linker errors.
FINALLY, let’s NOTICE one last thing. Under “Linker->Input”:
NOTICE under “Additional Dependencies” they’ve added:
So how to fix?
Well you could go and for every single one of your Visual Studio projects that use Cg, make the same changes that I’ve just shown you pictorally above.
But that’s painstaking and annoying to do for every project.
There’s a way to do this that’s better.
To make that part of your default settings in Visual Studio, you have to do the following. Open the Tools->Options menu in Visual Studio:
You get window:
On the left hand side of the window, choose “Projects and Solutions”, then choose “VC++ Directories”
Now do this:
Next, do this:
FINALLY, YOU MUST ADD the following 2 lines of code
#pragma comment( lib, "cg.lib" ) #pragma comment( lib, "cgGL.lib" )
ANYWHERE in your C program (I usually put them just after #include <Cg/cg.h>).
Those 2 #pragmas work to do the same thing as the
addition under “additional dependencies”.
What about using the CGC compiler?
How do you use the cgc compiler?
Its actually easy.
- Set up cgc environment variable. To do this, see “Adding the Cg Compiler Environment Variable” here
Open up the command line in windows by pressing WINDOWS_KEY+R to get the RUN dialog, then type in “cmd”.
You get this black window.
Then, try this.
To compile a cg program in general using cgc that has filename vertexShader.cg and entry function vertexShaderFunction():
cgc vertexShader.cg -entry vertexShaderFunction -o vertexShader.vp
vertexShader.cg – name of vertex shader program to compile
-entry vertexShaderFunction – tells cg the name of the vertex shader function. if you don’t specify the entry function name, then cg looks for an entry function named main(). If there is no main() function in vertexShader.cg and you don’t specify the entry function, then the compilation fails.
-o vertexShader.vp – the output file. If you open this after compilation, you’ll see all the assembly language that will be sent to the graphics card. Aren’t you glad you don’t have to write that!