1

I have a Native C++ Static Library. I am using Visual Studio 2012 in Windows and Eclipse in Linux.

Is there any Built-in functionality to give a Static Library Version information?

I tried following the same steps I took for my C++/CLI library but it doesn't seem to have any effect.

It builds but I don't see the version in any place.

How do you set versioning information to a Static Library?

  • Is there built-in functionality in Visual Studio to this purpose?
  • If not, What is the correct way for setting the Version information? Any best practices?
  • How do you do it for Linux? (I build my Library for windows as a *.lib file and for Linux as a .a file, I need to keep version information)

Update

This is the VersionInfo with the SpecialBuild that Paul Suggested. The field doesn't show in any place.

VS_VERSION_INFO VERSIONINFO
 FILEVERSION 61,0,0,1
 PRODUCTVERSION 1,0,0,1
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
 FILEFLAGS 0x21L
#else
 FILEFLAGS 0x20L
#endif
 FILEOS 0x40004L
 FILETYPE 0x2L
 FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040004b0"
        BEGIN
            VALUE "CompanyName", "TODO: <Company name>"
            VALUE "FileDescription", "TODO: <File description>"
            VALUE "FileVersion", "61.0.0.1"
            VALUE "InternalName", "ExxonMobil.Rapid.Services.TEM"
            VALUE "LegalCopyright", "Copyright (C) 2014"
            VALUE "OriginalFilename", "ExxonMobil.Rapid.Services.TEM"
            VALUE "ProductName", "TODO: <Product name>"
            VALUE "ProductVersion", "1.0.0.1"
            VALUE "SpecialBuild ", "Blah"           
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x400, 1200
    END
END
Community
  • 1
  • 1
Dzyann
  • 5,062
  • 11
  • 63
  • 95
  • A static library for linux is a `.a` file, not `.so` – Ben Voigt Apr 23 '14 at 21:54
  • @BenVoigt, Yes you are right, I was thinking of a shared library, sorry I will fix it – Dzyann Apr 24 '14 at 12:13
  • 2
    The `VersionInfo` structure is normally used for executable and dynamic library files, not for static libraries. The reason is that the resource is not referenced by any module in the static library nor in the program, so the linker optimizes it away. You would have to ensure the resource symbol is public, and add a library method the app could call to retrieve the structure, or parts of the structure. – Jesse Chisholm Jun 02 '16 at 15:39

2 Answers2

2

As far as I know, there is no specific version information for static libraries, since a static library is part of the entire application.

What you could do is use one of the existing VersionInfo entries, maybe SpecialBuild, that lists the version of the static library used to build the application. Of course, you will have to invent a version numbering system for the static library.

