I would like to add version information to a Fortran EXE when it is built. I would like the version number to follow the notes below. What is the recommended approach for doing this?
Major = pulled from file Minor = pulled from file Release = number of days since Jan 1 2000 Build = number of seconds since midnight (00:00:00), divided by 2 |
Registry change:
Initial Value
HKEY_CLASSES_ROOT\.rc.
(Default) , REG_SZ, BDS.rc
Change value "BDS.rc" to ""
HKEY_CLASSES_ROOT\.rc.
(Default) , REG_SZ,
链接已复制
There are many ways to do this.
One suggestion is to add to the solution a Pre-Build step (that runs a program/batch script), that generates the desired data into an appropriate source file (e.g. a module file).
You may want to add a Post-Build step that copies this file to a LastBuild....) file then deletes the module file.
Jim Dempsey
You can add a resource file (.rc) to your build that contains info Windows can display for version info. I have attached a sample file here (generated by Visual Studio), the important block is this:
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,314,159
PRODUCTVERSION 1,0,314,159
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x0L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "TODO: <Company name>"
VALUE "FileDescription", "TODO: <File description>"
VALUE "FileVersion", "1.0.314.159"
VALUE "InternalName", "TODO: <Internal name>"
VALUE "LegalCopyright", "Copyright (C) 2025"
VALUE "OriginalFilename", "TODO: <Original filename>"
VALUE "ProductName", "TODO: <Product name>"
VALUE "ProductVersion", "1.0.314.159"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
You could use a pre-build step to create this file and it would get built in automatically. When you ask Windows for the version info, you see something like this:
This data can also be retrieved with a call to a Windows API routine (GetFIleVersionInfo)
As a first test, I am trying to get the version information added to the EXE, then worry about dynamically updating it. First added the example resource1.rc to the project. After adding the existing item, it appears under the "resource files" folder in the project solution. Next ran "Clean" then "Build". The resulting EXE did not have any version information added to it. The build log gave the following below. Is there some additional configuration needed?
Compiling with Intel® Fortran Compiler Classic 2021.11.0 [IA-32]... ifort /nologo /debug:full /Od /Qdiag-disable:10448 /warn:interfaces /module:"Debug\\" /object:"Debug\\" /Fd"Debug\vc170.pdb" /traceback /check:bounds /check:stack /libs:dll /threads /dbglibs /c /Qlocation,link,"C:\Program Files\Microsoft Visual Studio\2022-LTSC-17.8.9\VC\Tools\MSVC\14.38.33130\bin\HostX86\x86" /Qm32 "C:\Users\me.COMPANY\source\repos\VersionTest\VersionTest\Main.for" Linking... Link /OUT:"Debug\VersionTest.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"Debug\VersionTest.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\bryan.MBS\source\repos\VersionTest\VersionTest\Debug\VersionTest.pdb" /SUBSYSTEM:CONSOLE /IMPLIB:"C:\Users\bryan.MBS\source\repos\VersionTest\VersionTest\Debug\VersionTest.lib" -qm32 "Debug\Main.obj" Embedding manifest... mt.exe /nologo /outputresource:"C:\Users\me.COMPANY\source\repos\VersionTest\VersionTest\Debug\VersionTest.exe;#1" /manifest "Debug\VersionTest.exe.intermediate.manifest" VersionTest - 0 error(s), 0 warning(s)
If you right click the RC in the solution explorer pane does it have a build instruction on the properties tab?
No.
In the Resource1.rc "Property Pages | Configuration Properties | Custom Build Step | General" are fields empty except the "Description" which has "Performing Custom Build Step...".
It needs to compile to make a .res
BTW I have never added this step it has been set up when the project is made.....
so if you do a clean and then a build is it make the .res file?
It threw a wobbler "can't open resource.h file", I opened the rc in a text ed, deleted the include lines for that at the top and did a rebuild. (X64 / iFX)
I think adding a "new" ie empty RC to the project and then in the VS resource editor adding a version object (control) is probably a better way to do this from the start.
Test 1:
--------
I commented out all 3 include lines, clean, rebuild. No version info.
Test 2:
--------
Removed Resource1.rc
Created blank file Resource2.rc
Added existing item Resource2.rc
Double clicked Resource2.rc in solution explorer to open it in visual studio.
In the editor, Rt-clicked on Resourec2.rc | Add Resource
In the Add resource dialog double clicked "Version"
Clicked save all, I am given prompt:
The resource script ... Resource2.rc was not created using Microsoft Visual Studio. Comments, macros, preprocessor directives, and conditionally included information may be modified and/or removed from this file during the build process. Replace existing file?
Selected "Yes"
Clean
Build
No version info.
Test 3:
--------
Closed all open files in visual studio
Double clicked Resource2.rc in solution explorer
Get the following errors:
error RC2144 : PRIMARY LANGUAGE ID not a number
error RC2135: file not found: 1
error RC2135: file not found: TEXTINCLUDE
error RC2135: file not found: END
"Created blank file Resource2.rc", what is meant was under resources do "add"> "new item">"Resource File"> "add" . The system will create the resource file.
"add"> "new item">"Resource File"> "add". This gives:
fatal error RC1015: cannot open include file 'winres.h'.
Is this a search path issue?
Questions:
1/ Where should this file be found? (C:\Program Files\Microsoft Visual Studio\2022-LTSC-17.8.9\SDK\ScopeCppSDK\vc15\SDK\include\um\winres.h?)
2/ What controls where it searches? (windows environment path variable?)
Those are questions that you should not need to answer. Your installation must be broken!
When I tested this, I right clicked on the project, selected Add > New Item and selected Resource File. I then opened the resource file, right clicked on Resource1.rc in the resource file, selected Add Resource > Version. By adding the resource file this way, it gets added to the project in a way that will get it compiled and linked in.
winres.h ought to be found from the list of folders under Include Files in the Intel Compilers and Libraries > IFX > Compilers dialog, but I've seen issues in the past where this was not honored by the resource compiler (part of Visual Studio's C++ support.) If for some reason this isn't working you can edit the .rc file and insert the complete path to the file.
Summary:
=========
Still no version information.
Questions:
=========
1/ Is there a way to know if the resource compiler was successfully run when compiling?
2/ Are there any special considerations/install order when I install Visual Studio, Windows SDK, and Intel Fortran so that versioning will work?
3/ Any suggestions on what should I try next?
Details:
=======
I tested adding the full path to all files referenced directly in the .rc file. Unfortunately, they referenced additional file dependencies. Since I do not want to edit these files that are needed outside of my test project, I instead did a temporary desperate move and copied all files in the "C:\Program Files\Microsoft Visual Studio\2022\Preview\SDK\ScopeCppSDK\vc15\SDK\include\um\" folder and "C:\Program Files\Microsoft Visual Studio\2022\Preview\SDK\ScopeCppSDK\vc15\SDK\include\shared\" folder to be beside my Fortran source code for this project.
Note: this is only temporary hack to try to bypass this search path problem in a proof of concept.
When this was done, I was able to Add > New Item and selected resource file, then opened the resource file, right clicked on Resource4.rc in the resource file, selected Add Resource > Version. Before I couldn't even open the resource file in visual studio. So, this is progress!
However, when I tried to compile the program, it still does not contain any resource information.
At this point it feels like the resource compiler is not being invoked or did not run successfully. Is there any way to determine if it was? My guess is that something path related might be causing this too.
Also, I installed everything on a second machine recently, and when I tested on that machine, I am getting the same issue with the resource file #include references not being found. So, it seems that how I installed things was incorrect, or both machines had their installs corrupted, at least as far as the paths needed for resource files are concerned.
Any gotchas I need to watch out for when I try to re-install again?
If you clean and build you should see it as above. It makes a .res file
On my windows 11 VS2022 system it installed the SDK at C:\Program Files (x86)\Windows Kits\10
You should not need to install the SDK. When you install the C++ components with VS you get the sdk and resource editor/ compiler etc
1/ Resource compiler not listed in Build output:
2/ SDK path exists
3/ Visual Studio installer when I ran it in modify mode, shows "Desktop development with C++" is already installed.
