The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Buiding a C project with multiple files on Visual Studio 2010
Discuss Buiding a C project with multiple files on Visual Studio 2010 in the C Programming forum on Dev Shed. Buiding a C project with multiple files on Visual Studio 2010 C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 10th, 2013, 03:16 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 1
Time spent in forums: 8 m 58 sec
Reputation 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
|

January 10th, 2013, 03:44 AM
|
 |
Contributed User
|
|
|
|
|
It goes something like this path through the menus...
project->settings->add source files
Use this dialog to add File1.c, File2.c etc.
|

January 10th, 2013, 09:28 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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 .
|

January 10th, 2013, 11:44 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | 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.
|

January 10th, 2013, 01:53 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|