There is one issue with the "SpecialBuild" entry, and that is that the VersionInfo viewer that comes with the Windows OS (open Explorer, right click on an executable file to bring up the context menu, choose "Properties" and go to the "Details tab) doesn't show the SpecialBuild information. This to me is an oversight from Microsoft.

There are third party viewers such as this one that shows the resource information: http://www.naughter.com/versioninfo.html. If this is not an option, there are other string fields in the version information that you can use to store additional information.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • I see, I thought it was the same as with the Dynamic Libraries. I am not sure what do you mean with "SpecialBuild", is that a property that can be set in a resource file? – Dzyann Apr 23 '14 at 12:06
  • The VersionInfo resource has a "SpecialBuild" entry, plus other entries that can be filled in with string information. http://msdn.microsoft.com/en-us/library/windows/desktop/aa381058(v=vs.85).aspx – PaulMcKenzie Apr 23 '14 at 15:10
  • Paul sorry I didn't understand you first, I don't have much experience doing this. I am trying to add the SpecialBuild but you have to set flags, and they do not work, do you know where I could get the underlying value of the flags? I couldn't find it In the documentation. – Dzyann Apr 23 '14 at 16:11
  • That's ok. The definitions are in the file `verrsrc.h`. You will find it in the Platform SDK headers. If you installed the Platform SDK (you should do this anyway, even if it isn't required for most apps), the directory where this file is found (assuming you use the defaults) should be: `C:\Program Files\Microsoft SDKs\Windows\v7.0A` or something similar to that. – PaulMcKenzie Apr 23 '14 at 16:28
  • Thanks Paul, I tried in different projects, and it doesn't work, I never see the SpecialBuild in the properties of the file. I updated my post with the content of my VersionInfo. – Dzyann Apr 23 '14 at 17:01
  • What version info viewer are you using? If it's the one that comes with Windows, unfortunately it's useless for looking at "SpecialBuild". Get another one that shows all the info. The other alternative is to place the info in one of the other string fields that do show up in the Window's info viewer. – PaulMcKenzie Apr 23 '14 at 17:23
  • Another alternative is to load your executable in Visual Studio as a resource. You can then view the SpecialBuild information. What I had to do to overcome the limitation of the Windows viewer is to write my own code to open up the executable and view the resources. However the warning I will give you is that it isn't easy at all to do this, even though it would look straightforward. There is no easy-to-use API that is built into the OS that walks the version information. – PaulMcKenzie Apr 23 '14 at 17:38
  • Yes I was using the one that comes with Windows, I want to provide a way for my users to easily verify what version they are using. From your post I kept searching and apparently people saves the information in const declared on a header file. It has the same issue though, I would have to build a special tool to read it. (like this guy suggests: http://stackoverflow.com/a/1638251/752842) – Dzyann Apr 23 '14 at 17:59
  • If you can go with MFC, here is a class that displays the info: http://www.naughter.com/versioninfo.html I would like to show you my code to do this, but it's proprietary. Edit: There is a TestVersion.exe in the Release folder of the zip file in that link. Maybe you can use that without rebuilding the app. – PaulMcKenzie Apr 23 '14 at 18:10
  • Paul I am not sure I could use that class, I will take a look into it. However I feel you have answered my question. Do you mind adding the information on your answer so I accept it? For now I have it with the const char* of that other post I found, but you have to open the file and search. Notepad cant handle it, I had to use Notepad++ – Dzyann Apr 23 '14 at 20:18
  • I find two problems with this solution: 1) this means the application needs to know at compile time what string to put in for `SpecialBuild` to refer to the library. 2) there is only one `SpecialBuild` so this works for only one static library. You need to get the information in the library itself, and retrievable by the application at run time. That means a method in the library that returns the version information. – Jesse Chisholm Jun 02 '16 at 15:42
2

You can include your compile time, or any version string of your choice, in .obj and .lib files using

#pragma comment( user, "Compiled on " __DATE__ " at " __TIME__ )

See the MSDN documentation

It's not going to appear in any obvious location, though, like file properties for the .lib

Probably more useful is to have a function that returns the version string, that way applications that link with the library can display the version in their "About" dialogs.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Interesting, I will look into it, but this would be good only for Visual Studio, no? – Dzyann Apr 24 '14 at 13:31
  • @Dzyann: The Microsoft compilers and all those which try to be compatible, which are pretty much all the ones that use `.lib` for static libraries (so not including the mingw or cygwin versions of gcc, which create `.a` libraries in a totally different format) – Ben Voigt Apr 24 '14 at 14:45
  • Yes sorry that Is what I meant. I would have to do something separate for Linux. – Dzyann Apr 24 '14 at 15:21
  • @Dzyann: Well, the idea I called "more useful" will work equally well on Linux. – Ben Voigt Apr 24 '14 at 15:42
  • re: `good only for Visual Studio?` Yes and No. Yes == The exact syntax of putting a string in the object file or library file is different based on COMPILER, so this exact style may well only work in VS. No == There is usually something similar in other compilers. E.g., on AIX in the xlC_r compiler it would be `#pragma copyright("Compiled on " __DATE__ " at " __TIME__)` Sadly, gcc doesn't appear to have such a simple pragma. – Jesse Chisholm Jun 02 '16 at 15:49