Thread: Config MVS to complie C

  1. #1
    Registered User ph071's Avatar
    Join Date
    Dec 2008
    Posts
    5

    Config MVS to complie C

    Hi

    New to programing

    just need some help with

    "How to set up Microsoft Visual Studio to compile C source code" because i know it is possible but i don't know how because the is no option to to select C templates only "Visual C++"

    please thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just name the file as yourname.c instead of yourname.cpp, and it will compile as C instead of C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Or you can keep the cpp extension and change some of the project properties. Depending on what version you have it may be different, but in Visual C++ 2005/2008 I think you can right-click on the project in the "Solution Explorer" and go to "Properties" then in the window that pops-up go to Configuration Properties->C/C++->Advanced. Once there in the section of the window on the right there should be an option that states something along the lines of "Compile As". You can highlight that and then select from a drop down option change that from "Compile as C++" to "Compile as C".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hk_mp5kpdw View Post
    Or you can keep the cpp extension and change some of the project properties. Depending on what version you have it may be different, but in Visual C++ 2005/2008 I think you can right-click on the project in the "Solution Explorer" and go to "Properties" then in the window that pops-up go to Configuration Properties->C/C++->Advanced. Once there in the section of the window on the right there should be an option that states something along the lines of "Compile As". You can highlight that and then select from a drop down option change that from "Compile as C++" to "Compile as C".
    Whilst that technically works, it is "the wrong solution" in the sense that it will not work if the files are then transferred to another system where another compiler is used - gcc for example also uses the extension to differentiate between C and C++ files.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User ph071's Avatar
    Join Date
    Dec 2008
    Posts
    5
    Thanks guys

    just one more question

    "About about the additional header files that are used by C can you still use the old fashion way of putting them in < , > or " ," like the header "stdafx.h" i know in this header there are some other headers common to C like <stdio.h> and others, So do i have to add the other addition headers for my program in that header file("stdafx.h") ."

    or is it ok just to add the header files by them self like this.

    eg
    #include "stdafx.h"
    #include <????.h> //---- ???? any name of a header file

    int main (********)
    {

    //-------- body of the program

    return 0;
    }

    this to say that the ext is ".cpp"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course you can add them either in the stdafx.h file or in your .c files.
    Usually, it is an advantage to add them into the stdafx.h file since it's a precompiled header which will speed up compilation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    stdafx.h is used as a "precompiled header". My suggestion is that you go to your project properties and turn "precompiled header" off, and ignore precompiled headers - at least until such a point that your project is dozens of source files that all include at least half a dozen files in common.

    Otherwise, visual studio's C compiler uses headers in the same way as other compilers. <somethign.h> in an include means system directories are searched only, whilst "something.h" means that the file is searched for in the same directory as the source code is in first, then in the system include directories.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    stdafx.h is used as a "precompiled header". My suggestion is that you go to your project properties and turn "precompiled header" off, and ignore precompiled headers - at least until such a point that your project is dozens of source files that all include at least half a dozen files in common.
    My advice is the contrary, because you will gain nothing from doing so and potentially also add compilation time.
    When using precompiled headers, you only have to add #include "stdafx.h" at the beginning of every sourcefile. No more.
    If you don't, you will get a compile error--that's quite friendly, btw--telling you that you forgot to do so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    My advice is the contrary, because you will gain nothing from doing so and potentially also add compilation time.
    As a beginner, you may gain from not using precompiled headers: it would be a reminder to associate functions with the headers that they are declared in.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Learn to remember to include headers and potentially learn what headers what is stored inside, you mean?
    Hmmmm. That is the first good argument against using precompiled headers I've heard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Learn to remember to include headers and potentially learn what headers what is stored inside, you mean?
    Hmmmm. That is the first good argument against using precompiled headers I've heard.
    You mean the fact that forgetting to put #include "stdafx.h" gives strange error messages is not bad enough? And then the user goes to school where they use Unix/Linux, and wonder why including stdafx.h doesn't work - or why the code (s)he wrote at school and compiled fine there won't work at home, etc.

    Yes, it's a good argument that you understand the relationship between header files and functions, but the complications of using precompiled headers are also quite apparent.

    And unless we're talking fairly large projects (or perhaps projects that include Windows.h or some such), there is no real benefit. Take one of your small projects (no more than 5 source files), do a complete build of it and time the time it takes to build with and without precompiled headers. How much is the difference? Two seconds or less is my guess, and a total time of mayeb 10 seconds.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    You mean the fact that forgetting to put #include "stdafx.h" gives strange error messages is not bad enough?
    Not bad at all.

    And then the user goes to school where they use Unix/Linux, and wonder why including stdafx.h doesn't work - or why the code (s)he wrote at school and compiled fine there won't work at home, etc.
    That's basic course - to separate local files in your project and library headers.
    There should be no confusion or the user seriously needs to re-learn the language.

    Yes, it's a good argument that you understand the relationship between header files and functions, but the complications of using precompiled headers are also quite apparent.
    No, I fail to see them.

    And unless we're talking fairly large projects (or perhaps projects that include Windows.h or some such), there is no real benefit. Take one of your small projects (no more than 5 source files), do a complete build of it and time the time it takes to build with and without precompiled headers. How much is the difference? Two seconds or less is my guess, and a total time of mayeb 10 seconds.
    I would not want to wait 10 seconds to compile my project. Absolutely not.
    2 seconds is acceptable, but I'd rather be without.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by Elysia View Post
    Learn to remember to include headers and potentially learn what headers what is stored inside, you mean?
    Hmmmm. That is the first good argument against using precompiled headers I've heard.
    Another excellent argument is that they're compiler specific and can cause confusion.
    Using Borland C++ you don't have any stdafx.h for example, and having it as an #include will cause trouble.
    gcc is similar.
    MSVC requires it to be included as the first thing in every single source, even if that source doesn't need any #includes at all.

    Strangely, if you create a project to not use precompiled headers in MSVC 2008, it still creates stdafx.h and stdafx.cpp, it just doesn't complain if you delete them (and all references to them) from the project.

  14. #14
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by Elysia View Post
    I would not want to wait 10 seconds to compile my project. Absolutely not.
    2 seconds is acceptable, but I'd rather be without.
    You've lived a sheltered life, and probably a short one.
    I've dealt with compile cycles that took literally days to perform a full build, and that on hardware that was above standard for the time.

    And for large projects today you can expect a compile cycle to take minutes to hours, it keeps you on your toes to make darn sure your code is correct before you start compiling

    For such projects, saving a few seconds or even minutes by using a compiler-specific precompiled header that otherwise causes more trouble than it's worth is just not worth it.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jwenting View Post
    Another excellent argument is that they're compiler specific and can cause confusion.
    Using Borland C++ you don't have any stdafx.h for example, and having it as an #include will cause trouble.
    gcc is similar.
    MSVC requires it to be included as the first thing in every single source, even if that source doesn't need any #includes at all.

    Strangely, if you create a project to not use precompiled headers in MSVC 2008, it still creates stdafx.h and stdafx.cpp, it just doesn't complain if you delete them (and all references to them) from the project.
    That old argument is just as untrue as it was then.
    stdafx.h is not a special header - it is a header created for the project, and thus is a normal header that you can include and which works on every compiler.
    And I cannot see confusion stemming from that, since it is a header tied to the project - a project header if you will, not a library header.

    Quote Originally Posted by jwenting View Post
    You've lived a sheltered life, and probably a short one.
    I've dealt with compile cycles that took literally days to perform a full build, and that on hardware that was above standard for the time.

    And for large projects today you can expect a compile cycle to take minutes to hours, it keeps you on your toes to make darn sure your code is correct before you start compiling

    For such projects, saving a few seconds or even minutes by using a compiler-specific precompiled header that otherwise causes more trouble than it's worth is just not worth it.
    Perhaps. I've had projects that might take a minute to build, but nothing really big. I am, after all, not a hired software developer. All my projects are private.
    But nevertheless, where there is time to be saved, I feel like I want it. Especially if compiling it done a lot.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using app config to create a dynamic menu
    By dana_cc1 in forum C# Programming
    Replies: 2
    Last Post: 04-29-2008, 11:29 PM
  2. config file interface
    By valis in forum C Programming
    Replies: 3
    Last Post: 08-17-2006, 09:30 PM
  3. Parsing config file in C++
    By Dynamo in forum C++ Programming
    Replies: 12
    Last Post: 09-01-2005, 11:21 AM
  4. knoppix config file
    By sentienttoaster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-17-2004, 09:23 PM
  5. command: borland v. mvs
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2002, 06:04 PM