MFC is failing to launch my dialog boxes, it seems, because it can't find the resource identifiers. The dialog boxes are in a separate .lib file (so it has a separate .rc file, which, I'm assuming, somehow conflicts with the one in my .exe file). How should I be handling this situation?
In the .rc file for the .exe file, add a line like this:
#include "YourLibResourceFile.rc"
Then, in the .exe's project settings, add an additional include directory to where YourLibResourceFile.rc is, in Resources/Additional Include Directories.
You can't store resources (.rc files contents) in a static library. And since you can have only one "main" .rc file, all other .rc files mst be included in that one using an #include statement, such as explained by Smashery (Edit: Oh! Smashery, you are the OP!).
Make sure all your resource IDs are unique.
Related
I want to include a specific header file (MyHeader.h) in a C++ project. The solution for my project is located in the folder:
C:\\Projects\\MyProgram
The header file is located in the folder:
C:\\Projects\\MyProgram\\Files
I tried the following line of code, but it doesn't work.
#include <Files\MyHeader.h>
Is there an easy way to include the header file without adding the full path to "Include directories" in the configuration properties?
Thanks in advance for any help. :)
Try this
#include "files/myheader.h"
It will work if the header is in a files folder in the same directory as the current source.
If you're trying to include a 3rd party library and not your own header, I'd suggest you to save the library headers in a particular path (say C:\Library\headers). (If there are static libraries put them in some other path like C:\Library\lib).
In your Visual Studio C++ Project, go to View > Other Windows > Property Manager.
Double Click on the Project Name. You will see a dialog box like this:
Make sure All Configurations is chosen in the dropdown, if you want the change to be applied to both the Debug and the Release Configurations. Else just choose the Configuration you want the properties to be applied to.
Go to VC++ Directories on the left and choose Include Directories in the right, and enter the path(s) in the textbox separated by a ;.
You can also use the drop down and use the Dialog box to add the paths if you'd prefer to browse to each path separately
Add the library path the same way to Library Directories
Save the changes using the Save button on the Property Manager Pane's toolbox.
You will then be able to access the header file contained in the directory you added by something like:
#include <myheader.h>
This approach will help, because it won't matter where the headers saved. The header path is not hard-coded.
The current directory of the source file is always searched, although if you use angled brackets it is searched after your include path, whilst if you use quotes it will be the first directory searched.
The directory of your solution or makefile/project file is irrelevant, the local path is relative to the compilation unit, i.e. the cpp file.
If that cpp file includes a header, that headers own includes are relative to itself, not the cpp file that included it. (It would be hell to manage if it were not).
Ideally you should use forward slashes in paths too.
Your actual correct setup here is to include the solution directory in your search path. If it is Visual Studio you can use a macro for this, $(SolutionDir) I think.
That means that if anyone else is going to build your solution, they can put it in a directory they choose and as long as the structure underneath is the same, it will still work.
To use a relative path in your cpp file without any include directory settings, you might need something like:
#include "../Files/MyHeader.h"
You just need to replace your brackets <> with double quotes "" like this:
#include "Files\MyHeader.h"
Brackets is used when you want Visual Studio to find the path from your project settings and double quotes when you want to access the header from a specific path or relative to your project.
Originally my MFC program had one .rc file that includes resources for two languages. But, now I have two .rc files in two separate resource-only dll projects(in the same solution), and use LoadLibrary() and AfxSetResourceHandle() to select language dynamically(I referenced this article).
It seems working well, but one problem is that I cannot use dialog editor properly for dialogs in resource-only dll projects. It shows dialogs normally but I cannot add event handler by double-clicking a control or by using property menu. Is there any way to connect code and resource files which are in separate projects in the same solution?
EDIT:
I changed the approach slightly. Instead of creating new RC files in separate resource projects, I created them in the main project and excluded them from the build. And I made them to be referenced by separate resource projects.
One important point was that I added #include "resource.h" into Read-only symbol directives of each RC file(You can do this by right-clicking the RC file in Resource View and choosing Resource Includes item). "resource.h" here is the one that is related to the main project's original RC file which has resources for multiple languages. I don't know why I should add it, but it seems necessary for satellite dll to be working well.
One problem remaining is how to maintain that multiple RC files. For adding resources, if I should maintain original RC file and its resource.h file, I might have to add resources first into the original RC file and then copy them into every language specific RC file. Is this right method, or can I go on without the original RC file?
Include the RC file for this resource only DLL into the project too. But exclude it from the build.
If you have two resource files. Use resource file in a different way:
Create a stand alone program with a normal MFC resource file...
Use language tags for all dialog resources internally
Than use this resource in a second project to build the satellite DLL
Set the resource compiler defines so that the resources tagged with the specfic language are not included.
I am having a problem of getting compile errors (red underlines) like:
Error: cannot open source file "stdafx.h"
Here an edited screenshot of the environment:
On the LEFT is my Visual Studio Solution Directory list with the "Show All Files" off.
I am working on a school project, and each Folder are the source files of different parts of the project with different people who are in-charge of them.
For example, Student A and B are incharge of AST and PARSER folders (we will call them sub-projects).
We have an API for each sub-project so other sub-projects know what to call.
At the TOP-CENTER, we have my Source File for a class QueryProcessor. (just the first few lines)
Below it, is the Output for the Build Success.
The red lines are all over all the classes, mainly cause the #include "stdafx.h" cannot be opened by the environment.
On the RIGHT, that is the stdafx.h where we include all the different sub-projects so we save the trouble of each project having a different stdafx.h
However, I am able to build the project. I am pretty sure I am doing this directory/linking wrongly.
This should work
Right click on the solution file
Click Open in Windows Explorer
Find file stdfx.h in explorer and copy the path of the folder
In visual studio solution explorer, Right click on the project file
Click properties-> C/C++ -> General
In the Additional Include Directories paste the path
Combining folders and virtual folders in VC is from my point of view messy because the virtual folders indicate that all files are in one directory and the folders created on the harddrive obviously indicate that all files are in different directories. You can combine it if you know what's going on but in your case I would not recommend it.
I assume you missunderstand the purpose of stdafx.h The purpose of this header file is NOT to put all header filles into it and then just include it to all other files. Here is a SO question about this Purpose of stdafx.h
After cleaning up your stdafx.h file include as many header files into your .cpp files and only put these includes in your header files if they are required in the header file
Turn on show all files, now you will work with actual folders and you can be sure that if you adress a folder like "PKB" that this folder really exists since you can see it in the left solution explorer.
If you use using namespace std; for example make sure you also include the required header files. You might think "hey I already included e.g. iostream in another header file which I now include in this header file so I don't need it" That will really destroy you when you work with bigger projects.
Oh and regarding the stdafx.h include problem as soon as you switch to show all files I assume you will realise that stdafx is in a different file than the file where you use the include. Maybe something like #include "..\stdafx.h" is required (depending on your structure).
I think it's obivious but if you include a header file the include is allway relative to the file which is including the other header file.
stdafx.h is commonly used for creating a precompiled-header, which essentially is a compile-time optimisation such that the compiler will not continually compile these headers for every compilation unit.
If any of these headers changes, you will need to do a full system rebuild.
In reality it is preferable only to use it to include standard headers plus third-party headers (like boost libraries and similar) that you are not ever going to change.
You may decide that some of your own libraries are "set in stone" and can also be included.
Every project, i.e. every part of the project that is built into a separate unit (DLL or .exe) should have its own precompiled header and its own version of stdafx.h
Projects should only ever include their own .stdafx and not those of other projects, therefore this header file can also be used to define your dllexport macro.
When arranging your project headers you should be aware of:
1. Which headers are included externally
2. Which headers are only included internally, and are not even included indirectly externally.
The latter sort should include your stdafx.h file and should ideally not be in the same directory as those headers included from outside your project.
I have the following question:
I was given the task - to build an application. There was a ready file counter.h and some other file - counter.obj. It turned out that in the counter.h there were only declarations of the functions - how can I include .obj file into the .cpp file so that it compiles? I am using Microsoft Visual Studio 2010 - and in which folder should the file itself go?
Add the obj-file to the Solution just as you would do with cpp-files (i usually do this by drag-and-drop, that is, drag the file from the Windows Explorer and drop it on a project in the Solution Exporer window).
You can put the obj-file together with cpp-files; it doesn't really matter.
You do cannot include object file in to a cpp file.
The compiler compiles the cpp file and generates the obj files, for each cpp file, these files are further linked together to create an libray or an executable.
Usually, you would link libraries(.lib or .dll) to an Application, Check if those are with you.
If not,
You can try linking the object file to your application by:
Go to project properties then from "Property Page" select the node "C/C++" their you will get "Additional Include Directories" add the name of your object file.Keep your obj file in the directory where your source code is or you can add the directory from:
Tools->Options->Projects and Solutions->VC++Directories.
I have never tried the second method except for academic projects,which was years ago, So not sure about it, Please check information on MSDN.
I am using #define and #ifndef to strip down the exe size, i noticed the resource.h and icon.ico files eat a lot of space, so i would like to not include those in my exe at all.
How do i add rule for this that obeys my #define commands ? I could edit the resource.h, but every time i change it, it would get overwritten by Visual Studio.
Edit: i dont know what is the correct name for these resource things, but my "resource.h" includes the window menu option stuff etc.
How much is "a lot of space" that you speak of?
resource.h is used for #define-ing constants that identify resources, e.g. they're just numbers. They shouldn't be a factor in executable size.
What is a factor in executable size is the resources that you embed in the executable, specified by a *.rc file. icon.ico takes up space in the *.exe because the compiler embeds the binary of the icon into the executable file itself. This icon is specified in an *.rc file that should be somewhere in your project.
You can choose to remove the icon from the *.rc file and store it separately from the *.exe file, but it's easier to just embed it into the executable. The information for defining menus, icons, dialogs, etc. has to be stored somewhere, after all.
Edit: You can have multiple resource files, so Visual Studio doesn't overwrite your directives. Refer to http://msdn.microsoft.com/en-us/library/6t3612sk(v=VS.80).aspx to see how Visual Studio handles multiple resource files. The section called "Using Multiple Resource Files in the Same Project" seems to be relevant to your problem.
In VS2010, properties for icon resources include an item "Condition" which is described as "Specifies the preprocessor symbol that determines the inclusion of the resource".
Can you upgrade?
The edit window is disabled though, you have to jump through some hoops to set the Condition. Right-click the resource and choose "Insert Copy", then you can set the condition, then delete the original unconditional icon.
Maybe this trick would work in VS2008 as well, don't have it installed on this computer so I can't test it.