I am developing Windows code in C using Visual Studio 2010.
I am working on several related projects: a static library, a DLL, and an executable. The DLL should have the static library linked in, and the executable depends on the DLL. (I am using "implicit" DLL linking; I do not need to control the DLL loading and unloading.)
Right now I have three separate VS2010 projects, but I know it is possible to set things up as "subprojects": the static library should be a subproject of the DLL, and the DLL should be a subproject of the EXE.
I want to solve the following:
When I rebuild the EXE, it should rebuild the DLL if need be, and in turn the DLL build should rebuild the static library if need be. I'd like to just have the EXE open, hit the F7
key, and everything builds.
When I rebuild the EXE as Debug, it should link with the Debug .lib
file from the DLL; likewise when I rebuild the EXE as Release, it should link with the Release .lib
file from the DLL. If I set up the subprojects correctly, will this Just Work automatically?
EDIT: I have made progress. First, in the DLL project, I used File / Add
and then chose Add Existing Project
to add the static library. (This is only available on the File
menu as far as I can tell! It definitely is not in the right-click menu.) Second, I clicked on the DLL project in the "Solution Explorer" sidebar on the left, then right-clicked and chose Project Dependencies...
and set that the DLL project depends on the static library project, which should build first. Visual Studio seems to have then magically set up the link dependency for me, and when I hit the F7
key in the DLL project, it will build the static library project.
I tried repeating the above steps for the EXE project. Now it will build the DLL, but it isn't linking things correctly yet; the linker is complaining that the functions exported from the DLL are not available.
I have it working. Here are the steps I used to make it work:
Choose File / Add / Existing Project...
and navigate to the other project that you want as a subproject. Choose the .vcxproj
file and okay the dialog. You have just added a project as a subproject.
Right-click on the main project in the "Solution Explorer" sidebar pane on the left, and choose Project Dependencies...
Click on the subproject to make it a dependency of the main project, then okay the dialog. This is a tabbed dialog, and the other tab is "Build Order"; if you click on that you can make sure that Visual Studio will build the subproject first and then build the main project. You have now set things up so that the subproject builds first, then the main project.
Right-click on the main project in the "Solution Explorer" sidebar pane on the left, and choose References...
In the references dialog, click on the Add New Reference...
button. Make the main project reference the sub-project and okay it. You have now set things so that the linker will link the output file of the sub-project.
I performed the above steps to make the DLL project have the static library project as a subproject, and I again performed these steps to make the EXE project have the DLL project as a subproject. But I got build errors about the static library file not being found. So I think that, even though I set the static library as a subproject of the DLL library, it is essential to set up that dependency within the EXE project; the dependencies don't seem to be transitive! I performed the above steps to make it clear that the static library is a subproject of the DLL subproject of the EXE project, and that fixed things.
Now when I hit the F7
key, everything builds. If I choose Build / Clean Solution
it cleans all three projects. When it links, it gets the Debug or Release library files as appropriate and links them in.
Sometimes when I hit F7
and build, the build fails, saying that the dependent library is outdated; but if I just hit F7
again then Visual Studio rebuilds the dependent library and the build succeeds. If I hit Ctrl+Alt+F7
to force a clean build, the build succeeds every time. I don't know why Visual Studio has this quirk.