Alter.Org.UA
 << Back Home UK uk   Donate Donate

MSVC vcxproj with relative path using CMake

Q:

CMake can generate Visual Studio projects, which is great! However, it looks like they are not supposed to be included in the source code repository. Sometimes I need to cooperate with people who are not familiar with CMake and are used to VS solution/vcxproj or is a legacy project where CLR/C++ is used for C#/Cpp communication. Is there a way to generate valid VS Cpp projects that would be OK to be shared along with the source code?

A (official):

The build trees (including the .vcxproj files) are not meant to be relocatable, and are specific to the machine you're on. They are supposed to be generated by each user.
Generating the .vcxproj files from the CMake project is not that difficult, especially with the CMake GUI.

A (practical):

Though, cmake has no option to build vcxproj with relative path, it is not too difficult. And sometimes it is really more convinient to create projects from command line (CLI) rather than with GUI. It is recommented to add to CMakeLists.txt the following to avoid further build problems.

CMakeLists.txt
...
set(CMAKE_SUPPRESS_REGENERATION ON)
set(CMAKE_USE_RELATIVE_PATHS ON)
...
_prebuild.cmd
rem (re)build .sln and .vcxproj files for both platforms in different subdirectories
cmake -S . -B build64 -A x64
cmake -S . -B build32 -A Win32

rem used-specified path to project hierarchy root, leave empty 
rem if CMakeLists.txt is located in your project root dir
rem e.g. ..\..
set root_rel_path=%1

rem remember cmake directory (absolute path) to return to
cd > _.tmp
set /p cmake_dir= < _.tmp
del _.tmp

rem get absolute path of root directory (cmake directory + relative root) 
rem and return back to cmake dir
cd %root_rel_path%
cd > _.tmp
set /p cur_dir= < _.tmp
del _.tmp
rem construct full paths with inverted slashes (template for replacement)
SET cur_dir2=%cur_dir:\=/%
cd %cmake_dir%

rem replace absolute paths with relative to $(SolutionDir), 
rem we always add extra '..\' since solution in built in directory 1 level deeper 
rem than cmake directory (see two 1st lines of script)
rem you can use any other search-replace tool, I use my own old one

cd build32
srchrep.exe -src "%cur_dir%" -dest "$(SolutionDir)\%root_rel_path%\..\" *.vcxproj.*
srchrep.EXE -src "%cur_dir2%" -dest "$(SolutionDir)\%root_rel_path%\..\" *.vcxproj.*
cd ..

cd build64
srchrep.EXE -src "%cur_dir%" -dest "$(SolutionDir)\%root_rel_path%\..\" *.vcxproj.*
srchrep.EXE -src "%cur_dir2%" -dest "$(SolutionDir)\%root_rel_path%\..\" *.vcxproj.*
cd ..

Example

c:\
  work\
    my_proj\ (project root)
      inc
      windows (here we have CMakeLists)
        build32 (here we have .sln, .vcxproj for Win32)
        build64 (here we have .sln, .vcxproj for x64)
      linux

> cd c:\work\my_proj\windows
> _prebuild.cmd ..

$(SolutionDir) = c:\work\my_proj\windows\build64
project root = c:\work\my_proj = $(SolutionDir)\..\..
c:\work\my_proj\windows -> $(SolutionDir)\..\..\windows
c:\work\my_proj\inc -> $(SolutionDir)\..\..\inc
c:\work\my_proj\windows\build64 -> $(SolutionDir)\..\..\windows\build64
...

Download

cmake_vcxproj_rel_path.rar
cmake_vcxproj_rel_path.zip




2023.05.29


FB or mail alterX@alter.org.ua (remove X)   Share
designed by Alter aka Alexander A. Telyatnikov powered by Apache+PHP under FBSD © 2002-2024