How to tell if a lib was compiled with /mt or /md? - c++

Given a compiled lib, is there a way to tell if it was compiled with /md or /mt just by looking at it (maybe with dumpbin tool)?
Edit:
dumpbin /directives foo.lib is a solution for the case where the lib was not compiled with /GL switch. Is there an option to inspect a lib file that was optimized in such a way?

Yes, you could use dumpbin's /DIRECTIVES option to find which runtime libraries the objects in the .lib want to link with:
dumpbin /directives foo.lib
Look for instances of the runtime libraries specified here. For example, you might see:
/DEFAULTLIB:MSVCRTD (module compiled with /MDd)
or
/DEFAULTLIB:MSVCRT (module compiled with /MD)
or
/DEFAULTLIB:LIBCMT (module compiled with /MT)
There will probably be many /DEFAULTLIB directives, so you can search using terms like:
dumpbin /DIRECTIVES foo.lib | find /i "msvcr"

A very nice feature of the Microsoft compiler is that it preserves the command line that was used to compile a source file into the .obj file. Which allows you to find it back by looking at the .lib file with, wait for it, Notepad.exe. Just search for "cl.exe".
For example, this is what I see when I use Notepad opened on a sample library named Win32Project1.lib that I built with VS2013:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe cmd -c -ZI -nologo -W3 -WX- -sdl -Od -Oy- -DWIN32 -D_DEBUG -D_LIB -DHELLO_STACKOVERFLOW -D_UNICODE -DUNICODE -Gm -EHs -EHc -RTC1 -MDd -GS -fp:precise -Zc:wchar_t -Zc:forScope -Ycstdafx.h -Fp"c:\Users\hpass_000\documents\visual studio 2013\Projects\Win32Project1\Debug\Win32Project1.pch" -Fo"c:\Users\hpass_000\documents\visual studio 2013\Projects\Win32Project1\Debug\" -Fd"c:\Users\hpass_000\documents\visual studio 2013\Projects\Win32Project1\Debug\vc120.pdb" -Gd -TP -analyze- -errorreport:prompt -I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" -I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include" -I"C:\Program Files (x86)\Windows Kits\8.1\Include\um" -I"C:\Program Files (x86)\Windows Kits\8.1\Include\shared" -I"C:\Program Files (x86)\Windows Kits\8.1\Include\winrt" -X src stdafx.cpp pdb c:\Users\hpass_000\documents\visual studio 2013\Projects\Win32Project1\Debug\vc120.pdb
As you can tell, I compiled with /MDd
Do beware that a .lib can contain multiple .obj files with possibly different settings. Searching for "-mt" and "-md" lets you find out quickly.

Related

Adding OR-Tools Library to Visual Studio

