Visual Studio C Help

This is a discussion on Visual Studio C Help within the C Programming forums, part of the General Programming Boards category; Just installed Visual Studio and I'm at a roadblock trying to compile a C program. The help files are all ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    VS Help

    Just installed Visual Studio and I'm at a roadblock trying to compile a C program. The help files are all about C++, so I'm not sure how to continue.

    I have a hashTable.h and hashTable.c file, and then my main.c file.

    When I try to compile, it says "undeclared" blah blah because it's not seeing the .h/.c files. How do I properly set up my project?
    Last edited by Paul22000; 04-29-2008 at 05:39 PM.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Create the Project:
    File->New->Project
    Select "General" and "Empty Project".

    Change the Project Settings:
    In the Solution Explorer, right-click your new project and click Properties.
    Change the Configuration pull down to "All Configurations".
    Go to C/C++ -> General.
    Change to Warning Level 4, and Treat Warnings As Errors = Yes.
    Go to C/C++ -> Advanded.
    Change to "Compile As C".
    Click Apply & OK.

    Add existing files to the project:
    In the Solution Explorer, right-click on the Header Files or Source Files folder under your project and click Add->Existing Item.
    Choose your file and add it.

    Add new files to the project:
    In the Solution Explorer, right-click on the Header Files or Source Files folder under your project and click Add->New Item.
    Choose "Header File (.h)" or "C++ File (.cpp)", give it a name (with a .h or .c extension) & click OK.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by cpjust View Post
    Create the Project:
    File->New->Project
    Select "General" and "Empty Project".

    Change the Project Settings:
    In the Solution Explorer, right-click your new project and click Properties.
    Change the Configuration pull down to "All Configurations".
    Go to C/C++ -> General.
    Change to Warning Level 4, and Treat Warnings As Errors = Yes.
    Go to C/C++ -> Advanded.
    Change to "Compile As C".
    Click Apply & OK.

    Add existing files to the project:
    In the Solution Explorer, right-click on the Header Files or Source Files folder under your project and click Add->Existing Item.
    Choose your file and add it.

    Add new files to the project:
    In the Solution Explorer, right-click on the Header Files or Source Files folder under your project and click Add->New Item.
    Choose "Header File (.h)" or "C++ File (.cpp)", give it a name (with a .h or .c extension) & click OK.
    Sweet.

    Now I'm at the compile step.

    I can compile hashTable.c separately, by right clicking the file -> selecting compile. That's works ok.

    But I can't compile main.c (or build the whole project). It gives errors because main.c is not recognizing hashTable.h or hashTable.c. It's acting like they're not even there.

    Any ideas on what I'm doing wrong here?
    Last edited by Paul22000; 04-29-2008 at 05:53 PM.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Ahhh, this is ridiculous. I've been through every option in Visual Studio, why the heck doesn't this crap work.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Perhaps this is not correct?

    #include <stdio.h>
    #include <string.h>
    #include "hashTable.c"
    #include "hashTable.h"

    But putting it in <> gives an error that the hashTable.c file doesn't exist.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Post the exact errors you're getting, and the code (or relevant parts if it's big).

    One thing that might be part of the problem would be if you didn't install the Platform SDK.
    You'll need it eventually, so even if it's not the problem, you might as well install it...

    Oh, and one other thing that might be the cause - In the Project Properties, in C/C++ -> General, make sure you have the path to your .h files in the "Additional Include Directories" if they aren't in the same directory as the VC++ project.
    Last edited by cpjust; 04-29-2008 at 07:15 PM.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by cpjust View Post
    Post the exact errors you're getting, and the code (or relevant parts if it's big).

    One thing that might be part of the problem would be if you didn't install the Platform SDK.
    You'll need it eventually, so even if it's not the problem, you might as well install it...

    Oh, and one other thing that might be the cause - In the Project Properties, in C/C++ -> General, make sure you have the path to your .h files in the "Additional Include Directories" if they aren't in the same directory as the VC++ project.
    Sure. It's pretty big though. Let me see if I can comment out a lot of crap and still get the relevant error to appear.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Paul22000 View Post
    #include "hashTable.c"
    Don't #include .c files.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by robwhit View Post
    Don't #include .c files.
    Why not? Works fine in Unix?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It's not the way to do multi-file projects. Usually you compile each source file separately. That way, you can use a makefile or an IDE, so that you don't have to compile as much each time you recompile if you are fixing something in one file.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    hashTable.h:

    Code:
    #ifndef HASHTABLE_H
    #define HASHTABLE_H
    
    struct wordCount
    {
      char *word;
      int count;
    };
    
    typedef struct wordCount HT_Element;
    typedef struct wordCount *HT_Element_Ptr;
    
    
    // Paul22000: Removed A LOT of comments for each method below for simplicity:
    
    extern void HT_initialize(int n);
    extern void HT_cleanup();
    extern HT_Element_Ptr HT_lookup(char *s);
    extern HT_Element_Ptr HT_install (char *word);
    extern int HT_getNrOfSlots();
    extern int HT_getNrOfElements();
    extern HT_Element HT_getFirstElement();
    extern HT_Element HT_getNextElement();
    
    #endif
    -----

    main.c:
    (Literally commented everything out for this post, to show errors)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "hashTable.c"
    
    int main()
    {
      // commented everything out, still get errors
    
      return 0;
    }
    -----

    The Errors:

    Code:
    1>------ Build started: Project: Project2, Configuration: Debug Win32 ------
    1>Linking...
    1>countWords.obj : error LNK2005: _HT_initialize already defined in hashTable.obj
    1>countWords.obj : error LNK2005: _HT_lookup already defined in hashTable.obj
    1>countWords.obj : error LNK2005: _hash already defined in hashTable.obj
    1>C:\Documents and Settings\Paul\My Documents\Visual Studio 2008\Projects\countWords\Debug\Project2.exe : fatal error LNK1169: one or more multiply defined symbols found
    1>Build log was saved at "file://c:\Documents and Settings\Paul\My Documents\Visual Studio 2008\Projects\countWords\countWords\Debug\BuildLog.htm"
    1>Project2 - 4 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




    And here's hashTable.c, it doesn't really help, which is why I put it last in the post, so feel free to ignore this completely.

    (Ignore all the crap going on in the functions, I'm in the middle of implementing them but having quit a lot of trouble)

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #ifndef HASHTABLE_H
    #define HASHTABLE_H
    
    #define HASHSIZE 50000
    
    /* =================================
     * Type Declarations:
     */
    
    unsigned hash(char *s);
    
    struct wordCount
    {
      char *word;
      int count;
    };
    
    
    typedef struct wordCount HT_Element;
    typedef struct wordCount *HT_Element_Ptr;
    
    struct llistelem
    {
      HT_Element hte;
      struct llistelem *next;
    };
    
    typedef struct llistelem LL_Element;
    
    LL_Element *myhashtable;
    
    /* ==============================================================
     * Function Declarations:
     */
    
    void HT_initialize(int n)
    {
      myhashtable = (LL_Element *)calloc(n, sizeof(LL_Element));
    }
    
    extern void HT_cleanup();
    
    
    /////////////////////////////////////////////////////////////////////
    // Ignore all this crap, I'm in the middle of implementing these functions, and have no idea how to do it
    
    HT_Element_Ptr HT_lookup(char *s)
    {
      //np = &myhashtable[0];
      //printf("test1: &#37;s\n", np->hte.word);
    
      LL_Element *np = (LL_Element *)calloc(1, sizeof(LL_Element));
    
            int x = 0;
      for (np = &myhashtable[hash(s)]; np != NULL; np = np->next)
      {
                    printf("%s\n", np->hte.word);
                    x++;
      }
        //if (strcmp(s, np->hte.word) == 0)
        //return NULL;//&np; /* found */
      return NULL; /* not found */
    }
    
    extern HT_Element_Ptr HT_install (char *word);
    extern int HT_getNrOfSlots();
    extern int HT_getNrOfElements();
    extern HT_Element HT_getFirstElement();
    extern HT_Element HT_getNextElement();
    
    unsigned hash(char *s)
    {
      unsigned hashval;
      for (hashval = 0; *s != '\0'; s++)
        hashval = *s + 31 * hashval;
      return hashval % HASHSIZE;
    }
    
    #endif /* HASHTABLE_H */

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by robwhit View Post
    It's not the way to do multi-file projects. Usually you compile each source file separately. That way, you can use a makefile or an IDE, so that you don't have to compile as much each time you recompile if you are fixing something in one file.
    How do you do this? Compiling each separately etc

    Yeah, I was using a makefile when I was doing it in Putty/Unix.

    Anywho, I posted my code. Mind taking a look at the errors and seeing what I'm doing wrong?

    Funny, so far hours wasted at this, and I'm not even working on the project. Maybe it's just not worth it? Putty is a pain in the ass but at least it works

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Paul22000 View Post
    How do you do this? Compiling each separately etc
    If you added it to the VC++ Project and it shows up in the "Source Files" folder, it will be compiled into a .obj file when you click the build button, then all those .obj files are linked together into a .exe. There's not much you need to do other than make sure it's included in the project.

    Code:
    #include "hashTable.c"
    Remove this line and see if it compiles.
    Never #include source files - only header files.

    hashTables.c will get compiled into hashTables.obj and main.c will compile into main.obj. It's the linkers job to combine those two .obj files together, which is why you don't #include source files.
    Last edited by cpjust; 04-29-2008 at 10:46 PM.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by cpjust View Post
    If you added it to the VC++ Project and it shows up in the "Source Files" folder, it will be compiled into a .obj file when you click the build button, then all those .obj files are linked together into a .exe. There's not much you need to do other than make sure it's included in the project.

    Code:
    #include "hashTable.c"
    Remove this line and see if it compiles.
    Never #include source files - only header files.

    hashTables.c will get compiled into hashTables.obj and main.c will compile into main.obj. It's the linkers job to combine those two .obj files together, which is why you don't #include source files.
    Ok, I changed it from .c to .h

    (I still don't understand why this works fine in Putty/Unix but not in Visual Studio)

    Here's my main now:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "hashTable.h"
    
    int main()
    {
      LL_Element head;
    
      return 0;
    }
    And the errors changed, now it's undeclared identifiers again. This was happening before I commented out *everything* in my main.c, but for different variables.

    What does it mean? Why is this happening??

    Code:
    1>------ Build started: Project: Project2, Configuration: Debug Win32 ------
    1>Compiling...
    1>countWords.c
    1>c:\documents and settings\paul\my documents\visual studio 2008\projects\countwords\countwords\countwords.c(65) : error C2065: 'LL_Element' : undeclared identifier
    1>c:\documents and settings\paul\my documents\visual studio 2008\projects\countwords\countwords\countwords.c(65) : error C2146: syntax error : missing ';' before identifier 'head'
    1>c:\documents and settings\paul\my documents\visual studio 2008\projects\countwords\countwords\countwords.c(65) : error C2065: 'head' : undeclared identifier
    1>Build log was saved at "file://c:\Documents and Settings\Paul\My Documents\Visual Studio 2008\Projects\countWords\countWords\Debug\BuildLog.htm"
    1>Project2 - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by Paul22000; 04-29-2008 at 10:59 PM.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Did you #include "hashtable.h" in countwords.c?
    Function prototypes are automatically extern. You don't need to put the extern there.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21