-1

I am trying to link a 3rd party lib statically to my own .exe so that I only need to distribute one single .exe file. First, I built all the 3rd party static lib from source, generating several .lib files.

Then in visual studio, in my own project, I set Project->properties->C/C++->Code generation->Runtime Library to Multi-threaded (/MT) . However, the linker would error

LNK2038 mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in ConsoleApplication1.obj

The latter part MT_StaticRelease does make sense since I am trying static linking, while the former part MD_DynamicRelease is absolutely wrong because I provided static lib. I set Project->properties->Linker->General->Additional Library Dependencies to a folder that only contains *.lib. And I am PRETTY SURE that these *.lib are static library instead of import library.

When I change to Project->properties->C/C++->Code generation->Runtime Library to Multi-threaded DLL (/MD) , the build succeed.

It is really weird that visual studio recognize *.lib as dynamic lib.

  1. I've checked %PATH% to rule out any other possible polution. Do you have any ideas?
  2. I've deleted all the cache from previous build.
#include "gdcmReader.h"

#ifdef _WIN32
#pragma comment(lib, "gdcmDSED")
#pragma comment(lib,"WS2_32")
#endif

typedef std::set<gdcm::DataElement> DataElementSet;
typedef DataElementSet::const_iterator ConstIterator;

int main(int argc, char *argv[])
{
    if (argc < 2) {
        std::cout << "Usage: xxxx.exe dicom_filename" << std::endl;
        return 1;
    }

    const char *filename = argv[1];

    gdcm::Reader reader;
    reader.SetFileName(filename);
    if (!reader.Read())
    {
        std::cerr << "Could not read: " << filename << std::endl;
        return 1;
    }
    std::stringstream strm;

    gdcm::File &file = reader.GetFile();
    gdcm::DataSet &ds = file.GetDataSet();
    gdcm::FileMetaInformation &fmi = file.GetHeader();


    if (ds.FindDataElement(gdcm::Tag(0x10, 0x10))) {
        const gdcm::DataElement &de = ds.GetDataElement(gdcm::Tag(0x10, 0x10));
        std::cout << de << std::endl;
    }
    return 0;
}
  • Though details of what you are doing are missing, I guess that you confuse the linking with some external library and the options for linking with the Windows runtime. –  Nov 02 '22 at 10:16
  • @YvesDaoust Yes, I am trying to link a third party lib statically. Isn't that the same as link static library of windows, e.g. kernel32.lib. – Mark Hayes Nov 02 '22 at 10:43

1 Answers1

0

Thanks to this comment, I've solved this. The problem is that 3rd party lib is not properly built.

When building 3rd party lib, although I set Project->Properties->General->Project Defaults->Configuration Type Static library (.lib), that's to say I am building static lib.

However, I use Project->properties->C/C++->Code generation->Runtime Library to Multi-threaded DLL(/MD). When changing it to Multi-threaded (/MT), everything is fine.