I have successfully built a C++ project using MSYS2 / MinGW64 / CMake tools. Now I would like to edit it and debug it in Visual Studio, as it is a nice IDE and the default development tool in Windows. Sadly, I am not able to build the project in Visual Studio because it cannot find libraries.
Here is the CMakeLists.txt
file I'm using where, among other things, I specify where are the include folders, the link files and link directories:
# src/CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
# Project name and current version
project(rascam VERSION 0.1 LANGUAGES CXX)
# Enable general warnings
# See http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
# Use 2014 C++ standard.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Must use GNUInstallDirs to install libraries into correct locations on all platforms:
include(GNUInstallDirs)
# Pkg_config is used to locate header and files for dependency libraries:
find_package(PkgConfig)
# Defines variables GTKMM_INCLUDE_DIRS, GTKMM_LIBRARY_DIRS and GTKMM_LIBRARIES.
pkg_check_modules(GTKMM gtkmm-3.0)
link_directories( ${GTKMM_LIBRARY_DIRS} )
include_directories( ${GTKMM_INCLUDE_DIRS} )
# Compile files:
add_executable(rascapp
cpp/main.cpp
cpp/main-window.cpp
)
# Add folder with all headers:
target_include_directories(rascapp PRIVATE hpp)
# Link files:
target_link_libraries(rascapp
${GTKMM_LIBRARIES}
)
To set up my environment I installed MSYS2 on a Windows platform, and added all the following packages via pacman:
pacman -S base base-devel net-utils git ruby wget man
pacman -S msys/openssh msys/vim msys/bc nano msys/tmux
pacman -S gzip zip unzip msys/p7zip tar msys/tree
pacman -S msys/winpty msys/ed msys/pwgen msys/zsh
pacman -S mingw64/mingw-w64-x86_64-jq
pacman -S msys/screenfetch
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw64/mingw-w64-x86_64-cmake
My project is dependent on Gtkmm, so I added the following dependency:
pacman -S mingw64/mingw-w64-x86_64-gtkmm3
Then I added the C:\Msys2\MinGW64\bin
to the path.
Then, I switched to the MSYS2/MinGW64 terminal, and fetched my project, created a build
folder and built it. As I'm in Windows, the CMake produces Visual Studio files by default. I would be fine with that, if only I could make it work. Running out of ideas, I specified the Unix Makefiles
option:
cd /c/where/your/root/folder/is
git clone https://github.com/cpp-tutorial/raspberry-cpp-gtk-opencv.git
cd raspberry-cpp-gtk-opencv
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug -G"Unix Makefiles" ../src/
make
./racapp.exe
And this does build the project. It produces an executable that I can launch from either MinGW64 or a Window Explorer by double clicking on it. I can also gdb it
After checking that the project looks right, I tried to open it in Visual Studio Community 2017 15.9.3 in two different ways:
- Use CMake to build a Visual Studio project: Visual studio opens the project and I can see the different targets, but when I launch a debug session it complains that it cannot found Gtk libraries. Looking in the project properties, I can see the that the libraries are taken from the correct path
C:\Msys2\MinGW64\bin
. And I verified that the library exists, although with a different name:libgtkmm-3.0.dll.a
instead ofgtkmm-3.0.dll
. - Use Visual Studio's Open File feature: Again, project opens, I can see the targets and the source files, but it complains that it doesn't find the include files
gtkmm.h
. I haven't managed to reach link phase, but my guess is that it wouldn't find libraries.
My question is, what is the correct and standard way to build and debug a MSYS2 / MinGW64 / CMake project in Visual Studio?