Skip navigation

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

Or

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:

    Project Property pages

    You get this dialog:

    Dialog

    Now click “C/C++” -> General:

    Additional include directory

    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”.

    Additional library for linker

    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”:

    Additional dependencies

    NOTICE under “Additional Dependencies” they’ve added:

    cg.lib cgGL.lib

    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.

    Do this

    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:

    Options menu in Visual Studio

    You get window:

    Options dialog in Visual Studio 2005

    On the left hand side of the window, choose “Projects and Solutions”, then choose “VC++ Directories”

    Now do this:

    Add include dirs to env variables

    Next, do this:

    Add lib dirs

    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

    cg.lib cgGL.lib

    addition under “additional dependencies”.

    What about using the CGC compiler?

    How do you use the cgc compiler?

    Its actually easy.

    1. Set up cgc environment variable. To do this, see “Adding the Cg Compiler Environment Variable” here
    2. 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.

      cgc -help
      

      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!

    3. and this

      Advertisements

2 Comments

    • Per
    • Posted June 15, 2009 at 12:55 am
    • Permalink

    I get this exact problem when I compile the NVIDIA OpenGL SDK 10 example code in Visual Studio 2008. It turns out that the new format for .sln files that are used in VS2008 are mis-translated from the VS2005-format that NVIDIA provides. The “Additional Library Directory” build property that should include “$(CG_LIB_PATH)” get a couple of extra &quot’s that messes things up. Just remove them if you have this problem.

    • dcxz
    • Posted May 19, 2010 at 12:32 pm
    • Permalink

    How to do this in Visual Studio 2008? There is no Options->”Projects and Solutions” there.


One Trackback/Pingback

  1. […] code package available on esnips (thanks esnips!) Possibly related posts: (automatically generated)Setting up Cg environment variables in Visual Studio 2005Radial Vertex Displacement MappingVertex Noise GLSL Sourcecode « “element is […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: