jmgonet
jmgonet

Reputation: 1261

How to build and debug a MSYS2 / MinGW64 / CMake project in Visual Studio 2017?

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:

  1. 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 of gtkmm-3.0.dll.
  2. 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?

Upvotes: 1

Views: 6154

Answers (2)

paddy
paddy

Reputation: 103

This is no longer true, you can build and debug projects using MingW gdb by following the steps mentioned below using the latest Visual Studio IDE: https://devblogs.microsoft.com/cppblog/using-mingw-and-cygwin-with-visual-cpp-and-open-folder/. I've tried for a bigger project and it works fine apart from the fact that the debugging is not very clean.

Upvotes: 1

jmgonet
jmgonet

Reputation: 1261

As other users commented (thanks, @DavidGrayson), the gcc.exe and g++.exe compilers that are installed in the MSYS2 configuration do not use the format that Visual Studio requires for debug symbols. In consequence, it is not possible to use Visual Studio for debugging executables built with Mingw.

To be able to debug (and build and edit), use some other IDE, more MinGW friendly, like Code::Blocks.

Still, to answer my own question, I describe here the procedure to configure the project in Visual Studio to edit the source files, build and launch the executable. For this procedure, no need to use CMake for anything:

  1. Launch Visual Studio 2017
  2. File -> Open -> Folder...
  3. Select the folder that contains the top CMakeLists.txt.
  4. It should find automatically that this is a CMake project, and try to load it. Maybe it complains about some errors.
  5. CMake -> Change CMake Settings -> Choose your top CMakeLists.txt.
  6. In the dialog, look for Mingw64-Release (actually, there is a *Mingw64-Debug, but you will not be able to debug anyway).
  7. If everything went well, a CMakeSettings.json has appeared on your project, and Visual Studio rebuilds the project, this time without errors.
  8. Choose an executable target.
  9. Launch.

It should build then execute the application.

Upvotes: 1

Related Questions