How does MSBuild specify object files for the linker? - visual-studio

I need to analyze exactly how MSBuild builds a C++ project, in order to rebuild it in a different way with a different toolchain. At the moment, my main strategy for doing this is to run msbuild -fl to ask it to log its actions, then I can try to analyze the resulting msbuild.log. The compile steps in this seem clear enough, but I am still trying to figure out the linking steps.
When I use Visual Studio 2022 to create a skeleton console application, the linking step is recorded in msbuild.log as
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\bin\HostX64\x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\olivine\test\ConsoleApplication1\x64\Debug\ConsoleApplication1.exe" /INCREMENTAL /ILK:"x64\Debug\ConsoleApplication1.ilk" /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\olivine\test\ConsoleApplication1\x64\Debug\ConsoleApplication1.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\olivine\test\ConsoleApplication1\x64\Debug\ConsoleApplication1.lib" /MACHINE:X64 /pdbthreads:4 x64\Debug\ConsoleApplication1.obj
that is, a lot of options, plus the name of the object file (the skeleton project only has one object file). That seems straightforward.
Now trying the same thing again on a skeleton desktop application:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\bin\HostX64\x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\olivine\test\Project1\x64\Debug\Project1.exe" /INCREMENTAL /ILK:"x64\Debug\Project1.ilk" /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\olivine\test\Project1\x64\Debug\Project1.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\olivine\test\Project1\x64\Debug\Project1.lib" /MACHINE:X64 /pdbthreads:4 x64\Debug\Project1.res
x64\Debug\Project1.obj
Again, the skeleton project only has one object file. This time, it is not placed at the end of the link command, but on a separate line after the link command. Inspection of msbuild.log for a real project suggests this pattern is repeated when there are multiple object files; they are mentioned after the link command, one per line.
What's up with this? Why the difference in output format between the two projects? And how exactly does MSBuild obtain the list of object files and communicate it to the linker?

Related

boost b2 --layout=system causing LNK1104 error on Visual Studio using CMake

I've compiled boost using:
b2.exe link=shared runtime-link=shared address-model=32 variant=release --layout=system --stagedir=./stage/win32
I'm loading boost components to CMake myself, specifically:
find_path(Boost_INCLUDE_DIR NAMES boost/config.hpp PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../external/boost_1_69_0)
message("${Boost_INCLUDE_DIR}")
add_library(Boost INTERFACE IMPORTED GLOBAL)
set_target_properties(Boost PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})
find_library(Boost_filesystem_LIBRARY NAMES boost_filesystem PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../external/boost_1_69_0/stage/win32/lib)
message("${Boost_filesystem_LIBRARY}")
add_library(Boost_filesystem UNKNOWN IMPORTED GLOBAL)
set_target_properties(Boost_filesystem PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})
set_target_properties(Boost_filesystem PROPERTIES IMPORTED_LOCATION ${Boost_filesystem_LIBRARY})
target_link_libraries(Boost INTERFACE Boost_filesystem)
When invoking CMake:
C:/bla/SW/cmake-template/external/boost_1_69_0
C:/bla/SW/cmake-
template/external/boost_1_69_0/stage/win32/lib/boost_filesystem.lib
-- Configuring done
-- Generating done
-- Build files have been written to: C:/bla/SW/cmake-template/build-windows
And last,
Link:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\
bin\HostX86\x86\link.exe /ERRORREPORT:QUEUE /OUT:"C:\bla\SW\cmake-template\build-windows
\mylib\Debug\mylib.dll" /INCREMENTAL /NOLOGO ..\myotherlib\Debug\myotherlib.lib "C:\bla\
SW\cmake-template\external\boost_1_69_0\stage\win32\lib\boost_filesystem.lib" kernel32.lib
user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib
advapi32.lib /DEF:"C:/bla/SW/cmake-template/build-windows/mylib/mylib.dir/Debug/exports.
def" /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PD
B:"C:/bla/SW/cmake-template/build-windows/mylib/Debug/mylib.pdb" /SUBSYSTEM:CONSOLE /TLB
ID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/bla/SW/cmake-template/build-windows/mylib/Debug/
mylib.lib" /MACHINE:X86 /SAFESEH /machine:X86 /DLL mylib.dir\Debug\mylib.obj
LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc141-mt-gd-x32-1_69.lib' [
C:\bla\SW\cmake-template\build-windows\mylib\mylib.vcxproj]
I don't get it, how come it's trying to link to libboost_filesystem-vc141-mt-gd-x32-1_69.lib and not to boost_filesystem.lib?
That link comes from the auto-ink feature of some of the Boost libraries. You can either adjust the auto-link to use the appropriate naming see macros for controlling this in Boost Config library. Or you can turn off the auto-linking with the BOOST_ALL_NO_LIB macro mentioned also in the Boost Config docs.

