美文网首页
基础C++教学⮱⮱001

基础C++教学⮱⮱001

作者: 平知 | 来源:发表于2019-11-10 07:57 被阅读0次

←↑→↓↖↙↗↘↕
unicode=Geometric Shapes
▶◆◉➼
Miscellaneous Symbols

Dingbats
✍✎✎

章节号 内容            
第1章节 C++常用开发工具
第2章节 使用Code::Blocks简要分析C++的预处理
第3章节 编程规范
第4章节 编译过程简探
第5章节 mingw含带的二进制工具一览

  ↑↓类的基本结构:

第1章节  C++常用开发工具

章内序 描述            
1、Code::Blocks 下载安装
2、CLion 下载安装,工具使用说明
3、Notepad++ 简单安装NppExec
4、相关内容 mingw下载,设置等

需要相关内容,请ctrl+F复制章内序内容快速定位。


  1、Code::Blocks
  注意:下载的时候,要选择带编译器的安装。mingw是g++在windows平台下的版本,具体内容可以自行百度关键字。单独安装还是不够简单的,直接使用这个比较容易一些。


  界面如下

  新建一个console application工程↓

  选择c++↓

  进行相关设置(最好不要出现中文字符)↓

  直接调试运行↓

  输出即如果↓

  设置mingw
  搜索mingw关键词↓


  找到mingw的bin目录,复制下来↓

  添加到环境变量中↓

  使用cmd,输入g++ -v命令,出现信息则表示添加成功↓

  命令行调试cpp文件
  在某路径下,新建一个文本文件,输入代码:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

  保存为1.cpp。在当前路径下,shift+右键,打开powershell。


  输入start cmd,运行cmd

  使用命令编译g++ -o 1 1.cpp。相等于:把1.cpp编译成可执行文件1。
  直接执行:1。输出了结果。

  2、CLion
  下载地址:https://www.jetbrains.com/clion/download/#section=windows 建议下载2019.2.4版本



  必要的工具:https://pan.baidu.com/s/1I61HXA_AC5oePVVmNgw6zA 提取码:gl0b

  使用步骤
  1、安装并启动,选择Evaluate for free


  2、选择config,Edit custom VM Options

  3、把agent放到非中文路径下,然后输入-javaagent:路径地址。具体见下图:

  如果不小心写错了,会导致程序无法正常启动。可以搜索如图所示后缀名 ( .vmoptions),把刚才写错的内容删掉。再执行上述操作。

  4、启动后,点击help,register
