C 編程環境搭建 Window 篇


前言 - 簡介

  我們在寫代碼的過程中, 不可避免的重度依賴所處的開發環境. 本文重點帶大家在 Window 搭建 C

簡單控制台項目. 當作存檔, 用於記錄項目搭建各種重復操作.  在詳細過程之前, 我們約定下基礎環境

  Best new version Window

  Best new version Visual Studio

例如筆者當前是 Windows 10 + Visual Studio 2019, 其中 Windows 推薦開啟 UTF-8 支持. 

那此間, 騷年去喚醒自己的道 ~ 

 

正文 - 過程

  正文分三個過程. 其一是 Visual Studio C Consule 常用設置. 其二是導出模板, 來解放生產力. 其三演示使用.

Visual Studio C Consule 常用設置

1. 創建 c_template 項目

  

 

2. 只保留 x64 環境

人的精氣有限, 做釘子更省力.

3. 添加基礎 main.c

4. Visual Studio 項目詳細配置

a). 區域環境配置

項目右擊 -> [配置屬性] -> [高級] -> [字符集] -> [未設置]

b). 添加包含庫
項目右擊 -> [配置屬性] -> [鏈接器] -> [輸入]

psapi.lib
user32.lib
shell32.lib
ws2_32.lib
userenv.lib
iphlpapi.lib
advapi32.lib

c). 添加預編譯處理器
項目右擊 -> [配置屬性] -> [C/C++]-> [預處理器] -> [預處理器定義]

[Debug]
_DEBUG

[Release]
NDEBUG
 
_LARGEFILE_SOURCE
_FILE_OFFSET_BITS=64
WIN32_LEAN_AND_MEAN
_CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_DEPRECATE
_WINSOCK_DEPRECATED_NO_WARNINGS

d). 設置編譯額外選項
項目右擊 -> [配置屬性] -> [C/C++] -> [常規] -> [調試信息格式] -> [程序數據庫 (/Zi)]
項目右擊 -> [配置屬性] -> [C/C++] -> [代碼生成] -> [運行庫] -> [多線程/MT]
項目右擊 -> [配置屬性] -> [C/C++] -> [高級] -> [編譯為] -> [編譯為C代碼/TC]
項目右擊 -> [配置屬性] -> [C/C++] -> [高級] -> [禁用特定警告] -> [4098]
項目右擊 -> [配置屬性] -> [C/C++] -> [命令行] -> [/D "restrict=__restrict"]
項目右擊 -> [配置屬性] -> [鏈接器] -> [常規] -> [啟用增量鏈接] -> [否 (/INCREMENTAL:NO)]
項目右擊 -> [配置屬性] -> [鏈接器] -> [系統] -> [子系統] -> [控制台]
項目右擊 -> [配置屬性] -> [鏈接器] -> [命令行] -> /IGNORE:4099
 
詳細配置遇到不明白可以自行搜索, 可以圖了解, 也可以 求精深

 

導出模板

  上面這些每次操作都添加, 很惡心. 我們可以通過 [項目] -> [導出模板] 一勞永逸. ~

1. 前戲

  找到 c_template.vcxproj 項目文件, 通過你的慧眼, 將其中所有關於 Win32 相關的 xml 配置刪除.

 

2. 導出模板

[項目] -> [導出模板]

添加額外補充

(圖片什么的可以因自己喜好自己整)

 

演示使用

最終生成如下模板內容

不妨既興通過這個模板演示一段代碼 

#include <stdio.h>
#include <float.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

/*
 題目: 
    地上右一個 m 行 n 列的方格. 
    一個機器人從坐標(0, 0) 的格子開始移動, 它每次可以向左, 右, 上, 下移動一格, 
    但不能進入行坐標和列坐標的數位之和大於 k 的格子. 例如, 當 k 為 18 的時候,
    機器人能夠進入方格 (35, 37), 因為 3 + 5 + 3 + 7 = 18. 
    但它不能進入方格 (35, 38), 因為 3 + 5 + 3 + 9 = 19.
    請問該機器人能夠到達多少個格子?
 */

extern int moving_count(int m, int n, int threshold);

int main(int argc, char * argv[]) {
    int m = 20, n = 20, threshold = 10;
    int count = moving_count(m, n, threshold);
    printf("<m = %d, n = %d, threshold = %d> -> %d\n", m, n, threshold, count);
    return 0;
}

struct visite {
    int  rows;
    int  cols;
    int  threshold;
    bool visited[];
};

inline struct visite * visite_create(int rows, int cols, int threshold) {
    struct visite * v = malloc(sizeof(struct visite) + (rows * cols) * sizeof (int));
    assert(v && rows > 0 && cols > 0 && threshold > 0);
    v->rows = rows;
    v->cols = cols;
    v->threshold = threshold;
    memset(v->visited, 0, (rows * cols) * sizeof (int));
    return v;
}

inline void visite_delete(struct visite * v) {
    if (v) free(v);
}

static inline int get_digit_sum(int num) {
    int sum = 0;
    while (num > 0) {
        sum = num % 10;
        num /= 10;
    }
    return sum;
}

inline bool visite_check(struct visite * v, int row, int col) {
    if (row >= 0 && row < v->rows && col >= 0 && col < v->cols && !v->visited[row * v->cols + col]) {
        return get_digit_sum(row) + get_digit_sum(col) <= v->threshold;
    }
    return false;
}

int visite_moving(struct visite * v, int row, int col) {
    if (!visite_check(v, row, col))
        return 0;

    v->visited[row * v->cols + col] = true;
    return 1 + visite_moving(v, row, col - 1) 
             + visite_moving(v, row, col + 1) 
             + visite_moving(v, row - 1, col) 
             + visite_moving(v, row + 1, col);
}

int 
moving_count(int m, int n, int threshold) {
    if (m < 0 || n < 0 || threshold < 0)
        return 0;
    if (threshold == 0)
        return 1;

    struct visite * v = visite_create(m, n, threshold);

    int count = visite_moving(v, 0, 0);

    visite_delete(v);

    return count;
}

(有心的道友, 也可以轉成棧回溯. )

 

后記 - 展望

錯誤是難免的, 歡迎朋友指正互相印證苦中作樂.

明月照小樓

  立秋 - 劉翰 - 南宋
  乳鴉啼散玉屏空,一枕新涼一扇風。
  睡起秋聲無覓處,滿階梧桐月明中。

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM