0

vcpkg: A good C++ package manger

Every time I need to add a library on my code I struggle on configurations, dependencies, incompatibilities and more…

And this problem becomes even more complex, when you try to build something with lots of libraries. For instance, in the demoengine we have lots of libraries like:

As you can imagine, configuring all those libraries manually (and keeping them up to date), is really painful! but, lucky for us, we have vcpkg! as mentioned in the main page: “vcpkg is a free C/C++ package manager for acquiring and managing libraries. Choose from over 1500 open source libraries to download and build in a single step or add your own private libraries to simplify your build process. Maintained by the Microsoft C++ team and open source contributors.

I have been using it for years, and everything I can say about vcpkg is that simply works as a charm, and saved me thousands of hours of wasted time 🙂

I even use it for my “test” projects when I want to test some library (fi: EnTT or PhysicsFS), in order to have a “quick and dirty” project working as fast as possible.

The instructions to use it are super simple, and I recommend to have a look at the official documentation, but here you have the simple steps that I do normally, it may be useful for you 🙂

Step 1: Preconditions

In order to be able to prepare a project you will need to install first the following:

  • Git
  • CMake
  • A compiler, in my case, I use Visual Studio 2022 Community Edition

Step 2: Clone vcpkg repo and install the packages

In the batch file below you can see that I download the vcpkg repository, and then I call the “bootstrap-vcpkg.bat” file to build vcpkg, then I install the desired packages, in this case “physfs” and “stb”, please note that I have linked them dynamically, but you can link them statically as well if you want by changing the “–triplet x64-windows” to “–triplet x64-windows-static“:

rmdir vcpkg /s /q
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
call bootstrap-vcpkg.bat
vcpkg install --recurse physfs stb --triplet x64-windows
cd..

You can browse the vcpkg available packages from here.

Step 3: Run CMake to build your project

Before running CMake you will need first to prepare a CMakeLists.txt file, and include the desired libraries (physfs and stb), I normally use always the same CMakeLists in my projects and I just tweak it a little bit, based on the needs of the project, here you have the one used in this example:

cmake_minimum_required(VERSION 3.8)
set(CMAKE_TOOLCHAIN_FILE "../vcpkg/scripts/buildsystems/vcpkg.cmake")
project(physfs_test)
set(CMAKE_CXX_STANDARD 20)

if (MSVC)
	add_compile_options(/MP)
	add_compile_options(-bigobj)
	add_compile_options(
		"$<$<CONFIG:Debug>:/MTd>"
		"$<$<CONFIG:RelWithDebInfo>:/MT>"
		"$<$<CONFIG:Release>:/MT>"
		"$<$<CONFIG:MinSizeRel>:/MT>"
	)
endif()

# Hide console and allow main() to be the entry point
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup")

# HACK: Hide some warnings
add_definitions(-D_CRT_SECURE_NO_WARNINGS)

file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.c ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE HEADER_FILES ${CMAKE_SOURCE_DIR}/src/*.h ${CMAKE_SOURCE_DIR}/src/*.hpp ${CMAKE_SOURCE_DIR}/src/*.ipp ${CMAKE_SOURCE_DIR}/src/*.inl)
file(GLOB_RECURSE RESOURCE_FILES ${CMAKE_SOURCE_DIR}/res/*.rc)

# Dependencies
find_package(PhysFS CONFIG REQUIRED)

add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES} ${RESOURCE_FILES})
if(MSVC)
	set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE PhysFS::PhysFS)

message($CMAKE_BUILD_TYPE)

include_directories("${CMAKE_SOURCE_DIR}/src" "${CMAKE_SOURCE_DIR}/include")

# Set project working dir
set_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

# Declare Visual Studio project virtual folders
if(MSVC_IDE)
	# Macro to preserve source files hierarchy in the IDE
	macro(GroupSources curdir)
		file(GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${curdir} ${PROJECT_SOURCE_DIR}/${curdir}/*)

		foreach(child ${children})
			if(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${curdir}/${child})
				GroupSources(${curdir}/${child})
			else()
				string(REPLACE "/" "\\" groupname ${curdir})
				string(REPLACE "src" "Sources" groupname ${groupname})
				source_group(${groupname} FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
			endif()
		endforeach()
	endmacro()

	# Run macro
	GroupSources(src)
endif()

As you can see, you just need to include 2 lines for Physfs (find_package and target_link_libraries), and nothing is required for stb since it’s just a header file.

Once the file is ready, we can run CMake and the project will be created with all the required setup!

rmdir physfs_test_vs2022 /s /q
mkdir physfs_test_vs2022
cd physfs_test_vs2022
cmake.exe -DBUILD_SHARED_LIBS=ON -DVCPKG_TARGET_TRIPLET=x64-windows -G "Visual Studio 17 2022" ..
cd..
pause

I hope you find it useful! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

spammer, go home! * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.