#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Buiding a C project with multiple files on Visual Studio 2010


    Hi there,
    Does anyone know how to compile a C project with multiple .c, .h files(e.g. File1.c, File1.h, File2.c, File2.h...etc) using Visual Studio 2010

    the project has main.c file that includes all the .h files that compiles but fails to link as it needs to include the .c files to link....is there any better way to include just the .h files just like C++ style?

    Thanks in advance
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    It goes something like this path through the menus...
    project->settings->add source files

    Use this dialog to add File1.c, File2.c etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    More specifically, in 2008 at least, it's Add Existing Items. You could use Add New Items to create the source file, but then you'd have to go back and rename them to be .c instead of the default .cpp .
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by baher_nicola
    is there any better way to include just the .h files just like C++ style?
    What are you talking about!? C++ and C are no different in this respect, you need to separately compile and link all translation units, and in Visual Studio, this is simply a case of adding the sources to the project.

    I suspect that you might be referring to C++'s ability to allow the placement of inline code in the header files, however if you build large projects that way you will encounter significant code bloat and extended build times - that is not what in-lining is intended for.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by baher_nicola
    the project has main.c file that includes all the .h files that compiles but fails to link as it needs to include the .c files to link....is there any better way to include just the .h files just like C++ style?
    If you're talking about placing all the code in the header files and including them, then forget that idea right now! Some beginning books and teachers may teach it, but it is a horrible idea that will only cause you far too much trouble further down the line. It is a quick-and-dirty trick that proves woefully inadequate. It's far better to do it the right way.

    In a development environment that provides project management, such as Visual Studio 2010 does, the right way is to add the source files to the project, as we have all told you.

    What may be raising your question is how the header files were included even though they also had not yet been added to the project. That is because main.c had included them. In reality, you do not need to add header files to the project; once you have added the source files to the project then the project will build just fine without your having also added the header files. In order to build the project, the project needs to be aware of the source files so that it will know to compile them and to link in their object files, the project does not need to be aware of the header files because that's the job of the source files.

    However, in practice it is very advisable that you do also add the header files to the project. By making the project aware of the header files, you have ready access to them for editting. Plus, IntelliSense can scan them and enable you to use the pop-up speed menu to jump to an identifier's declaration or references. And at the very least, when you do a Find in Files search then the project will know to also search through the header files.

    Bottom line: add both source and headers files to the Project.

IMN logo majestic logo threadwatch logo seochat tools logo