2

I need to get the file version from a DLL/EXE file. I primarily wrote my code from this answer.


#ifdef _WIN32
#include <winver.h>
#include <windows.h>
void getFileVersion(LPCTSTR szVersionFile)
{
    DWORD  verHandle = 0;
    UINT   size      = 0;
    LPBYTE lpBuffer  = nullptr;
    DWORD  verSize   = GetFileVersionInfoSize( szVersionFile, &verHandle);
 
    if (verSize != 0)
    {
        LPSTR verData = new char[verSize];
 
        if (GetFileVersionInfo( szVersionFile, verHandle, verSize, verData))
        {
            if (VerQueryValue(verData,"\\", (VOID FAR* FAR*)&lpBuffer,&size))
            {
                if (size)
                {
                    VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
                    if (verInfo->dwSignature == 0xfeef04bd)
                    {
 
                        // Doesn't matter if you are on 32 bit or 64 bit,
                        // DWORD is always 32 bits, so first two revision numbers
                        // come from dwFileVersionMS, last two come from dwFileVersionLS
                        Logger::log(LOGFILE, LOGSTART, "File Version: %d.%d.%d.%d\n",
                        ( verInfo->dwFileVersionMS >> 16 ) & 0xffff,
                        ( verInfo->dwFileVersionMS >>  0 ) & 0xffff,
                        ( verInfo->dwFileVersionLS >> 16 ) & 0xffff,
                        ( verInfo->dwFileVersionLS >>  0 ) & 0xffff
                        );
                    }
                }
            }
        }
        delete[] verData;
    }
}
#endif


#ifdef DLL
void MyApplication::init(const void * mdScalarVariableMap)
#elif EXE
void MyApplication::init()
#endif
{

std::string szVersionFile(this->resourceLocation);
#ifdef DLL
    szVersionFile.append("/libapplication_x86.dll");
#elif EXE
    szVersionFile.append("/MyApp.exe");
#endif

    getFileVersion(szVersionFile.c_str());
}

I am compiling this with a CMAKE file. I am extremely new to cmake and have no idea how to write it, however after going through everything I understood that I have to link the library "libversion.a" for the actual method definitions.

I altered the cmake from this :

  # Let's ensure -std=c++xx instead of -std=g++xx
  set(CMAKE_CXX_EXTENSIONS OFF)
  # Ensure that object files are renamed correctly src.obj instead of src.cpp.obj
  set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE ON)
  # Let's nicely support folders in IDEs
  set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  # Set install prefix to source directory
  set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR} CACHE STRING "Install prefix" FORCE)
  # Statically link libraries
  set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")

  # ----Compiler options----
  # Optionally set things like CMAKE_CXX_STANDARD, CMAKE_POSITION_INDEPENDENT_CODE here
  set(CMAKE_CXX_STANDARD 11)

to this :

  # Let's ensure -std=c++xx instead of -std=g++xx
  set(CMAKE_CXX_EXTENSIONS OFF)
  # Ensure that object files are renamed correctly src.obj instead of src.cpp.obj
  set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE ON)
  # Let's nicely support folders in IDEs
  set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  # Set install prefix to source directory
  set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR} CACHE STRING "Install prefix" FORCE)
  # Statically link libraries
  set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -lversion -static")

  # ----Compiler options----
  # Optionally set things like CMAKE_CXX_STANDARD, CMAKE_POSITION_INDEPENDENT_CODE here
  set(CMAKE_CXX_STANDARD 11)

However, everytime I attempt to compile, I get this error :

../src/App.a(App.obj):App.cpp:(.text+0xa48): undefined reference to `GetFileVersionInfoSizeA@8'
../src/App.a(App.obj):App.cpp:(.text+0xa86): undefined reference to `GetFileVersionInfoA@16'
../src/App.a(App.obj):App.cpp:(.text+0xab7): undefined reference to `VerQueryValueA@16'

I am using the GCC compiler. gcc --version returns

gcc (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Can someone please help me to get the version info? Any pointers will be gratefully appreciated.

doa4321
  • 176
  • 2
  • 9
  • 1
    Instead of adding `-l` flag to variable `CMAKE_EXE_LINKER_FLAGS` use `target_link_libraries` command. This is a proper way to link with a library in CMake. – Tsyvarev Dec 01 '21 at 08:19
  • Thank you!! This solved my issue right away - thanks for the tip. – doa4321 Dec 01 '21 at 09:54

0 Answers0