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

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\"

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

/Fe (Name EXE File) is not working

I would like to compile a cpp source file in windows 7, visual studio 2010 using
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01 for x64
When I do the following
>cl /EHs main.cpp -o test
I got
cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release
Ok fair enough. I've checked this page /Fe (Name EXE File). This option is not working with me.It gives me this error
cl : Command line warning D9024 : unrecognized source file type 'test', object file assumed
main.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
test
LINK : fatal error LNK1181: cannot open input file 'test.obj'
Now how should I activate this option? I always find it difficult to read MSDN Library. It is not friendly at all.
Note that there is no space between /Fe and the file name.
cl /EHs /Fetest.exe main.cpp
Alternatively, you can use the colon syntax:
cl /EHs /Fe: test.exe main.cpp
You can achieve same thing using
cl /Ehs main.cpp /link /OUT:test.exe
/link tells cl to pass these options to linker and /OUT specifies the name of output file.

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

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.

Which compilation options should I use to link with Boost using cl.exe?

I have a program which I would like compile using cl.exe on the command-line. This program depends on some boost libraries which I fail to link to.
The error I'm getting is:
cl /Fosamples\proxy\proxy.obj /c samples\proxy\proxy.cpp /TP /O2 /EHsc
/DBOOST_ALL_NO_LIB /DBOOST_THREAD_USE_LIB /DBOOST_SYSTEM_USE_LIB
/DBOOST_USE_WINDOWS_H /DTAP_ID=\"tap0901\" /D_WIN32_WINNT=0x0501 /MD /nologo
/Isamples\proxy /Iinclude proxy.cpp
link /nologo /MD /OUT:samples\proxy\proxy.exe /LIBPATH:samples\proxy
/LIBPATH:lib asiotap.lib libboost_system-vc100-mt-1_47.lib
libboost_thread-vc100-mt-1_47.lib ws2_32.lib gdi32.lib iphlpapi.lib
advapi32.lib samples\proxy\proxy.obj
LINK : warning LNK4044: unrecognized option '/MD'; ignored
asiotap.lib(bootp_builder.obj) : error LNK2001: unresolved external
symbol "class boost::system::error_category const & __cdecl
boost::system::system_category(void)"
(?system_category#system#boost##YAAEBVerror_category#12#XZ)
I compiled Boost, using the following command-line, from the x64 MSVC command prompt:
.\b2.exe install toolset=msvc --prefix=C:\Boost-VC-x64
If I look inside libboost_system-vc100-mt-1_47.lib I can see that:
?system_category#system#boost##YAABVerror_category#12#XZ
Is exported. But If you look closely it differs a bit from the one in my compilation errors:
?system_category#system#boost##YAAEBVerror_category#12#XZ // The symbol I miss
?system_category#system#boost##YAABVerror_category#12#XZ // The exported symbol
I guess I should either change Boost or my compilation options but fail to figure what to change exactly. Any clue ?
Thank you very much.
After some investigations, I realized that I compiled Boost for a x86 platform where I was linking with a x64 binary.
I thought that compiling Boost inside a Visual Studio x64 command prompt was enough but you actually have to specify:
.\b2.exe install toolset=msvc address-model=64 --prefix=C:\Boost-VC-x64
To make it work.

compiling v8 on windows

When I am trying to compile like this
cl /Iinclude hello_world.cpp v8.lib
I get this error
LINK : fatal error LNK1104: cannot open file 'kernel32.lib'
and when I am trying to do it like this
cl /Iinclude v8.lib Winmm.lib WSock32.lib hello_world.cpp ws2_32.lib
I am getting this error
LINK : fatal error LNK1181: cannot open input file 'Winmm.lib'
how to fix this?
The compiler/linker can't find the Windows lib files. There are lots of ways to resolve this. For example, you can add the paths to the locations of the lib files to the LIB environment variable. Or you can pass the /LIBPATH option to the linker.
Finally, it doesn't look like you are compiling V8 here. It would appear that you are just trying to link to the V8 library that has already been compiled.