I am trying to write a code using Google's OR-Tools library on Microsoft Visual Studio 2019. I followed the following steps:
Installed OR-Tools from Binary on Windows on their website.
Extracted the .zip file in C:\Libraries
Wrote my code on VS (I wrote #include <ortools/linear_solver/linear_solver.h> and using namespace operations_research; rest is usual C++ Code)
In Visual Studio, went to Project > Properties > C/C++ > Additional Include Directories
Added "C:\Libraries\or-tools\include" (which contains the folder "ortools" that I included)
Clicked Apply then OK then compiled my code.
I am getting a bunch of linking errors "error LINK2019". Is there anything else I should do so I can use this library freely on my machine?
From the supplied makefile:
Compile flags:
DEBUG = /O2 -DNDEBUG
CXXFLAGS = /std:c++17 /EHsc /MD /nologo /D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS -nologo $(DEBUG) \
/DPSAPI_VERSION=1 /D__WIN32__ /DNOMINMAX /DWIN32_LEAN_AND_MEAN=1 /D_CRT_SECURE_NO_WARNINGS \
/DGFLAGS_DLL_DECL= /DGFLAGS_DLL_DECLARE_FLAG= /DGFLAGS_DLL_DEFINE_FLAG= /DGOOGLE_GLOG_DLL_DECL= \
/I$(INC_DIR)\\src\\windows /I$(INC_DIR) /I. \
/DUSE_BOP /DUSE_GLOP \
/DUSE_CBC /DUSE_CLP \
/DUSE_SCIP
Link flags:
LDFLAGS = psapi.lib ws2_32.lib
OR_TOOLS_LNK = lib\\ortools.lib

Change Output Directory in Dev C++

I want to separate .c/.cpp files and .exe files in different folders.
How it's possible??
When I execute the code -
C++ Compiler: C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\g++.exe
Command: g++.exe "D:\C\init\pointers\String.cpp" -o "D:\C\init\pointers\String.exe" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib" -static-libgcc
Necromancy here, I know, but I've just found myself in the same situation.
I'm using Dev-C++ 5.11, the current up to date fork, not the original Bloodshed one.
Dev-C++ allow to directly compile cpp files but the option you're looking for is related to project options. You must create a new project, add your code and then you can find it under "Project Options" -> "Output" -> "Executable output directory".

Building a DLL with both assembly and C++ functions

I need to create a single DLL containing both C++ and MASM functions.
I compile C++ files with g++ which outputs .o files.
Is it possible to use these with MASM linker? If not, how can I link these?
Edit:
I tried converting .o file to .obj using objconv utility and then linking it with assembly object files but it didn't work.
Here are the commands I used:
g++ -mabi=ms -c copy.cpp
objconv -fcoff64 copy.o copy.obj
C:\masm32\bin\ml /c /Zd /coff init.asm series.asm windows.asm
C:\masm32\bin\link /DLL /SUBSYSTEM:WINDOWS /DEF:mydll.def /LIBPATH:c:\masm32\lib init.obj series.obj windows.obj copy.obj
And the output is:
copy.obj : fatal error LNK1136: invalid or corrupt file
What am I doing wrong?

Can clang-modernize update Windows/MFC codebases?

In an attempt to add the "override" keyword to my codebase, and without any better options, I've tried to get clang-modernize to update my source files. Eventually clang chokes on the various safety macros in the Windows headers.
clang-modernize -risk=safe -summary -add-override -include-from=Src\inc Src\AuditFile.cpp -- -std=c++11 -ISrc "-IC:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" "-Ic:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include" "-IC:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include" -DWIN32 -D_WINDOWS -D_DEBUG -D_CRTDBG_MAP_ALLOC -D_UNICODE -DUNICODE -D_AFXDLL -D_DLL -fms-compatibility -fmsc-version=1000 -fcxx-exceptions -fexceptions -w -D_M_AMD64=1 -D_M_X64=1
First "compilation" error:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winnt.h:366:9: error: unknown type name '__possibly_notnullterminated'
typedef __possibly_notnullterminated WCHAR *PNZWCH;
^
I'd like to know if anyone has worked through these issues, and how.
I came across this question a bit late, but I've seen this issue.
Make sure that you include all the include directories. And the system includes should not be used with -I, but with -isystem.
Hope that helps.

LINK1104 cannot open boost static library using visual studio 2008 command prompt

I'm trying to compile a cpp file which uses static boost libraries. I'm using the visual studio 2008 command prompt as I have not set up a VS project file.
The command I'm using is (run from the folder containing my source code):
cl /EHsc /I "C:\Program Files\boost\boost_1_53_0" Client.cpp
The error is:
LINK: fatal error LNK1104: cannot open file 'libboost_system-vc90-mt-s-1_53.lib'
However, the file 'libboost_system-vc90-mt-s-1_53.lib' can be found in "C:\Program Files\boost\boost_1_53_0\stage\lib" so my understanding is that I've installed boost properly and I'm just failing to link to it?
I've tried including it directly using
cl /EHsc /I"C:\Program Files\boost\boost_1_53_0" /I "C:\Program Files\boost\boost_1_53_0\stage\lib\" Client.cpp
which gives the same error.
I've also tried linking to it directly using /link as follows:
cl /EHsc /I"C:\Program Files\boost\boost_1_53_0" /link "C:\Program Files\boost\boost_1_53_0\stage\lib\libboost_system-vc90-mt-s-1_53.lib" Client.cpp
Which returns a different error:
cl : Command line error D8003 : missing source filename
I seem to be calling the compiler flags wrong? But I can't see where/how.
There is a similar question here,but the solution involves issues with how visual studio/ the project file is set up. Since I don't have a project file, is there an easy solution for the above that I can't see or would I need to set up a project?
Thanks for any help in advance!
The linker needs to be told where the library file is located. You were very close with the last command line, but the file name needs to precede the /link option. This should work:
cl /EHsc /I"C:\Program Files\boost\boost_1_53_0" Client.cpp /link "C:\Program Files\boost\boost_1_53_0\stage\lib\libboost_system-vc90-mt-s-1_53.lib"
Also, when linking to multiple libraries in the same directory, it is more concise to use the LIBPATH option to tell the linker where to look for .lib files.
cl /EHsc /I"C:\Program Files\boost\boost_1_53_0" Client.cpp /link "libboost_system-vc90-mt-s-1_53.lib" /LIBPATH:"C:\Program Files\boost\boost_1_53_0\stage\lib\"