Error in premake generated linker command for (at least) visual studio 2010 express

There seems to be an error when generating VS2010 projects from premake, even with the "Hello World" example (Also tried with clink (here) and premake itself)
hello.c
#include <stdio.h>
int main(void) {
puts("Hello, word!");
return 0;
}
premake4.lua
-- premake4.lua
solution "HelloWorld"
configurations "Any"
project "HelloWorld"
kind "ConsoleApp"
language "C"
files "*.c"
defines { "DEBUG" }
flags { "Symbols" }
This simple example fails to link with the message "LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt"
The logfile says:
Build started 09-02-2015 16:06:34.
1>Project "E:\D\Source\premake-dev\pm-test\HelloWorld.vcxproj" on node 2 (build target(s)).
1>InitializeBuildStatus:
Creating "obj\Any\HelloWorld.unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D DEBUG /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fo"obj\Any\\" /Fd".\HelloWorld.pdb" /Gd /TC /analyze- /errorReport:prompt hello.c
hello.c
ManifestResourceCompile:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\rc.exe /nologo /fo"obj\Any\HelloWorld.exe.embed.manifest.res" obj\Any\HelloWorld_manifest.rc
Link:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:".\HelloWorld.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"obj\Any\HelloWorld.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"E:\D\Source\premake-dev\pm-test\HelloWorld.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /ENTRY:"mainCRTStartup" /DYNAMICBASE /NXCOMPAT /IMPLIB:".\HelloWorld.lib" /MACHINE:X86 obj\Any\HelloWorld.exe.embed.manifest.res
obj\Any\hello.obj
1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
1>Done Building Project "E:\D\Source\premake-dev\pm-test\HelloWorld.vcxproj" (build target(s)) -- FAILED.
Build FAILED.
Time Elapsed 00:00:00.33
SOLVED!!
It was an error in my pristine VS2010 install!! See
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d10adba0-e082-494a-bb16-2bfc039faa80/vs2012-rc-installation-breaks-vs2010-c-projects?forum=vssetup
Although i never installed anything but VS2010 ..
SOLUTION: From this page:
http://blogs.msdn.com/b/heaths/archive/2011/04/01/visual-c-2010-sp1-compiler-update-for-the-windows-sdk-7-1.aspx
Copy c:\windows\Microsoft.Net\Framework\v4.0.30319\ cvtres.exe (42Kb)
into Programs(x86)\Microsoft Visual Studio 10.0\VC\Bin\

Writing Permission for Executable on Windows 7

I try to compile some stuff with Matlab, and during the process Matlab should create some
temporary folders. Unfortunately, for some reason, this is not happening and therefore the compilation fails. I executed Matlab with Administrator rights, but the problems reminds. I am not sure if their is some other problem with the User Access Control under Windows 7. Could anyone give me some instructions of how I can make sure that Matlab has all the privileges to write temporary files?
EDIT:
Actually it is mex file compilation, so the error could also come from the VS compiler:
--> cl /c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD /FoC:\Temp\mex_OmN_AO\test.obj -I"C:\Program Files\MATLAB\R2011a\extern\include" -I"C:\Program Files\MATLAB\R2011a\simulink\include" /O2 /Oy- /DNDEBUG -DMX_COMPAT_32 vcd_count_transitions.c
test.c
Contents of C:\Temp\mex_OmN_AO\mex_tmp.rsp:
C:\Temp\mex_OmN_AO\test.obj
--> link /out:"test.mexw64" /dll /export:mexFunction /MAP /LIBPATH:"C:\Program Files\MATLAB\R2011a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:"C:\Temp\mex_OmN_AO\templib.x" /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib #C:\Temp\mex_OmN_AO\mex_tmp.rsp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Temp\mex_OmN_AO\test.obj
LINK : fatal error LNK1104: cannot open file 'test.mexw64'
C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'test.mexw64
I would expect after the compilation fails, there should be C:\Temp\mex_OmN_AO directory,
but there is nothing in my TEMP folder.
Many thanks for your help!