2019-11-10_190701.png
  5、启动后,点击help,register,输入注册码

  补充mingw的设定此处需要科学上网
  程序启动后,会有mingw的设定页,根据Download的提示,来到下载页面


  点击后跳转到sourceforge,自动开始下载

  双击运行,点击next

  开始下载,这里需要稍等

  下载完成!一般Clion都会自动去搜索并填写相关的内容。

  如果没有,请参照下图进行手动查找设置。

  可以简单的运行输出结果了。

  3、Notepad++
  最新下载地址:https://sourceforge.net/projects/npp-plugins/

  下载安装Notepad++,如果有插件管理功能,直接搜索NppExec安装。

  如果是老版本的notepad++,没有插件管理功能,请自行搜索后下载。



  下载dll的压缩包。
  解压包,然后把doc和NppExec复制到notepad++的plugins的文件夹下。

  重启软件后,插件中会出现插件。


  !这里只是万里长征第一步,编译器配置等等,均未完成!

  以下的相关内容,均为插件的帮助文档所讲述。
  NppExec.txt中列出的,支持的插件支持的所有命令和环境变量

 * Abilities:
 *   1) Run multiple commands from the "Execute..." dialog
 *   2) Run stand-alone command from the Console Dlg
 *   3) Separate console for each NppExec.dll (e.g. NppExec1.dll, NppExec2.dll)
 *   4) Additional commands:
 *        cls - clear Console screen
 *        cd - shows current path
 *        cd <path> - changes current directory (absolute or relative)
 *        cd <drive:\path> - changes current drive and directory
 *        dir - lists subdirs and files
 *        dir <mask> - lists subdirs and files matched the mask
 *        dir <path\mask> - lists subdirs and files matched the mask
 *        echo <text> - prints a text in the Console
 *        if <condition> goto <label> - jumps to the label if the condition is true
 *        if ... else if ... else ... endif - conditional execution
 *        goto <label> - jumps to the label
 *        exit - exits the current NppExec's script
 *        exit <type> - exits the NppExec's script
 *        set - shows all user's variables
 *        set <var> - shows the value of user's variable <var>
 *        set <var> = <value> - sets the value of user's variable <var>
 *        set <var> ~ <math expression> - calculates the math expression
 *        set <var> ~ strlen <string> - calculates the string length
 *        set <var> ~ strlenutf8 <string> - calculates the UTF-8 string length
 *        set <var> ~ strlenu <string> - the same as strlenutf8
 *        set <var> ~ strlensci <string> - string length, using Scintilla's encoding
 *        set <var> ~ strlens <string> - the same as strlensci
 *        set <var> ~ strupper <string> - returns the string in upper case
 *        set <var> ~ strlower <string> - returns the string in lower case
 *        set <var> ~ substr <pos> <len> <string> - returns the substring
 *        set <var> ~ strfind <s> <t> - returns the first position of <t> in <s>
 *        set <var> ~ strrfind <s> <t> - returns the last position of <t> in <s>
 *        set <var> ~ strreplace <s> <t0> <t1> - replaces all <t0> with <t1>
 *        set <var> ~ strfromhex <hs> - returns a string from the hex-string
 *        set <var> ~ strtohex <s> - returns a hex-string from the string
 *        set local - shows all user's local variables
 *        set local <var> - shows the value of user's local variable <var>
 *        set local <var> = ... - sets the value of user's local variable <var>
 *        set local <var> ~ ... - calculates the value of user's local variable
 *        unset <var> - removes user's variable <var>
 *        unset local <var> - removes user's local variable <var>
 *        env_set <var> - shows the value of environment variable <var>
 *        env_set <var> = <value> - sets the value of environment variable <var>
 *        env_unset <var> - removes/restores the environment variable <var>
 *        inputbox "message" - shows InputBox, sets $(INPUT)
 *        inputbox "message" : initial_value - InputBox, sets $(INPUT)
 *        inputbox "message" : "value_name" : initial_value - InputBox customization
 *        messagebox "text" - shows a simple MessageBox
 *        messagebox "text" : "title" - shows a MessageBox with a custom title
 *        messagebox "text" : "title" : type - shows a MessageBox of a given type
 *        con_colour <colours> - sets the Console's colours
 *        con_filter <filters> - enables/disables the Console's output filters
 *        con_loadfrom <file> - loads a file's content to the Console
 *        con_load <file> - see "con_loadfrom"
 *        con_saveto <file> - saves the Console's content to a file
 *        con_save - see "con_saveto"
 *        sel_loadfrom <file> - replace current selection with a file's content
 *        sel_load <file> - see "sel_loadfrom"
 *        sel_saveto <file> - save the selected text to a file
 *        sel_saveto <file> : <encoding> - save the selected text to a file
 *        sel_save <file> : <encoding> - see "sel_saveto"
 *        sel_settext <text> - replace current selection with the text specified
 *        sel_settext+ <text> - replace current selection with the text specified
 *        text_loadfrom <file> - replace the whole text with a file's content
 *        text_load <file> - see "text_loadfrom"
 *        text_saveto <file> - save the whole text to a file
 *        text_saveto <file> : <encoding> - save the whole text to a file
 *        text_save <file> : <encoding> - see "text_saveto"
 *        clip_settext <text> : set the clipboard text
 *        npp_exec <script> - execute commands from specified NppExec's script
 *        npp_exec <file> - execute commands from specified NppExec's file (*)
 *        npp_close - close current file in Notepad++
 *        npp_close <file> - close specified file opened in Notepad++      (*)
 *        npp_console <on/off/keep> - show/hide the Console window
 *        npp_console <enable/disable> - enable/disable output to the Console
 *        npp_console <1/0/?> - show/hide the Console window
 *        npp_console <+/-> - enable/disable output to the Console
 *        npp_menucommand <menu\item\name> - executes (invokes) a menu item
 *        npp_open <file> - open a file in Notepad++
 *        npp_open <mask> - open files matched the mask
 *        npp_open <path\mask> - open files matched the mask
 *        npp_run <command> - run external process/command
 *        npp_save - save current file in Notepad++
 *        npp_save <file> - save a file in Notepad++ (if it's opened)      (*)
 *        npp_saveas <file> - save current file with a new (path)name
 *        npp_saveall - save all modified files
 *        npp_switch <file> - switch to specified opened file              (*)
 *        npp_setfocus - set the keyboard focus
 *        npp_sendmsg <msg> - send a message (msg) to Notepad++
 *        npp_sendmsg <msg> <wparam> - message with parameter (wparam)
 *        npp_sendmsg <msg> <wparam> <lparam> - msg to Notepad++
 *        npp_sendmsgex <hwnd> <msg> <wparam> <lparam> - msg to hwnd
 *        sci_sendmsg <msg> - send a message (msg) to current Scintilla
 *        sci_sendmsg <msg> <wparam> - message with parameter (wparam)
 *        sci_sendmsg <msg> <wparam> <lparam> - msg to Scintilla
 *        sci_find <flags> <find_what> - find a string
 *        sci_replace <flags> <find_what> <replace_with> - replace a string
 *        proc_signal <signal> - signal to a child process
 *        sleep <ms> - sleep during ms milliseconds
 *        sleep <ms> <text> - print the text and sleep during ms milliseconds
 *        npe_cmdalias - show all command aliases
 *        npe_cmdalias <alias> - shows the value of command alias
 *        npe_cmdalias <alias> = - removes the command alias
 *        npe_cmdalias <alias> = <command> - sets the command alias
 *        npe_console <options> - set/modify Console options/mode
 *        npe_debuglog <on/off> - enable/disable Debug Log
 *        npe_debug <1/0> - see "npe_debuglog"
 *        npe_noemptyvars <1/0> - enable/disable replacement of empty vars
 *        npe_queue <command> - queue NppExec's command to be executed
 *        npe_sendmsgbuflen <max_len> - set npp/sci_sendmsg's buffer length
 *        nppexec: - prefix for NppExec's commands (e.g. "nppexec:npp_console off")
 *        nppexec:: - always executes a command in a collateral (parallel) script
 *        (*) these commands work with a partial file path/name also
 *            i.e.  npp_save c:\dir\f.txt  is the same as  npp_save f.txt
 *   5) Additional console commands (Console Dlg only):
 *        help           - show available commands
 *        help <command> - information on the specific command (e.g. "help cls")
 *        ver            - show plugin's version
 *        manual         - show NppExec User Guide
 *        about          - show NppExec Help/About
 *        CTRL+C         - kill (via Ctrl+C) or terminate current child process
 *        CTRL+BREAK     - kill (via Ctrl+Break) or terminate current child process
 *        CTRL+Z         - send ^Z to current child process
 *   6) All Notepad++ environment variables are supported:
 *        $(FULL_CURRENT_PATH)  : E:\my Web\main\welcome.html
 *        $(CURRENT_DIRECTORY)  : E:\my Web\main
 *        $(FILE_NAME)          : welcome.html
 *        $(NAME_PART)          : welcome
 *        $(EXT_PART)           : .html
 *        $(NPP_DIRECTORY)      : the full path of notepad++'s directory
 *        $(CURRENT_WORD)       : word(s) you selected in Notepad++
 *        $(CURRENT_LINE)       : current line number
 *        $(CURRENT_COLUMN)     : current column number
 *   7) Additional environment variables:
 *        $(CLIPBOARD_TEXT)     : text from the clipboard
 *        $(#0)                 : C:\Program Files\Notepad++\notepad++.exe
 *        $(#N), N=1,2,3...     : full path of the Nth opened document
 *        $(LEFT_VIEW_FILE)     : current file path-name in primary (left) view
 *        $(RIGHT_VIEW_FILE)    : current file path-name in second (right) view
 *        $(PLUGINS_CONFIG_DIR) : full path of the plugins configuration directory
 *        $(CWD)                : current working directory of NppExec (use "cd" to change it)
 *        $(ARGC)               : number of arguments passed to the NPP_EXEC command
 *        $(ARGV)               : all arguments passed to the NPP_EXEC command after the script name
 *        $(ARGV[0])            : script name - first parameter of the NPP_EXEC command
 *        $(ARGV[N])            : Nth argument (N=1,2,3...)
 *        $(RARGV)              : all arguments in reverse order (except the script name)
 *        $(RARGV[N])           : Nth argument in reverse order (N=1,2,3...)
 *        $(INPUT)              : this value is set by the 'inputbox' command
 *        $(INPUT[N])           : Nth field of the $(INPUT) value (N=1,2,3...)
 *        $(OUTPUT)             : this value can be set by the child process, see npe_console v+
 *        $(OUTPUT1)            : first line in $(OUTPUT)
 *        $(OUTPUTL)            : last line in $(OUTPUT)
 *        $(EXITCODE)           : exit code of the last executed child process
 *        $(PID)                : process id of the current (or the last) child process
 *        $(LAST_CMD_RESULT)    : result of the last NppExec's command
 *                                  (1 - succeeded, 0 - failed, -1 - invalid arg)
 *        $(MSG_RESULT)         : result of 'npp_sendmsg[ex]' or 'sci_sendmsg'
 *        $(MSG_WPARAM)         : wParam (output) of 'npp_sendmsg[ex]' or 'sci_sendmsg'
 *        $(MSG_LPARAM)         : lParam (output) of 'npp_sendmsg[ex]' or 'sci_sendmsg'
 *        $(NPP_HWND)           : Notepad++'s main window handle
 *        $(SCI_HWND)           : current Scintilla's window handle
 *        $(SYS.<var>)          : system's environment variable, e.g. $(SYS.PATH)
 *        $(@EXIT_CMD)          : a callback exit command for a child process
 *        $(@EXIT_CMD_SILENT)   : a silent (non-printed) callback exit command
 ****************************************************************************

  下面简单运行几个命令看看:

cd
CD
Current directory: C:\Program Files\Notepad++
================ READY ================
dir
DIR: *
<DIR> autoCompletion
<DIR> localization
<DIR> plugins
<DIR> updater
change.log
contextMenu.xml
functionList.xml
langs.model.xml
LICENSE
notepad++.exe
NppShell_06.dll
readme.txt
SciLexer.dll
shortcuts.xml
stylers.model.xml
uninstall.exe
================ READY ================

  输入help命令,显示如下:

help
-------- Console keys --------
Enter  -  executes entered command
Tab  -  auto-completes current command/looks through the commands history
Shift+Tab  -  the same as Tab but backwards
Arrow Up  -  previous command (when Console Commands History is enabled)
Arrow Down  -  next command (when Console Commands History is enabled)
Ctrl+C  -  copy selected text to the clipboard
Ctrl+V  -  paste from the clipboard
Ctrl+W  -  word wrap on/off
Ctrl+~  -  toggle Console/editing window
Ctrl+T  -  hide toggled Console on/off
Ctrl+F, F3, F7  -  shows Console's Find dialog
Esc  -  hides Console's Find dialog
Enter, F3 (in Console's Find dialog)  -  find next (down)
Shift+Enter, Shift+F3 (in Console's Find dialog)  -  find previous (up)
-------- Console commands --------
help  -  show available commands
help <command>  -  show detailed information about specified command
help all  -  show all available help (all the commands in detail)
ver  -  show plugin's version
manual  -  show NppExec User Guide
about  - show NppExec Help/About
nppexec:  -  prefix for NppExec's commands (e.g. "nppexec:npp_console off")
nppexec::  -  always executes a command in a collateral (parallel) script
CTRL+C  -  kill (via Ctrl+C) or terminate current child process
CTRL+BREAK  -  kill (via Ctrl+Break) or terminate current child process
CTRL+Z  -  send ^Z to current child process


第2章节  使用Code::Blocks简要分析C++的预处理

  • 2-1 头文件命名约定

类型 约定 示例 说明
C旧式风格 以h结尾 stdio.h C、C++通用
C++旧式风格 以h结尾 iostream.h C++适用
C++新式风格 无扩展名 iostream C++适用,使用using
转换后的C 前缀C,无扩展 cmath C++适用
  • 2-2 具体分析

  首先看如下代码:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello world!" << endl;
    return 0;  
}

  使用IDE打开iostream查看
  继续翻查内部的头文件,可见内部引用的文件依然含有.h
  修改代码如下,同样可以正常编译运行。
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    printf("123");

    return 0;

}
  如果修改过程中格式乱了,可以使用自动格式化代码功能:

  或者精简为:

#include <cstdio>

int main()
{
    printf("123");
    return 0;

}
  为什么能正常编译?我们具体来看一看cstdio的内部:
  2-2-1. 导言—用户管理—用户的分类及介绍

第3章节  编程规范


第4章节 编译过程简探

  四个过程,四个概念:预处理(Prepressing)、编译(Compilation)、汇编(Assembly)、链接(Linking)
  根据GCC手册,3 GCC Command Options:When you compile C++ programs, you should invoke GCC as g++ instead.

  一、预处理(Prepressing)
  使用CodeBlocks创建一个默认的C++项目,然后执行一次build and run,找到该目录所在的文件夹,包含内容如图所示:

  上面提到的预处理,相当于如下命令:

g++ -E main.cpp -o main.ii -v
//对main.cpp进行预处理,把结果输出到main.ii文件中,执行过程中打印执行有关的信息

  查阅GCC手册,上述的参数解释如下:
  -E:(只执行预处理操作)
  Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.Input files that don’t require preprocessing are ignored.

  -o file:(输出到文件file中)
  Place output in file file. This applies to whatever sort of output is being produced,whether it be an executable file, an object file, an assembler file or preprocessed C code.
  If ‘-o’ is not specified, the default is to put an executable file in ‘a.out’, the object file for ‘source.suffix’ in ‘source.o’, its assembler file in ‘source.s’, a precompiled header file in ‘source.suffix.gch’, and all preprocessed C source on standard output.

  -v:(打印命令执行过程中的相关信息)
  Print (on standard error output) the commands executed to run the stages of compilation. Also print the version number of the compiler driver program and of the preprocessor and the compiler proper.


C:\Users\lo\Desktop\New Folder\1\1>g++ -E main.cpp -o main.ii -v


Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=mingw32 --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --disable-symvers --enable-cxx-flags='-fno-function-sections -fno-data-sections -DWINPTHREAD_STATIC' --prefix=/mingw32tdm --with-local-prefix=/mingw32tdm --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm-1)
COLLECT_GCC_OPTIONS='-E' '-o' 'main.ii' '-v' '-mtune=generic' '-march=pentiumpro'
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/cc1plus.exe -E -quiet -v -iprefix C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/ -D_REENTRANT main.cpp -o main.ii -mtune=generic -march=pentiumpro
ignoring nonexistent directory "C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++/mingw32"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++/backward"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/../../../../include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include-fixed"
ignoring nonexistent directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/../../../../mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++/mingw32
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++/backward
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../include
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include-fixed
End of search list.
COMPILER_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/
LIBRARY_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/lib/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../
COLLECT_GCC_OPTIONS='-E' '-o' 'main.ii' '-v' '-mtune=generic' '-march=pentiumpro'

  或者使用--verbose也能达到相同效果

C:\Users\lo\Desktop\New Folder\1\1>g++ -E main.cpp -o main.ii --verbose
Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=mingw32 --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --disable-symvers --enable-cxx-flags='-fno-function-sections -fno-data-sections -DWINPTHREAD_STATIC' --prefix=/mingw32tdm --with-local-prefix=/mingw32tdm --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm-1)
COLLECT_GCC_OPTIONS='-E' '-o' 'main.ii' '-v' '-mtune=generic' '-march=pentiumpro'
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/cc1plus.exe -E -quiet -v -iprefix C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/ -D_REENTRANT main.cpp -o main.ii -mtune=generic -march=pentiumpro
ignoring nonexistent directory "C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++/mingw32"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++/backward"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/../../../../include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include-fixed"
ignoring nonexistent directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/../../../../mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++/mingw32
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++/backward
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../include
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include-fixed
End of search list.
COMPILER_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/
LIBRARY_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/lib/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../
COLLECT_GCC_OPTIONS='-E' '-o' 'main.ii' '-v' '-mtune=generic' '-march=pentiumpro'
  打开main.ii文件查看,一共是466440行代码。

  1、将所有的"#define"删除, 并且展开所有的宏定义。
  2、处理所有条件预编译指令, 比如"#if"、"#ifdef"、 "#elif'、"#else"、"#endif"。
  3、处理"#include"预编译指令, 将被包含的文件插入到该预编译指令的位置。 注意, 这个过程是递归进行的, 也就是说被包含的文件可能还包含其他文件。
  4、删除所有的注释"//"和"/* */"
  5、添加行号和文件名标识, 比如#2 "hello.c" 2, 以便于编译时编译器产生调试用的行号信息及用于编译时产生编译错误或警告时能够显示行号。
  6、保留所有的#pragma编译器指令 , 因为编译器须要使用它们。

  二、编译(compilation)
  把预处理完的文件进行一系列词法分析、语法分析、语义分析及优化后生产相应的汇编代码文件。
  根据GCC手册,Chapter 3: GCC Command Options,3.2 Options Controlling the Kind of Output:
  -S:(执行编译操作)
  Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified.By default, the assembler file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, etc., with ‘.s’.

C:\Users\lo\Desktop\New Folder\1\1>g++ -S main.ii -o main.s -v
Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=mingw32 --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --disable-symvers --enable-cxx-flags='-fno-function-sections -fno-data-sections -DWINPTHREAD_STATIC' --prefix=/mingw32tdm --with-local-prefix=/mingw32tdm --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm-1)
COLLECT_GCC_OPTIONS='-S' '-o' 'main.s' '-v' '-mtune=generic' '-march=pentiumpro'
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/cc1plus.exe -fpreprocessed main.ii -quiet -dumpbase main.ii -mtune=generic -march=pentiumpro -auxbase-strip main.s -version -o main.s
GNU C++ (tdm-1) version 5.1.0 (mingw32)
        compiled by GNU C version 5.1.0, GMP version 4.3.2, MPFR version 2.4.2, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++ (tdm-1) version 5.1.0 (mingw32)
        compiled by GNU C version 5.1.0, GMP version 4.3.2, MPFR version 2.4.2, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 2e47d181020f054da881eda9fc3e9f75
COMPILER_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/
LIBRARY_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/lib/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../
COLLECT_GCC_OPTIONS='-S' '-o' 'main.s' '-v' '-mtune=generic' '-march=pentiumpro'

  三、汇编(assembly)
  汇编器是将汇编代码转变成机器可以执行的指令。

  根据GCC手册,Chapter 3: GCC Command Options,3.2 Options Controlling the Kind of Output:
  -C:(执行汇编操作)
  Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
  By default, the object file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, ‘.s’, etc., with ‘.o’.
  Unrecognized input files, not requiring compilation or assembly, are ignored.

