![](/img/trans.png)
[英]MSYS2 MinGW64 to build GMP/MPFR on Windows as static library, and link them in MSVC project compiled with CL
[英]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中打開它:
C:\Msys2\MinGW64\bin
。 我驗證了該庫存在,盡管名稱不同: libgtkmm-3.0.dll.a
而不是gtkmm-3.0.dll
。gtkmm.h
。 我還沒有設法達到鏈接階段,但我的猜測是它找不到庫。我的問題是,在 Visual Studio 中構建和調試 MSYS2 / MinGW64 / CMake 項目的正確和標准方法是什么?
正如其他用戶評論(感謝@DavidGrayson)一樣,在MSYS2配置中安裝的gcc.exe
和g++.exe
編譯器不使用Visual Studio調試符號所需的格式。 因此, 無法使用Visual Studio調試使用Mingw構建的可執行文件 。
為了能夠進行調試(以及構建和編輯),請使用其他一些對MinGW更友好的IDE,例如Code :: Blocks 。
盡管如此,為了回答我自己的問題,我在這里描述了在Visual Studio中配置項目以編輯源文件,生成和啟動可執行文件的過程。 對於此過程,無需對任何東西使用CMake :
CMakeLists.txt
的文件夾。 CMakeLists.txt
。 CMakeSettings.json
, Visual Studio會重新生成項目,這次沒有錯誤。 它應該先構建然后執行應用程序。
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.