Compiling C file in Matlab with Visual Studio

I have a C-File that I wanna compile in Matlab with MS Visual Studio 10.
For that I use the following command:
mex -v test.c
The output that I get after compilation is the following:
test.c
Contents of C:\Temp\mex_kpuu4t\mex_tmp.rsp:
C:\Temp\mex_kpuu4t\test.obj
--> link /out:"test.mexw64" /dll /export:mexFunction /MAP /LIBPATH:"C:\Program Files\MATLAB\R2011a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:"C:\Temp\mex_kpuu4t\templib.x" /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib #C:\Temp\mex_kpuu4t\mex_tmp.rsp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Temp\mex_kpuu4t\test.obj
LINK : fatal error LNK1104: cannot open file 'test.mexw64'
C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'test.mexw64' failed.
Anyone an idea what could go wrong here? The interesting thing is, that the test.obj file seems not to be written to the temp folder, could that be the issue here?
Many thanks!
Maybe you called "test" from a previously compiled mex? If yes, it might still be loaded to Matlab's memory, and cannot be overwritten. Try:
clear mex
and then compile again.

Build dll from command line [ Visual Studio ]

I have developed a DLLin c++ using visual studio 2008. I want to run static code analysis on it using a tool which requires me to compile it on command line.
How can i use cl.exe to achieve this.
My project depends on the boost date_time library and couple other referenced projects.
How to build a DLL from the command line in Windows using MSVC
UPDATE: command line
cl /Od /GL /I "..\..\..\..\..\..\..\..\..\..\..\..\..\boost144" /I "..\include" /I "..\..\..\..\..\cincludes" /I "..\..\BASBESUtil\include" /I "..\..\..\..\..\..\..\..\..\..\..\wutility\include" /I "..\..\..\t\include" /I "..\..\..\..\..\..\..\..\..\..\..\enterprise\common\LicenseLib" /I "..\..\Common" /I "..\\" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "UNICODE" /D "_UNICODE_" /D "_WINDLL" /D "_UNICODE" /FD /EHa /MD /Zc:wchar_t- /Fo"Release\\" /Fd"Release\vc90.pdb" /W3 /c /Wp64 /Zi /TP <<src files >> /OUT:".\Release\snaplv.dll" /INCREMENTAL:NO /LIBPATH:"..\..\..\..\..\..\..\..\..\..\..\..\..\boost144\stage\lib" /DLL /MANIFEST /MANIFESTFILE:"Release\snaplv.dll.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"c:\DevEnv\PerforceWorkspace\dev\adamodarachar\snap-tools\LicenseValidator\main\enterprise\BAS\main\server\nativeutil\src\core\main\cpp\xyxmgr\xyxmgr\Release\snaplv.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /DYNAMICBASE:NO /MACHINE:X86 /NXCompat /DynamicBase version.lib Ws2_32.lib rpcrt4.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib <<obj files>>
Instead of calling the compiler executable directly and passing all those options manually, you can simply call MSVC, and pass the project file on the command line.
devenv.com MyProject.sln /Build "Release|x86_64" /Project MyLib
In my copy of MSVS v8, devenv.com can be found in C:\Programs\Microsoft Visual Studio 8\Common7\IDE. Call devenv.com /? for a list of options.
From the Tools menu select the option Visual Studio 2008 Command Prompt ?

Resources