C:\Users\lo\Desktop\New Folder\1\1>g++ -c main.s -o main.o -v
Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=mingw32 --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --disable-symvers --enable-cxx-flags='-fno-function-sections -fno-data-sections -DWINPTHREAD_STATIC' --prefix=/mingw32tdm --with-local-prefix=/mingw32tdm --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm-1)
COLLECT_GCC_OPTIONS='-c' '-o' 'main.o' '-v' '-mtune=generic' '-march=pentiumpro'
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/as.exe -v -o main.o main.s
GNU assembler version 2.24 (mingw32) using BFD version (GNU Binutils) 2.24
COMPILER_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/
LIBRARY_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/lib/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../
COLLECT_GCC_OPTIONS='-c' '-o' 'main.o' '-v' '-mtune=generic' '-march=pentiumpro'

  根据ASCII表,可知0x74确实是字符t。


  四、链接(linking)

  根据gcc手册之3.14 Options for Linking
  -static:(静态链接)
  On systems that support dynamic linking, this overrides ‘-pie’ and prevents linking with the shared libraries. On other systems, this option has no effect.
  要分析链接的一些粗略细节,要让g++完成整个编译的过程,然后从中寻找踪迹。


  下面把整个过程割裂为几个部分,用来突出关键的地方:
C:\Users\lo\Desktop\New Folder\1\1>g++ main.cpp -o main.oo -static -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/Program\ Files\ (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/lto-wrapper.exe
Target: mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=mingw32 --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --disable-symvers --enable-cxx-flags='-fno-function-sections -fno-data-sections -DWINPTHREAD_STATIC' --prefix=/mingw32tdm --with-local-prefix=/mingw32tdm --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm-1)
COLLECT_GCC_OPTIONS='-o' 'main.oo' '-static' '-v' '-mtune=generic' '-march=pentiumpro'

  使用cc1plus.exe进行编译

C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/cc1plus.exe -quiet -v -iprefix C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/ -D_REENTRANT main.cpp -quiet -dumpbase main.cpp -mtune=generic -march=pentiumpro -auxbase main -version -o C:\Users\lo\AppData\Local\Temp\ccticUlp.s
GNU C++ (tdm-1) version 5.1.0 (mingw32)
        compiled by GNU C version 5.1.0, GMP version 4.3.2, MPFR version 2.4.2, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++/mingw32"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include/c++/backward"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/../../../../include"
ignoring duplicate directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/include-fixed"
ignoring nonexistent directory "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/../../lib/gcc/mingw32/5.1.0/../../../../mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++/mingw32
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include/c++/backward
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../include
 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/include-fixed
End of search list.
GNU C++ (tdm-1) version 5.1.0 (mingw32)
        compiled by GNU C version 5.1.0, GMP version 4.3.2, MPFR version 2.4.2, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 2e47d181020f054da881eda9fc3e9f75
COLLECT_GCC_OPTIONS='-o' 'main.oo' '-static' '-v' '-mtune=generic' '-march=pentiumpro'

  使用as.exe进行汇编

 C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/as.exe -v -o C:\Users\lo\AppData\Local\Temp\ccc0ZC3X.o C:\Users\lo\AppData\Local\Temp\ccticUlp.s
GNU assembler version 2.24 (mingw32) using BFD version (GNU Binutils) 2.24
COMPILER_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/
LIBRARY_PATH=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/lib/;C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../
COLLECT_GCC_OPTIONS='-o' 'main.oo' '-static' '-v' '-mtune=generic' '-march=pentiumpro'

  使用collect2.exe进行链接

C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/collect2.exe -plugin C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/liblto_plugin-0.dll -plugin-opt=C:/Program Files (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/mingw32/5.1.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\lo\AppData\Local\Temp\ccBNranD.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --exclude-libs=libpthread.a -Bstatic -o main.oo C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../crt2.o C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/crtbegin.o -LC:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0 -LC:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc -LC:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/lib -LC:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../.. C:\Users\lo\AppData\Local\Temp\ccc0ZC3X.o -lstdc++ -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/crtend.o

  对上述输出进行筛选,可以看到如下一些.o文件出现:crt2.o、crtbegin.o、ccc0ZC3X.o、crtend.o。

  1、首先要关注的点是,这里调用的的是collect2.exe。参阅以下链接说明,大致可以把collect2.exe作用表述为:是ld的一种封装,执行的是一些初始化和链接操作。
  https://gcc.gnu.org/onlinedocs/gccint/Collect2.html#Collect2
  The program collect2 is installed as ld in the directory where the passes of the compiler are installed. When collect2 needs to find the real ld, it tries the following file names:
  ֍ a hard coded linker file name, if GCC was configured with the --with-ld option.
  ֍ real-ld in the directories listed in the compiler’s search directories.
  ֍ real-ld in the directories listed in the environment variable PATH.
  ֍ The file specified in the REAL_LD_FILE_NAME configuration macro, if specified.
  ֍ ld in the compiler’s search directories, except that collect2 will not execute itself recursively.
  ֍ ld in PATH.

  2、其次要关注的点是参数-plugin:
  https://sourceware.org/binutils/docs-2.33.1/ld/Options.html#Options
  -plugin name:
  Involve a plugin in the linking process. The name parameter is the absolute filename of the plugin. Usually this parameter is automatically added by the complier, when using link time optimization, but users can also add their own plugins if they so wish.
  Note that the location of the compiler originated plugins is different from the place where the ar, nm and ranlib programs search for their plugins. In order for those commands to make use of a compiler based plugin it must first be copied into the ${libdir}/bfd-plugins directory. All gcc based linker plugins are backward compatible, so it is sufficient to just copy in the newest one.

  3、还有-L参数的使用:
  https://sourceware.org/binutils/docs-2.33.1/ld/Options.html#Options
  -L searchdir:
  --library-path=searchdir:
  Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts. You may use this option any number of times. The directories are searched in the order in which they are specified on the command line. Directories specified on the command line are searched before the default directories. All -L options apply to all -l options, regardless of the order in which the options appear. -L options do not affect how ld searches for a linker script unless -T option is specified.
  If searchdir begins with = or $SYSROOT, then this prefix will be replaced by the sysroot prefix, controlled by the ‘--sysroot’ option, or specified when the linker is configured.
  The default set of paths searched (without being specified with ‘-L’) depends on which emulation mode ld is using, and in some cases also on how it was configured. See Environment.
  The paths can also be specified in a link script with the SEARCH_DIR command. Directories specified this way are searched at the point in which the linker script appears in the command line.

  根据gcc手册之3.14 Options for Linking
  -llibrary:
  -l library:
  Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
  The ‘-l’ option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.
  The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with ‘-L’.
  Static libraries are archives of object files, and have file names like ‘liblibrary.a’. Some targets also support shared libraries, which typically have names like ‘liblibrary.so’. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the ‘-static’ option is used.
  It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file ‘foo.o’ but before ‘bar.o’. If ‘bar.o’ refers to functions in ‘z’, those functions may not be loaded.

第4章节 mingw含带的二进制工具一览

  在mingw的安装目录的bin文件夹下:


  https://sourceware.org/binutils/docs/binutils/列出的部分工具的文档

相关文章

网友评论

      本文标题:基础C++教学⮱⮱001

      本文链接:https://www.haomeiwen.com/subject/egpobctx.html