<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>~iany/ C++</title><link>https://blog.iany.me/tags/cpp/</link><description>Recent content in C++ «~iany/»</description><language>en-US</language><managingEditor>me@iany.me (Ian Yang)</managingEditor><webMaster>me@iany.me (Ian Yang)</webMaster><copyright>CC-BY-SA 4.0</copyright><lastBuildDate>Sun, 05 Mar 2017 12:13:07 +0800</lastBuildDate><atom:link href="https://blog.iany.me/tags/cpp/index.xml" rel="self" type="application/rss+xml"/><item><title>Vcpkg static linking</title><link>https://blog.iany.me/2017/03/vcpkg-static-linking/</link><pubDate>Sun, 05 Mar 2017 12:13:07 +0800</pubDate><author>me@iany.me (Ian Yang)</author><guid>https://blog.iany.me/2017/03/vcpkg-static-linking/</guid><description>&lt;p&gt;&lt;a href="https://github.com/Microsoft/vcpkg"&gt;Vcpkg&lt;/a&gt; is a tool published by Microsoft, which is used to manage C/C++ libraries in Windows. It makes libraries installation easier, and it works well with CMake.&lt;/p&gt;
&lt;p&gt;But it is not straitforward to staticly link the depdendent libraries using vcpkg.&lt;/p&gt;
&lt;p&gt;Under Linux，static linking just need to use &lt;code&gt;.a&lt;/code&gt; files instead of &lt;code&gt;.so&lt;/code&gt; files (similar in macOS). Use &lt;code&gt;boost&lt;/code&gt; as an example, the bundled &lt;code&gt;FindBoost&lt;/code&gt; returns found static library files in &lt;code&gt;Boost_LIBRARIES&lt;/code&gt; when setting following flags.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-cmake"&gt;set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then just use &lt;code&gt;Boost_LIBRARIES&lt;/code&gt; in &lt;code&gt;link_libraries&lt;/code&gt; or &lt;code&gt;target_link_libraries&lt;/code&gt; to statically link boost.&lt;/p&gt;
&lt;p&gt;But when these two flags are set in Windows using vcpkg, CMake will complain that it cannot find static libraries of boost. It seems a constraint of VC compiler, so &lt;a href="https://blogs.msdn.microsoft.com/vcblog/2016/11/01/vcpkg-updates-static-linking-is-now-available/"&gt;vcpkg requires install the static version of libraries explicitly&lt;/a&gt;. For example, to install boost static 64bit libraries:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;vcpkg install zlib:x64-windows-static
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And &lt;code&gt;VCPKG_TARGET_TRIPLET&lt;/code&gt; is required to generate VC project:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;cmake .. \
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg.cmake \
-DVCPKG_TARGET_TRIPLET=x64-windows-static \
-G &amp;quot;Visual Studio 14 2015 Win64&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But the project generated throws error in linking. It can be fixed referring this Stack Overflow thread: &lt;a href="http://stackoverflow.com/questions/14172856/cmake-compile-with-mt-instead-of-md"&gt;CMake compile with /MT instead of /MD&lt;/a&gt;. Here is the final CMake sample:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-cmake"&gt;cmake_minimum_required(VERSION 3.0)
project(static_link_test CXX)
set(CMAKE_CXX_STANDARD 11)
if(MSVC)
string(REPLACE &amp;quot;/MD&amp;quot; &amp;quot;/MT&amp;quot; CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
string(REPLACE &amp;quot;/MD&amp;quot; &amp;quot;/MT&amp;quot; CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REPLACE &amp;quot;/MD&amp;quot; &amp;quot;/MT&amp;quot; CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
else(MSVC)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
endif(MSVC)
find_package(Boost 1.36.0 REQUIRED COMPONENTS filesystem program_options)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/src
${Boost_INCLUDE_DIRS}
)
link_libraries(
${Boost_LIBRARIES}
)
add_executable(static_link_test
src/main.cpp
)
install(TARGETS static_link_test RUNTIME DESTINATION bin)
&lt;/code&gt;&lt;/pre&gt;</description><category domain="https://blog.iany.me/post/">Posts</category><category domain="https://blog.iany.me/tags/cpp/">C++</category><category domain="https://blog.iany.me/tags/windows/">Windows</category></item></channel></rss>