windows下使用cmake和mingw32构建编译项目

windows下使用cmake和mingw32构建编译项目

1. 安装cmake, mingw32环境

  • cmake下载地址:https://github.com/Kitware/CMake/releases/download/v3.28.1/cmake-3.28.1-windows-x86_64.msi
  • mingw32下载地址:https://sourceforge.net/projects/mingw-w64/
    两个软件都是直接下载安装就行,安装路径可以自己选择,但是要记住安装路径,后面会用到。

2. 配置环境变量

编辑系统变量Path,添加两个路径:D:\myApp\mingw64\bin;D:\myApp\cmake\bin

  • 出现下面的情况,说明cmake配置成功
    在cmd中输入cmake --version
G:\proj\test>cmake --version
cmake version 3.28.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
  • 出现下面的情况,说明mingw32配置成功
    在cmd中输入gcc --version; mingw32-make --version
G:\proj\test>gcc --version
gcc (x86_64-win32-seh-rev1, Built by MinGW-Builds project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

G:\proj\test>mingw32-make --version
GNU Make 4.4.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

3. 编译项目

  • 项目目录结构
G:.
│  CMakeLists.txt
│  readme.md
│
├─build
└─src
        test_shared_ptr.cpp
  • 用cmake生成makefile, mingw32-make编译
cd build
cmake .. -G "MinGW Makefiles" 
mingw32-make
  • 输出结果
G:\proj\test\demo>cd build

G:\proj\test\demo\build>cmake .. -G "MinGW Makefiles"
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/myApp/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/myApp/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.0s)
-- Generating done (0.0s)
-- Build files have been written to: G:/proj/test/demo/build

G:\proj\test\demo\build>mingw32-make
[ 50%] Building CXX object CMakeFiles/MyProject.dir/src/test_shared_ptr.cpp.obj
[100%] Linking CXX executable MyProject.exe
[100%] Built target MyProject

G:\proj\test\demo\build>MyProject.exe
[2024-01-10 18:40:34.350] [info] Welcome to spdlog!
[2024-01-10 18:40:34.352] [error] Some error message with arg: 1
[2024-01-10 18:40:34.352] [warning] Easy padding in numbers like 00000012
[2024-01-10 18:40:34.353] [critical] Support for int: 42; hex: 2a; oct: 52; bin: 101010
[2024-01-10 18:40:34.353] [info] Support for floats 1.23
[2024-01-10 18:40:34.354] [info] Positional args are supported too..
[2024-01-10 18:40:34.354] [info] left aligned
[2024-01-10 18:40:34.354] [debug] This message should be displayed..
[18:40:34] : This an info message with custom format
a.use_count() = 1
b.use_count() = 2
a.use_count() = 2
b.use_count() = 2

G:\proj\test\demo\build>

附录1

  • cmakelists.txt文件内容
cmake_minimum_required(VERSION 3.12)
project(MyProject)

# Add your source files here
set(SOURCES
    src/test_shared_ptr.cpp
    # Add your header files here
    # header1.h
    # header2.h
)

# Add your header files here
include_directories(G:/third_party/spdlog/include)


# Add any additional libraries or dependencies here
# find_package(...)

# Set the executable target
add_executable(MyProject ${SOURCES} ${HEADERS})

  • test_shared_ptr.cpp文件内容
#include 
#include 

#include "spdlog/spdlog.h"

struct A;
struct B;

struct A
{
    std::shared_ptr<B> pointer;
    ~A()
    {
        std::cout << "A 被销毁" << std::endl;
    }
};
struct B
{
    std::shared_ptr<A> pointer;
    ~B()
    {
        std::cout << "B 被销毁" << std::endl;
    }
};

void TestSpdlog()
{
    spdlog::info("Welcome to spdlog!");
    spdlog::error("Some error message with arg: {}", 1);

    spdlog::warn("Easy padding in numbers like {:08d}", 12);
    spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);

    spdlog::info("Support for floats {:03.2f}", 1.23456);
    spdlog::info("Positional args are {1} {0}..", "too", "supported");
    spdlog::info("{:<30}", "left aligned");

    spdlog::set_level(spdlog::level::debug); // Set global log level to debug
    spdlog::debug("This message should be displayed..");

    // change log pattern
    spdlog::set_pattern("%^[%T] %n: %v%$");
    spdlog::info("This an info message with custom format");
}

void TestSharedPtr()
{
    auto a = std::make_shared<A>();
    auto b = std::make_shared<B>();
    a->pointer = b;
    std::cout << "a.use_count() = " << a.use_count() << std::endl;
    std::cout << "b.use_count() = " << b.use_count() << std::endl;
    b->pointer = a;
    std::cout << "a.use_count() = " << a.use_count() << std::endl;
    std::cout << "b.use_count() = " << b.use_count() << std::endl;
}

int main()
{

    TestSpdlog();
    TestSharedPtr();
    return 0;
}

附录2

Windows下的文件系统常用指令
dir:列出当前目录中的文件和子目录。

cd:更改当前目录。例如,cd Documents将会将当前目录更改为Documents子目录。

md或mkdir:创建一个新的目录。例如,md new_directory将会创建一个名为new_directory的新目录。

rd或rmdir:删除一个目录。例如,rd old_directory将会删除名为old_directory的目录。

copy:复制文件。例如,copy source.txt destination.txt将会复制source.txt文件到destination.txt

xcopy:复制文件和目录,包括子目录。

del:删除一个或多个文件。例如,del old_file.txt将会删除old_file.txt文件。

move:移动文件和重命名文件或目录。

ren或rename:重命名文件或目录。

type:显示文件的内容。

find:在文件中查找文本。

tree:以图形方式显示目录结构。

attrib:更改文件属性。

chkdsk:检查磁盘的状态。

diskpart:磁盘分区工具。

format:格式化磁盘。

fsutil:执行各种文件系统操作。

请注意,这些命令可能需要不同的参数和选项,你可以通过在命令后面添加/h/help(例如,copy /help)来获取关于命令的更多信息。

你可能感兴趣的:(C++,windows,c++,mingw32,cmake)