Thread: linkage

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    linkage

    My question is how files are linked in C++, specifically in MS Visual Studio 2008 since that's what I'm using (but would be very interested to know if there are significant differences between compilers in this regard).

    When you generate a new solution in MSVS you also get 3 folders delivered: Header Files, Resource Files (which I haven't used yet but would be also interested to know what it's used for), and Source Files. One part of the intention is pretty obvious: Put your .h files in header files and your .cpp files in source files.

    Just doing that and writing correct code will compile and run as console application IF, when you created the project in the brief wizard, you clicked "create empty project." However, if you get in a rush (as I just did, hence reminding me of this question, which I've had for a while) and just click "finish" before specifying that you want an empty project, then MS creates several generic files for you (can't remember exactly what since I deleted them all and have no desire to repeat this mistake).

    I kind of think these files are for a visual C++ project, but that's really peripheral to my main question: After deleting these files from the solution folder and just creating some new ones initially that I knew would work (I just copied some working files that I was intending to change), Visual Studio refused to compile the program and explicitly asked for the files that I had just deleted.

    At the same time, on the surface, everything looked just as it had in the functioning version (except that the name of the solution folder was different). So, what I'm wondering is what the compiler, or specifically the one used in Visual Studio, uses for linking and compiling the files that goes above and beyond simply their location in the appropriate folders. So, basically, the things I would need to change if I clicked the wrong button and got some precompiled files I didn't want.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    By default, it uses something called precompiled headers, which speeds up compiling of headers.
    For this to work, you need a header file which it will precompile (usually called stdafx.h) and a source file that includes this (usually called stdafx.cpp) and you must also include this header in all your source files.
    In case you do not want this, then you should go into settings, recompiled headers and select none. This will eliminate the need of these files.
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    242
    Well, it sounds like you like it with the reasoning that it speeds up compiling of other headers.

    And I feel like I'm also reaching the point where it's probably beneficial to start exploring how this kind of thing works.

    Is my understanding correct that in order to use this feature, you need to put #include "stdafx.h" in every .cpp file in your solution but ONLY those (i.e., other header files don't need to #include it)?

    Also, is there a reasonably simple explanation for why it doesn't compile at all if you just delete those files (and obviously don't #include them) and start again seemingly from scratch but in the same solution folder that VS created? It seems like there have to be some kind of properties somewhere that you can edit if you decide to change the way you're setting it up.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You only need to make sure you include the header in every source file. You may include any other include files you want, as well. The only requirement is that you include the precompiled header file.
    Also, like I explained, if you create a solution with the default settings and want to start from scratch, you need to disable the precompiled header option. After that, it will work.
    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. Linkage question
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2009, 07:56 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM