簡體   English   中英

如何在 Visual Studio 2017 中構建和調試 MSYS2 / MinGW64 / CMake 項目?

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

我已經使用 MSYS2 / MinGW64 / CMake 工具成功構建了 C++ 項目。 現在我想在 Visual Studio 中對其進行編輯和調試,因為它是一個不錯的 IDE 和 Windows 中的默認開發工具。 遺憾的是,我無法在 Visual Studio 中構建項目,因為它找不到庫。

這是我正在使用的CMakeLists.txt文件,其中我指定了包含文件夾、鏈接文件和鏈接目錄的位置:

# 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}  
)

為了設置我的環境,我在 Windows 平台上安裝了 MSYS2,並通過 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

我的項目依賴於Gtkmm ,所以我添加了以下依賴項:

pacman -S mingw64/mingw-w64-x86_64-gtkmm3

然后我將C:\Msys2\MinGW64\bin添加到路徑中。

然后,我切換到 MSYS2/MinGW64 終端,獲取我的項目,創建一個build文件夾並構建它。 正如我在 Windows 中一樣, CMake默認生成Visual Studio文件。 我會很好的,只要我能讓它工作。 沒有想法,我指定了Unix Makefiles選項:

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

這確實構建了項目。 它會生成一個可執行文件,我可以通過雙擊它從 MinGW64 或 Window Explorer 啟動它。 我也可以gdb

在檢查項目看起來正確后,我嘗試以兩種不同的方式在Visual Studio Community 2017 15.9.3中打開它:

  1. 使用CMake構建Visual Studio項目:Visual Studio 打開項目,我可以看到不同的目標,但是當我啟動調試 session 時,它抱怨它找不到 ZE6094CAAEAA630167ZCCA0F55504C8BE8 庫。 查看項目屬性,我可以看到這些庫取自正確的路徑C:\Msys2\MinGW64\bin 我驗證了該庫存在,盡管名稱不同: libgtkmm-3.0.dll.a而不是gtkmm-3.0.dll
  2. 使用Visual Studio的打開文件功能:再次打開項目,我可以看到目標和源文件,但它抱怨它沒有找到包含文件gtkmm.h 我還沒有設法達到鏈接階段,但我的猜測是它找不到庫。

我的問題是,在 Visual Studio 中構建和調試 MSYS2 / MinGW64 / CMake 項目的正確和標准方法是什么?

正如其他用戶評論(感謝@DavidGrayson)一樣,在MSYS2配置中安裝的gcc.exeg++.exe編譯器不使用Visual Studio調試符號所需的格式。 因此, 無法使用Visual Studio調試使用Mingw構建的可執行文件

為了能夠進行調試(以及構建和編輯),請使用其他一些對MinGW更友好的IDE,例如Code :: Blocks

盡管如此,為了回答我自己的問題,我在這里描述了在Visual Studio中配置項目以編輯源文件,生成和啟動可執行文件的過程。 對於此過程,無需對任何東西使用CMake

  1. 啟動Visual Studio 2017
  2. 文件 -> 打開 -> 文件夾...
  3. 選擇包含頂部CMakeLists.txt的文件夾。
  4. 它應該自動發現這是一個CMake項目,並嘗試加載它。 也許它抱怨一些錯誤。
  5. CMake- > 更改CMake設置 ->選擇頂部的CMakeLists.txt
  6. 在對話框中,查找Mingw64-Release (實際上,有一個* Mingw64-Debug,但無論如何您將無法調試)。
  7. 如果一切順利,則項目中會出現一個CMakeSettings.jsonVisual Studio會重新生成項目,這次沒有錯誤。
  8. 選擇一個可執行目標。
  9. 發射。

它應該先構建然后執行應用程序。

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-視覺 cpp 和打開文件夾/ 我已經嘗試過一個更大的項目,除了調試不是很干凈之外,它工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM